import React, {useCallback, useEffect, useState} from 'react'; import {Grid, Icon, Label, Progress} from 'semantic-ui-react'; import Message from 'semantic-ui-react/dist/commonjs/collections/Message'; import {Button, Modal, Table} from '../Theme'; import {APIButton} from '../Common'; import { executeCollectionReorganization, getReorganizationStatus, previewCollectionReorganization } from '../../api'; import {useReorganizationStatus} from '../../contexts/FileWorkerStatusContext'; /** * Modal component for previewing and executing collection file reorganization. * * Shows a preview of files that will be moved when the file_name_format changes, * allows user to execute the reorganization, and tracks progress. * * @param {boolean} open - Whether the modal is open * @param {Function} onClose - Callback when modal is closed * @param {number} collectionId - ID of the collection to reorganize * @param {string} collectionName - Name of the collection for display * @param {Function} onComplete - Optional callback when reorganization completes * @param {boolean} needsReorganization - Whether the collection.needs_reorganization flag is true */ export function CollectionReorganizeModal({ open, onClose, collectionId, collectionName, onComplete, needsReorganization, }) { const [loading, setLoading] = useState(false); const [preview, setPreview] = useState(null); const [error, setError] = useState(null); const [jobId, setJobId] = useState(null); const [status, setStatus] = useState(null); const [polling, setPolling] = useState(false); // Check if this collection is currently being reorganized const { isReorganizing, taskType, collectionId: activeCollectionId, workerStatus, } = useReorganizationStatus(); // Load preview when modal opens, or resume if already reorganizing useEffect(() => { if (open && collectionId) { // Check if THIS collection is currently being reorganized const isThisActive = isReorganizing && taskType === 'reorganize' && activeCollectionId === collectionId; if (isThisActive) { // Resume: show current progress from worker status setLoading(false); setPreview(null); setJobId('active'); // Marker for active job setStatus({ status: workerStatus.status, total: workerStatus.operation_total, completed: workerStatus.operation_processed, percent: workerStatus.operation_percent, }); setPolling(true); } else if (!polling && !jobId) { // Normal: fetch preview (only if not already polling/resuming) setLoading(true); setError(null); setPreview(null); setJobId(null); setStatus(null); previewCollectionReorganization(collectionId) .then(data => { setPreview(data); setLoading(false); }) .catch(err => { setError(err.message); setLoading(false); }); } } }, [open, collectionId, isReorganizing, taskType, activeCollectionId]); // Poll for status when we have a job ID // Uses setTimeout instead of setInterval to wait for each request to complete // before scheduling the next one, preventing request pile-up on slow connections useEffect(() => { if (!jobId || !polling) return; let timeoutId = null; let cancelled = false; const pollStatus = async () => { if (cancelled) return; try { const statusData = await getReorganizationStatus(collectionId, jobId); if (cancelled) return; setStatus(statusData); if (statusData.status === 'complete') { setPolling(false); if (onComplete) { onComplete(); } } else if (statusData.status === 'error' || statusData.error) { setPolling(false); setError(statusData.error || 'Reorganization failed'); } else { // Schedule next poll after this one completes timeoutId = setTimeout(pollStatus, 1000); } } catch (err) { console.error('Failed to get reorganization status', err); if (!cancelled) { // Retry after delay even on error timeoutId = setTimeout(pollStatus, 1000); } } }; // Start polling immediately pollStatus(); return () => { cancelled = true; if (timeoutId) { clearTimeout(timeoutId); } }; }, [jobId, polling, collectionId, onComplete]); const handleClose = useCallback(() => { // Allow closing - user can reopen to resume if needed setPreview(null); setError(null); setJobId(null); setStatus(null); setPolling(false); onClose(); }, [onClose]); const handleReorganize = useCallback(async () => { try { setError(null); const result = await executeCollectionReorganization(collectionId); if (result.job_id) { setJobId(result.job_id); setPolling(true); setStatus({status: 'pending', total: 0, completed: 0, percent: 0}); } else { // No files needed reorganization if (onComplete) { onComplete(); } handleClose(); } } catch (err) { setError(err.message); } }, [collectionId, onComplete, handleClose]); const renderPreview = () => { if (!preview) return null; return ( {/* Warning when collection appears organized but has files to move */} {preview.files_needing_move > 0 && !needsReorganization && ( Collection Appears Organized

This collection's format matches the current configuration, but {' '}{preview.files_needing_move} files don't match the expected layout. This can happen if a previous reorganization was interrupted.

)}

{preview.files_needing_move} of {preview.total_files} files will be reorganized.

{preview.current_file_format && (

Current format: {preview.current_file_format}

)}

New format:

{preview.sample_moves && preview.sample_moves.length > 0 && ( Sample moves: Current Path New Path {preview.sample_moves.map((move, idx) => ( {move.old_path} {move.new_path} ))}
{preview.files_needing_move > preview.sample_moves.length && (

...and {preview.files_needing_move - preview.sample_moves.length} more files

)}
)} {preview.files_needing_move === 0 && ( No Files Need Reorganization

All files are already organized according to the current format.

)}
); }; const renderProgress = () => { if (!status) return null; return (

Status: {status.status}

{status.completed || 0} of {status.total || 0} files processed

{status.status === 'complete' && ( Reorganization Complete

All files have been reorganized successfully.

)}
); }; const isInProgress = polling || (status && status.status !== 'complete' && status.status !== 'error'); const canReorganize = preview && preview.files_needing_move > 0 && !isInProgress && !jobId; return ( Reorganize Files: {collectionName} {loading && ( Loading Preview Analyzing files... )} {error && ( Error

{error}

)} {!loading && !jobId && renderPreview()} {jobId && renderProgress()}
{canReorganize && ( Reorganize Files )}
); }