wrolpi/app/src/contexts/FileWorkerStatusContext.js
Roland 74cb5171da
FileGroups in Collections can be reorganized in the Collection's directory.
* Batch reorganization is supported for both Domain and Channel Collections.
* Batch reorganization happens when any Collection does not match the respective file format. For example, a Channel Collection file format that does not match the `yt_dlp_options.file_name_format` will show `needs_reorganization`.
* Individual Collections can always be reorganized. The reorganization is a little more aggressive. Useful for when some files may have been missed in the batch reorganization.
2026-01-31 18:08:41 -07:00

67 lines
2.1 KiB
JavaScript

import React, {createContext, useContext, useState, useEffect, useCallback, useRef} from 'react';
import {fetchFilesProgress} from '../api';
const FileWorkerStatusContext = createContext(null);
export function FileWorkerStatusProvider({children, pollInterval = 3000}) {
const [status, setStatus] = useState(null);
const [error, setError] = useState(null);
const requestedInterval = useRef(pollInterval);
const refresh = useCallback(async () => {
try {
const data = await fetchFilesProgress();
setStatus(data);
setError(null);
} catch (err) {
setError(err);
}
}, []);
// Allow components to request faster polling temporarily
const setFastPolling = useCallback((fast) => {
requestedInterval.current = fast ? 500 : pollInterval;
}, [pollInterval]);
useEffect(() => {
let timeoutId;
const poll = async () => {
await refresh();
timeoutId = setTimeout(poll, requestedInterval.current);
};
poll();
return () => clearTimeout(timeoutId);
}, [refresh]);
return (
<FileWorkerStatusContext.Provider value={{status, error, refresh, setFastPolling}}>
{children}
</FileWorkerStatusContext.Provider>
);
}
export function useFileWorkerStatus() {
const context = useContext(FileWorkerStatusContext);
if (!context) {
throw new Error('useFileWorkerStatus must be used within FileWorkerStatusProvider');
}
return context;
}
// Convenience hook for reorganization-specific status
export function useReorganizationStatus() {
const {status: progress, refresh} = useFileWorkerStatus();
const isReorganizing = progress?.status === 'reorganizing' ||
progress?.status === 'batch_reorganizing';
return {
isReorganizing,
taskType: progress?.task_type,
collectionId: progress?.collection_id,
collectionKind: progress?.collection_kind,
batchStatus: progress?.batch_status,
workerStatus: progress,
refresh,
};
}