feat(SearchLocal, locate-db, webdav): implement scan cancellation with AbortController and enhance progress tracking

This commit is contained in:
Jaime Idolpx 2026-06-17 12:42:42 -04:00
parent ec61853957
commit 4b901c96b5
3 changed files with 34 additions and 5 deletions

View File

@ -167,6 +167,7 @@ function FilterChips({
export default function SearchLocal({ config, setConfig, onClose, onOpenFolder }: SearchLocalProps) { export default function SearchLocal({ config, setConfig, onClose, onOpenFolder }: SearchLocalProps) {
const scrollRef = useRef<HTMLDivElement>(null); const scrollRef = useRef<HTMLDivElement>(null);
const scanAbortRef = useRef<AbortController | null>(null);
const [query, setQuery] = useState(() => _store.query); const [query, setQuery] = useState(() => _store.query);
const [isSearching, setIsSearching] = useState(false); const [isSearching, setIsSearching] = useState(false);
const [isScanning, setIsScanning] = useState(false); const [isScanning, setIsScanning] = useState(false);
@ -214,6 +215,10 @@ export default function SearchLocal({ config, setConfig, onClose, onOpenFolder }
} }
}, []); }, []);
useEffect(() => {
return () => { scanAbortRef.current?.abort(); };
}, []);
const mountedPaths = useMemo(() => { const mountedPaths = useMemo(() => {
const paths = new Set<string>(); const paths = new Set<string>();
for (const d of Object.values(config?.devices?.iec ?? {})) { for (const d of Object.values(config?.devices?.iec ?? {})) {
@ -267,24 +272,32 @@ export default function SearchLocal({ config, setConfig, onClose, onOpenFolder }
}; };
const handleScan = async () => { const handleScan = async () => {
const controller = new AbortController();
scanAbortRef.current = controller;
setIsScanning(true); setIsScanning(true);
setScanPhase('scanning'); setScanPhase('scanning');
setScanValue(0); setScanValue(0);
try { try {
const { count, bytes } = await buildLocateDb((phase, value, path) => const { count, bytes } = await buildLocateDb(
flushSync(() => { setScanPhase(phase); setScanValue(value); if (path !== undefined) setScanPath(path); }) (phase, value, path) =>
flushSync(() => { setScanPhase(phase); setScanValue(value); if (path !== undefined) setScanPath(path); }),
controller.signal,
); );
toast.success(`Indexed ${count.toLocaleString()} items · ${humanFileSize(bytes)}`); toast.success(`Indexed ${count.toLocaleString()} items · ${humanFileSize(bytes)}`);
setDbPhase('ready'); setDbPhase('ready');
} catch (e: any) { } catch (e: any) {
toast.error(`Scan failed: ${e?.message ?? e}`); if (e?.name === 'AbortError') toast.info('Scan stopped.');
else toast.error(`Scan failed: ${e?.message ?? e}`);
} finally { } finally {
scanAbortRef.current = null;
setIsScanning(false); setIsScanning(false);
setScanPhase(null); setScanPhase(null);
setScanPath(null); setScanPath(null);
} }
}; };
const handleStopScan = () => scanAbortRef.current?.abort();
const handleRefreshDb = () => { const handleRefreshDb = () => {
resetLocateDb(); resetLocateDb();
setDbPhase('idle'); setDbPhase('idle');
@ -459,6 +472,12 @@ export default function SearchLocal({ config, setConfig, onClose, onOpenFolder }
) : ( ) : (
<p className="text-xs text-neutral-400">Scanning /sd recursively</p> <p className="text-xs text-neutral-400">Scanning /sd recursively</p>
)} )}
<button
onClick={handleStopScan}
className="mt-2 px-4 py-1.5 text-xs font-medium rounded-lg border border-neutral-300 text-neutral-600 hover:bg-neutral-100 transition-colors"
>
Stop
</button>
</div> </div>
)} )}

View File

@ -175,14 +175,18 @@ export type ScanPhase = 'scanning' | 'building' | 'saving' | 'compressing';
*/ */
export async function buildLocateDb( export async function buildLocateDb(
onProgress?: (phase: ScanPhase, value: number, path?: string) => void, onProgress?: (phase: ScanPhase, value: number, path?: string) => void,
signal?: AbortSignal,
): Promise<{ count: number; bytes: number }> { ): Promise<{ count: number; bytes: number }> {
signal?.throwIfAborted();
const sqlite3 = await getSqlite3(); const sqlite3 = await getSqlite3();
// ── 1. Recursive PROPFIND on /sd ──────────────────────────────────────── // ── 1. Recursive PROPFIND on /sd ────────────────────────────────────────
const entries = await listDirectory( const entries = await listDirectory(
'/sd', true, '/sd', true,
bytes => onProgress?.('scanning', bytes), bytes => onProgress?.('scanning', bytes),
signal,
); );
signal?.throwIfAborted();
// ── 2. Build in-memory DB ─────────────────────────────────────────────── // ── 2. Build in-memory DB ───────────────────────────────────────────────
const db = new sqlite3.oo1.DB(':memory:', 'ct'); const db = new sqlite3.oo1.DB(':memory:', 'ct');
@ -207,12 +211,16 @@ export async function buildLocateDb(
e.lastModified ? Math.floor(e.lastModified.getTime() / 1000) : 0, e.lastModified ? Math.floor(e.lastModified.getTime() / 1000) : 0,
e.type === 'folder' ? 1 : 0, e.type === 'folder' ? 1 : 0,
]).stepReset(); ]).stepReset();
if (i % SCAN_PROGRESS_INTERVAL === 0) onProgress?.('building', i, e.path); if (i % SCAN_PROGRESS_INTERVAL === 0) {
signal?.throwIfAborted();
onProgress?.('building', i, e.path);
}
} }
} finally { } finally {
stmt.finalize(); stmt.finalize();
} }
db.exec('COMMIT'); db.exec('COMMIT');
signal?.throwIfAborted();
onProgress?.('building', entries.length); onProgress?.('building', entries.length);
// ── 3. Serialize to bytes ──────────────────────────────────────────────── // ── 3. Serialize to bytes ────────────────────────────────────────────────

View File

@ -175,6 +175,7 @@ export async function listDirectory(
path: string, path: string,
recursive = false, recursive = false,
onProgress?: (bytes: number) => void, onProgress?: (bytes: number) => void,
signal?: AbortSignal,
): Promise<EntryInfo[]> { ): Promise<EntryInfo[]> {
const manager = getWebDAVClient(); const manager = getWebDAVClient();
const base = manager.client.baseUrl; const base = manager.client.baseUrl;
@ -190,6 +191,7 @@ export async function listDirectory(
method: 'PROPFIND', method: 'PROPFIND',
headers: { 'Depth': String(depth), 'Content-Type': 'text/xml; charset=utf-8' }, headers: { 'Depth': String(depth), 'Content-Type': 'text/xml; charset=utf-8' },
body: PROPFIND_LIST_BODY, body: PROPFIND_LIST_BODY,
signal,
}); });
if (!response.body) throw new Error('No response body'); if (!response.body) throw new Error('No response body');
const reader = response.body.getReader(); const reader = response.body.getReader();