File size: 4,470 Bytes
07bbbbf |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 |
import React from 'react';
import { IonPopover, IonList, IonItem, IonLabel, IonAvatar } from '@ionic/react';
import { bookOutline } from 'ionicons/icons';
import { useTranslation } from 'react-i18next';
import { MdxProfile } from '../types';
import { getIconUrl } from '../utils/iframeTools/mdxHtmlTools';
interface ProfileListMenuProps {
profiles: MdxProfile[];
activeProfileId: number;
onProfileSelect: (profileId: number) => void;
anchorEl: HTMLElement | null;
open: boolean;
onClose: () => void;
}
/**
* ProfileListMenu Component
*
* A dropdown menu component that displays a list of profiles (dictionaries).
* Uses Ionic Popover component for consistent behavior.
* This component only handles the menu rendering - the trigger button
* should be managed by the parent component.
*
* @example
* ```tsx
* <ProfileListMenu
* profiles={profiles}
* activeProfileId={currentProfileId}
* onProfileSelect={(profileId) => handleProfileChange(profileId)}
* anchorEl={anchorEl}
* open={open}
* onClose={() => setAnchorEl(null)}
* />
* ```
*/
export const ProfileListMenu: React.FC<ProfileListMenuProps> = ({
profiles,
activeProfileId,
onProfileSelect,
anchorEl,
open,
onClose,
}) => {
const { t } = useTranslation();
const handleProfileSelect = (profileId: number) => {
onProfileSelect(profileId);
onClose();
};
// Don't render menu if there are no profiles or only one profile
if (!profiles.length || profiles.length <= 1) {
return null;
}
return (
<IonPopover
isOpen={open}
event={anchorEl ? { target: anchorEl } as any : undefined}
onDidDismiss={onClose}
side="bottom"
alignment="end"
style={{
'--min-width': '300px',
'--max-width': '400px',
'--max-height': '400px',
}}
>
{/* Header */}
<div style={{
padding: '8px 16px',
borderBottom: '1px solid var(--ion-color-step-150)',
backgroundColor: 'var(--ion-color-step-50)'
}}>
<div style={{
color: 'var(--ion-color-medium)',
fontSize: '13px',
fontWeight: 500
}}>
{t('Select Dictionary ({count} total)', { count: profiles.length })}
</div>
</div>
{/* Profile list */}
<IonList lines="none" style={{ padding: 0 }}>
{profiles.map((profile) => {
const isActive = profile.profileId === activeProfileId;
return (
<IonItem
key={profile.profileId}
button
detail={false}
onClick={() => handleProfileSelect(profile.profileId)}
color={isActive ? 'light' : undefined}
style={{
'--padding-start': '16px',
'--padding-end': '16px',
'--min-height': '40px',
}}
>
<IonAvatar slot="start" style={{ width: '20px', height: '20px', display: 'flex', alignItems: 'center', justifyContent: 'center' }}>
<img
src={getIconUrl(profile.profileId)}
onError={(e) => {
// Fallback to icon if image fails to load
e.currentTarget.style.display = 'none';
e.currentTarget.parentElement!.innerHTML = `
<ion-icon icon="${bookOutline}" style="align-items: center; font-size: 24px; color: ${profile.disabled ? 'var(--ion-color-medium)' : 'var(--ion-color-primary)'}"></ion-icon>
`;
}}
/>
</IonAvatar>
<IonLabel>
<div style={{
fontWeight: isActive ? 600 : 400,
color: isActive ? 'var(--ion-color-primary)' : 'var(--ion-color-step-850)',
overflow: 'hidden',
textOverflow: 'ellipsis',
whiteSpace: 'nowrap',
fontSize: '14px'
}}>
{profile.title}
</div>
</IonLabel>
{isActive && (
<div slot="end" style={{
width: '8px',
height: '8px',
borderRadius: '50%',
backgroundColor: 'var(--ion-color-primary)',
}} />
)}
</IonItem>
);
})}
</IonList>
</IonPopover>
);
};
export default ProfileListMenu;
|