import React, {useCallback, useEffect, useState} from 'react'; import {Link} from 'react-router'; import {Button, Icon, Label, Loading, Message, Modal, Progress, Stack, Table} from '../ui'; import {APIButton} from '../Common'; import { executeBatchReorganization, getBatchReorganizationStatus, previewBatchReorganization } from '../../api'; import {useReorganizationStatus, useFileWorkerStatus} from '../../contexts/FileWorkerStatusContext'; /** * Modal component for previewing and executing batch collection file reorganization. * * Shows a list of collections that need reorganization, allows user to execute * batch reorganization, and tracks progress with dual progress bars (overall and per-collection). * * @param {boolean} open - Whether the modal is open * @param {Function} onClose - Callback when modal is closed * @param {string} kind - 'channel' or 'domain' * @param {Function} onComplete - Optional callback when reorganization completes */ export function BatchReorganizeModal({ open, onClose, kind, onComplete, }) { const [loading, setLoading] = useState(false); const [preview, setPreview] = useState(null); const [error, setError] = useState(null); const [batchJobId, setBatchJobId] = useState(null); const [status, setStatus] = useState(null); const [polling, setPolling] = useState(false); const kindLabel = kind === 'channel' ? 'Channel' : 'Domain'; const kindLabelPlural = kind === 'channel' ? 'Channels' : 'Domains'; // Check if batch reorganization is currently active const { isReorganizing, taskType, collectionKind, batchStatus: workerBatchStatus, workerStatus, } = useReorganizationStatus(); const {setFastPolling} = useFileWorkerStatus(); // Enable fast polling when modal is open useEffect(() => { if (open) { setFastPolling(true); } return () => setFastPolling(false); }, [open, setFastPolling]); // Load preview when modal opens, or resume if already reorganizing useEffect(() => { if (open && kind) { // Check if batch_status has an active job for this kind // This is the primary indicator since worker status can briefly show // 'reorganizing'/'reorganize' during individual collection processing const hasActiveBatchJob = workerBatchStatus?.batch_job_id && collectionKind === kind; // Worker is actively doing something (taskType is set) // After batch completes, taskType becomes null, so this distinguishes // "batch in progress" from "batch completed but batch_job_id still cached" const workerIsActive = taskType != null; // Use either the standard detection OR the batch_status fallback (only when worker is active) const isThisActive = (isReorganizing && taskType === 'batch_reorganize' && collectionKind === kind) || (hasActiveBatchJob && workerIsActive); if (isThisActive && workerBatchStatus) { // Resume: start polling API immediately for accurate progress // Don't use workerBatchStatus.current_collection directly as it may have stale // percent/total/completed values. The batch status API returns fresh values // by copying operation_percent into current_collection.percent. setLoading(false); setPreview(null); setBatchJobId(workerBatchStatus.batch_job_id || null); // Don't setStatus from workerBatchStatus - let the first poll populate it // with accurate values from the batch status API setPolling(true); } else if (!polling && !batchJobId) { // Normal: fetch preview (only if not already polling/resuming) setLoading(true); setError(null); setPreview(null); setBatchJobId(null); setStatus(null); previewBatchReorganization(kind) .then(data => { setPreview(data); setLoading(false); }) .catch(err => { setError(err.message); setLoading(false); }); } } }, [open, kind, isReorganizing, taskType, collectionKind]); // Poll for status when we have a batch job ID useEffect(() => { if (!batchJobId || !polling) { return; } let timeoutId = null; let cancelled = false; const pollStatus = async () => { if (cancelled) { return; } try { const statusData = await getBatchReorganizationStatus(batchJobId); if (cancelled) return; setStatus(statusData); if (statusData.status === 'complete') { setPolling(false); if (onComplete) { onComplete(); } } else if (statusData.status === 'failed' || statusData.error) { setPolling(false); setError(statusData.error || 'Batch reorganization failed'); } else { // Schedule next poll after this one completes timeoutId = setTimeout(pollStatus, 500); } } catch (err) { console.error('Failed to get batch 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); } }; }, [batchJobId, polling, onComplete]); const handleClose = useCallback(() => { // Allow closing - user can reopen to resume if needed setPreview(null); setError(null); setBatchJobId(null); setStatus(null); setPolling(false); onClose(); }, [onClose]); const handleReorganize = useCallback(async () => { try { setError(null); const result = await executeBatchReorganization(kind); if (result.batch_job_id) { setBatchJobId(result.batch_job_id); setPolling(true); setStatus({ status: 'pending', total_collections: result.collection_count, completed_collections: 0, current_collection: null, overall_percent: 0, failed_collection: null, error: null, }); } else { // No collections needed reorganization if (onComplete) { onComplete(); } handleClose(); } } catch (err) { setError(err.message); } }, [kind, onComplete, handleClose]); const renderPreview = () => { if (!preview) return null; return (

{preview.total_collections} {kindLabelPlural.toLowerCase()} need reorganization, with {preview.total_files_needing_move} total files to move.

New format:

{preview.collections && preview.collections.length > 0 && (
{kindLabelPlural} to reorganize: {kindLabel} Files to Move Sample {preview.collections.slice(0, 10).map((collection, idx) => ( {collection.collection_name} {collection.total_files} {collection.sample_move ? ( <> {collection.sample_move.old_path}
{collection.sample_move.new_path} ) : '-'}
))}
{preview.collections.length > 10 && (

...and {preview.collections.length - 10} more {kindLabelPlural.toLowerCase()}

)}
)} {preview.total_collections === 0 && (

All {kindLabelPlural.toLowerCase()} are already organized according to the current format.

)}
); }; const renderProgress = () => { if (!status) return null; const currentCollection = status.current_collection; const overallColor = status.status === 'complete' ? 'green' : status.status === 'failed' ? 'red' : 'blue'; return (

Overall Status: {status.status}

Overall Progress:

{status.completed_collections || 0} of {status.total_collections || 0} {kindLabelPlural.toLowerCase()} completed

{currentCollection && (
Currently Processing: {currentCollection.name}

{currentCollection.completed || 0} of {currentCollection.total || 0} files

)} {status.status === 'complete' && (

All {kindLabelPlural.toLowerCase()} have been reorganized successfully.

)} {status.status === 'failed' && status.failed_collection && (

Failed on {kindLabel.toLowerCase()}: {status.failed_collection.name}

{status.error && (

Error: {status.error}

)}
)}
); }; const isInProgress = polling || (status && status.status !== 'complete' && status.status !== 'failed'); const canReorganize = preview && preview.total_collections > 0 && !isInProgress && !batchJobId; return ( Reorganize All {kindLabelPlural} {loading && Analyzing {kindLabelPlural.toLowerCase()}...} {error && !status?.failed_collection && (

{error}

)} {!loading && !batchJobId && renderPreview()} {batchJobId && renderProgress()}
{canReorganize && ( Reorganize All {kindLabelPlural} )}
); }