File size: 10,704 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 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 |
import { useState, useEffect, useCallback } from 'react';
import {
IonIcon,
IonText,
IonReorderGroup,
useIonToast,
} from '@ionic/react';
import {
bookOutline,
} from 'ionicons/icons';
import { ReorderEndCustomEvent } from '@ionic/core';
import { MdxProfile } from '../../../types';
import { ProfileListItem } from './ProfileListItem';
import { useLibraryStore } from '../../../store/useLibraryStore';
import { useSearchStore } from '../../../store/useSearchStore';
import { convertFile, createFtsIndex, cancelConversion, ConvertOptions, CollationOptions } from '../../../api/library';
import { useTranslation } from 'react-i18next';
import { ConvertDBDialogWrapper } from '../../../components/ConvertDBDialogWrapper';
import { ConversionProgressDialog } from '../../../components/ConversionProgressDialog';
/**
* ProfileListView - Combined Component
*
* Handles profile list management with drag-and-drop reordering and enable/disable toggling.
* All state is managed internally with props for initial values.
*/
export interface ProfileListViewProps {
parentGroupId: number;
onSelectProfile?: (profileId: number) => void;
isEditMode?: boolean;
}
export const ProfileListView: React.FC<ProfileListViewProps> = ({
parentGroupId,
onSelectProfile,
isEditMode = false,
}) => {
const { t } = useTranslation();
const [presentToast] = useIonToast();
// State
const [profiles, setProfiles] = useState<MdxProfile[]>([]);
const {updateProfileDisabledStatus, adjustProfileOrder, getGroup, groups} = useLibraryStore();
const currentProfile = useSearchStore((state) => state.currentProfile);
useEffect(() => {
const group = getGroup(parentGroupId);
if (group && group.profiles) {
// Mark the current profile as active
let profiles = group.profiles.map(profile => ({
...profile,
isActive: profile.profileId === currentProfile?.profileId,
}));
setProfiles(profiles);
}else{
setProfiles([]);
}
}, [parentGroupId, getGroup, currentProfile, groups]);
// Handle profile open
const handleOpenProfile = useCallback((profile: MdxProfile) => {
onSelectProfile?.(profile.profileId);
}, [onSelectProfile]);
// Handle profile toggle
const handleToggleProfileInGroup = useCallback(async (profile: MdxProfile) => {
if (profiles) {
const newStatus=!profile.disabled;
await updateProfileDisabledStatus(parentGroupId, profile.profileId, newStatus);
//If the profile is disabled, adjust the order before the disabled profile
//Otherwise, adjust the order after the enabled profile
if (profiles && profiles.length > 0) {
const currentIndex = profiles?.findIndex(p => p.profileId === profile.profileId);
let newIndex = currentIndex;
if (newStatus) {
newIndex = profiles.findIndex(p => !p.disabled) + 1;
}else{
newIndex = profiles.findIndex(p => p.disabled);
}
if (newIndex !== undefined) {
await adjustProfileOrder(parentGroupId, profile.profileId, newIndex);
}
}
}
}, [updateProfileDisabledStatus, adjustProfileOrder, profiles, parentGroupId ]);
// Handle drag end - using event's from/to properties
const handleReorderEnd = useCallback(async (event: ReorderEndCustomEvent) => {
event.detail.complete();
const { from, to } = event.detail;
const movedProfile = profiles[from];
if (movedProfile) {
await adjustProfileOrder(parentGroupId, movedProfile.profileId, to);
}
}, [parentGroupId, profiles, adjustProfileOrder]);
// State for conversion progress dialog
const [showProgressDialog, setShowProgressDialog] = useState(false);
const [progressProfile, setProgressProfile] = useState<MdxProfile | null>(null);
const [progressOptions, setProgressOptions] = useState<ConvertOptions | null>(null);
const [conversionError, setConversionError] = useState<string>('');
// Handle rebuild index confirmation
const handleRebuildConfirm = useCallback(async (profile: MdxProfile, options: ConvertOptions) => {
// Build collation locale string from collation options
let collationLocale = options.collation.language;
const extensions: string[] = [];
if (options.collation.collationType && options.collation.collationType !== 'standard') {
extensions.push(`co-${options.collation.collationType}`);
}
const strength = options.collation.strength.replace('level', 'level');
extensions.push(`ks-${strength}`);
if (options.collation.alternateHandling === 'shifted') {
extensions.push('ka-shifted');
}
if (options.collation.caseLevel === 'true') {
extensions.push('kc-true');
}
if (options.collation.caseFirst !== 'off') {
extensions.push(`kf-${options.collation.caseFirst}`);
}
if (extensions.length > 0) {
collationLocale += '-u-' + extensions.join('-');
}
// Show progress dialog and reset error state
setProgressProfile(profile);
setProgressOptions(options);
setConversionError('');
setShowProgressDialog(true);
try {
// Step 1: Convert files
const conversionResult = await convertFile(profile.profileId, collationLocale, options.removeOldFiles);
// Step 2: Create fulltext index (if enabled)
// Always use the converted new mdx file for full-text indexing
if (options.createFulltextIndex) {
await createFtsIndex(conversionResult.newMdxPath);
}
// Conversion completed - dialog will show completion state automatically
} catch (error) {
console.error('Failed to rebuild index:', error);
// Don't close dialog, just set error message
const errorMsg = error instanceof Error ? error.message : String(error);
setConversionError(errorMsg);
}
}, [t]);
// Handle opening rebuild dialog
const handleOpenRebuildDialog = useCallback(async (profile: MdxProfile) => {
// Check if the profile is currently in use
// A profile is in use if:
// 1. It's the currently active single profile (currentProfile.profileId === profile.profileId)
// 2. It's within an active group (currentProfile is a group containing this profile)
let isProfileInUse = false;
if (currentProfile) {
// Check if it's the current single profile
if (currentProfile.profileId === profile.profileId) {
isProfileInUse = true;
}
// Check if it's within the current group
else if (currentProfile.profiles && currentProfile.profiles.length > 0) {
isProfileInUse = currentProfile.profiles.some(p => p.profileId === profile.profileId);
}
}
// Reset options to default values
const defaultCollationOptions: CollationOptions = {
language: 'zh',
collationType: 'pinyin',
strength: 'level1',
alternateHandling: 'shifted',
caseLevel: 'false',
caseFirst: 'off',
};
const defaultOptions: ConvertOptions = {
collation: defaultCollationOptions,
removeOldFiles: !isProfileInUse, // Default to false if profile is in use
createFulltextIndex: true,
};
// Use a ref-like object to store the latest options
const optionsRef = { current: { ...defaultOptions } };
// Show rebuild dialog using showEnhancedDialog
const { showEnhancedDialog } = await import('../../../components/EnhancedDialog');
showEnhancedDialog({
children: (
<ConvertDBDialogWrapper
profile={profile}
initialOptions={defaultOptions}
onOptionsReady={(newOptions) => {
optionsRef.current = newOptions;
}}
isProfileInUse={isProfileInUse}
/>
),
showButtons: true,
confirmLabel: t('Start Rebuild'),
cancelLabel: t('Cancel'),
backdropDismiss: true,
onConfirm: () => {
handleRebuildConfirm(profile, optionsRef.current);
},
});
}, [t, handleRebuildConfirm, currentProfile]);
if (!profiles) {
return null;
}
return (
<div style={{
display: 'flex',
flexDirection: 'column',
height: '100%',
overflow: 'hidden',
minHeight: 0
}}>
{/* Empty state */}
{profiles?.length === 0 && (
<div
style={{
textAlign: 'center',
padding: '48px 24px',
color: 'var(--ion-color-medium)',
display: 'flex',
flexDirection: 'column',
justifyContent: 'center',
alignItems: 'center',
minHeight: '200px',
}}
>
<div style={{ fontSize: '48px', opacity: 0.3, marginBottom: '16px' }}>
<IonIcon icon={bookOutline} style={{ fontSize: '48px' }} />
</div>
<IonText>
<h3 style={{ margin: 0 }}>
该词典组暂无词典
</h3>
</IonText>
</div>
)}
{/* Profile list with drag-and-drop */}
{profiles.length > 0 && (
<IonReorderGroup disabled={false} onIonReorderEnd={handleReorderEnd}>
{profiles?.map((profile) => (
<ProfileListItem
key={profile.profileId}
profile={profile}
onToggle={handleToggleProfileInGroup}
onOpen={handleOpenProfile}
onRebuildIndex={handleOpenRebuildDialog}
showToggle={isEditMode}
showReorder={isEditMode}
showRebuildButton={true}
/>
))}
</IonReorderGroup>
)}
{/* Conversion Progress Dialog */}
{progressProfile && progressOptions && (
<ConversionProgressDialog
isOpen={showProgressDialog}
profileTitle={progressProfile.title}
showFulltextIndex={progressOptions.createFulltextIndex}
errorMessage={conversionError}
onCancel={async () => {
try {
await cancelConversion();
setShowProgressDialog(false);
setConversionError('');
await presentToast({
message: t('Conversion cancelled'),
duration: 2000,
color: 'warning',
});
} catch (error) {
console.error('Failed to cancel conversion:', error);
}
}}
onComplete={() => {
setShowProgressDialog(false);
setConversionError('');
presentToast({
message: t('Conversion completed successfully'),
duration: 2000,
color: 'success',
});
}}
/>
)}
</div>
);
};
|