File size: 11,008 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 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 |
/**
* ConversionProgressDialog Component
*
* Self-contained dialog for MDX/MDD conversion and fulltext index generation
* Shows progress for all stages and switches to completion mode when done
*/
import React, { useState, useEffect } from 'react';
import { useTranslation } from 'react-i18next';
import {
IonModal,
IonHeader,
IonToolbar,
IonTitle,
IonContent,
IonLabel,
IonProgressBar,
IonNote,
IonButton,
IonFooter,
IonAlert,
} from '@ionic/react';
import { listen, UnlistenFn } from '@tauri-apps/api/event';
export interface ConversionProgress {
stage: 'mdx' | 'mdd' | 'idx';
subStage: string;
current: number;
total: number;
message?: string;
}
interface StageInfo {
current: number;
total: number;
subStage: string;
}
interface StageProgress {
mdx: StageInfo;
mdd: StageInfo;
idx: StageInfo;
}
interface ConversionProgressDialogProps {
isOpen: boolean;
profileTitle: string;
showFulltextIndex?: boolean;
errorMessage?: string;
onCancel: () => void;
onComplete: () => void;
}
export const ConversionProgressDialog: React.FC<ConversionProgressDialogProps> = ({
isOpen,
profileTitle,
showFulltextIndex = true,
errorMessage: externalErrorMessage,
onCancel,
onComplete,
}) => {
const { t } = useTranslation();
const [stageProgress, setStageProgress] = useState<StageProgress>({
mdx: { current: 0, total: 100, subStage: '' },
mdd: { current: 0, total: 100, subStage: '' },
idx: { current: 0, total: 100, subStage: '' },
});
const [currentStage, setCurrentStage] = useState<'mdx' | 'mdd' | 'idx'>('mdx');
const [currentMessage, setCurrentMessage] = useState<string | undefined>();
const [isCompleted, setIsCompleted] = useState(false);
const [showCancelAlert, setShowCancelAlert] = useState(false);
// Use external error message if provided
const hasError = !!externalErrorMessage;
const errorMessage = externalErrorMessage || '';
useEffect(() => {
if (!isOpen) {
// Reset state when dialog closes
setStageProgress({
mdx: { current: 0, total: 100, subStage: '' },
mdd: { current: 0, total: 100, subStage: '' },
idx: { current: 0, total: 100, subStage: '' },
});
setCurrentStage('mdx');
setCurrentMessage(undefined);
setIsCompleted(false);
}
}, [isOpen]);
useEffect(() => {
let unlisten: UnlistenFn | null = null;
if (isOpen) {
// Listen for progress updates from backend
const setupListener = async () => {
unlisten = await listen<ConversionProgress>('conversion-progress', (event) => {
const { stage, subStage, current, total, message } = event.payload;
console.log('Progress update:', { stage, subStage, current, total, message });
setCurrentStage(stage);
setCurrentMessage(message);
setStageProgress(prev => ({
...prev,
[stage]: { current, total, subStage },
}));
});
};
setupListener();
}
return () => {
if (unlisten) {
unlisten();
}
};
}, [isOpen]);
const getStageLabel = (stage: 'mdx' | 'mdd' | 'idx') => {
switch (stage) {
case 'mdx':
return t('Converting MDX file');
case 'mdd':
return t('Converting MDD file');
case 'idx':
return t('Generating fulltext index');
default:
return t('Processing');
}
};
const getSubStageLabel = (subStage: string) => {
if (!subStage) return '';
// Replace :: with __ to avoid i18next namespace interpretation
const normalizedSubStage = subStage.replace(/::/g, '__');
const key = `substage_${normalizedSubStage}`;
const translated = t(key);
const found = translated !== key;
console.log('[SubStage] Translation:', {
subStage,
normalizedSubStage,
key,
translated,
found
});
// If translation not found, format the substage name nicely
if (!found) {
// Extract the function name after :: if present
const nameOnly = subStage.includes('::') ? subStage.split('::').pop() || subStage : subStage;
const formatted = nameOnly.replace(/_/g, ' ');
return formatted;
}
return translated;
};
const getProgressPercent = (stage: 'mdx' | 'mdd' | 'idx') => {
const { current, total } = stageProgress[stage];
return total > 0 ? current / total : 0;
};
const isStageComplete = (stage: 'mdx' | 'mdd' | 'idx') => {
const { subStage } = stageProgress[stage];
// Check if the stage has reached "completed" or "skipped" substage
return subStage === 'completed' || subStage === 'skipped';
};
const handleCancelClick = () => {
if (isCompleted || hasError) {
// If completed or has error, directly close
if (isCompleted) {
onComplete();
} else {
onCancel();
}
} else {
// If not completed and no error, show confirmation dialog
setShowCancelAlert(true);
}
};
const handleCancelConfirm = () => {
setShowCancelAlert(false);
onCancel();
};
// Mark as completed when all stages are done
useEffect(() => {
const mdxComplete = isStageComplete('mdx');
const mddComplete = isStageComplete('mdd');
const idxComplete = showFulltextIndex ? isStageComplete('idx') : true;
if (mdxComplete && mddComplete && idxComplete && !isCompleted) {
setIsCompleted(true);
setCurrentMessage(t('Conversion completed successfully'));
}
}, [stageProgress, showFulltextIndex, isCompleted, t]);
return (
<>
<IonModal
isOpen={isOpen}
backdropDismiss={false}
>
<IonHeader>
<IonToolbar>
<IonTitle>{t('Rebuild Index')}: {profileTitle}</IonTitle>
</IonToolbar>
</IonHeader>
<IonContent className="ion-padding">
<div style={{
minHeight: '200px',
display: 'flex',
flexDirection: 'column',
gap: '20px',
paddingTop: '20px'
}}>
{/* MDX Stage */}
<div>
<IonLabel>
<h3 style={{ margin: '0 0 4px 0' }}>
{getStageLabel('mdx')}
{isStageComplete('mdx') && ' ✓'}
</h3>
{stageProgress.mdx.subStage && (
<IonNote style={{ fontSize: '11px', display: 'block', marginBottom: '4px' }}>
{getSubStageLabel(stageProgress.mdx.subStage)}
</IonNote>
)}
</IonLabel>
<IonProgressBar
value={getProgressPercent('mdx')}
color={currentStage === 'mdx' ? 'primary' : 'medium'}
style={{ height: '6px' }}
/>
<IonNote style={{ fontSize: '12px' }}>
{stageProgress.mdx.current} / {stageProgress.mdx.total}
{' '}({Math.round(getProgressPercent('mdx') * 100)}%)
</IonNote>
</div>
{/* MDD Stage */}
<div>
<IonLabel>
<h3 style={{ margin: '0 0 4px 0' }}>
{getStageLabel('mdd')}
{isStageComplete('mdd') && ' ✓'}
</h3>
{stageProgress.mdd.subStage && (
<IonNote style={{ fontSize: '11px', display: 'block', marginBottom: '4px' }}>
{getSubStageLabel(stageProgress.mdd.subStage)}
</IonNote>
)}
</IonLabel>
<IonProgressBar
value={getProgressPercent('mdd')}
color={currentStage === 'mdd' ? 'primary' : 'medium'}
style={{ height: '6px' }}
/>
<IonNote style={{ fontSize: '12px' }}>
{stageProgress.mdd.current} / {stageProgress.mdd.total}
{' '}({Math.round(getProgressPercent('mdd') * 100)}%)
</IonNote>
</div>
{/* IDX Stage - Only show if fulltext index is enabled */}
{showFulltextIndex && (
<div>
<IonLabel>
<h3 style={{ margin: '0 0 4px 0' }}>
{getStageLabel('idx')}
{isStageComplete('idx') && ' ✓'}
</h3>
{stageProgress.idx.subStage && (
<IonNote style={{ fontSize: '11px', display: 'block', marginBottom: '4px' }}>
{getSubStageLabel(stageProgress.idx.subStage)}
</IonNote>
)}
</IonLabel>
<IonProgressBar
value={getProgressPercent('idx')}
color={currentStage === 'idx' ? 'primary' : 'medium'}
style={{ height: '6px' }}
/>
<IonNote style={{ fontSize: '12px' }}>
{stageProgress.idx.current} / {stageProgress.idx.total}
{' '}({Math.round(getProgressPercent('idx') * 100)}%)
</IonNote>
</div>
)}
{/* Message */}
{currentMessage && !hasError && (
<IonNote
color={isCompleted ? 'success' : 'primary'}
style={{ textAlign: 'center', marginTop: '10px' }}
>
{currentMessage}
</IonNote>
)}
{/* Error Message */}
{hasError && (
<div style={{
marginTop: '20px',
padding: '16px',
backgroundColor: 'var(--ion-color-danger-tint)',
borderRadius: '8px',
border: '1px solid var(--ion-color-danger)'
}}>
<IonLabel>
<h3 style={{ margin: '0 0 8px 0', color: 'var(--ion-color-danger)' }}>
{t('Conversion failed')}
</h3>
<p style={{ margin: 0, whiteSpace: 'pre-wrap', fontSize: '14px' }}>
{errorMessage}
</p>
</IonLabel>
</div>
)}
</div>
</IonContent>
<IonFooter>
<IonToolbar>
<div style={{ display: 'flex', justifyContent: 'flex-end', padding: '10px' }}>
<IonButton
color={isCompleted ? 'primary' : hasError ? 'medium' : 'danger'}
onClick={handleCancelClick}
>
{isCompleted ? t('OK') : hasError ? t('Close') : t('Cancel')}
</IonButton>
</div>
</IonToolbar>
</IonFooter>
</IonModal>
{/* Cancel Confirmation Alert */}
<IonAlert
isOpen={showCancelAlert}
onDidDismiss={() => setShowCancelAlert(false)}
header={t('Confirm Cancellation')}
message={t('Are you sure you want to cancel the conversion? All progress will be lost.')}
buttons={[
{
text: t('No'),
role: 'cancel',
handler: () => setShowCancelAlert(false)
},
{
text: t('Yes'),
role: 'destructive',
handler: handleCancelConfirm
}
]}
/>
</>
);
};
|