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

@ -166,7 +166,8 @@ function FilterChips({
}
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 [isSearching, setIsSearching] = 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 paths = new Set<string>();
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 controller = new AbortController();
scanAbortRef.current = controller;
setIsScanning(true);
setScanPhase('scanning');
setScanValue(0);
try {
const { count, bytes } = await buildLocateDb((phase, value, path) =>
flushSync(() => { setScanPhase(phase); setScanValue(value); if (path !== undefined) setScanPath(path); })
const { count, bytes } = await buildLocateDb(
(phase, value, path) =>
flushSync(() => { setScanPhase(phase); setScanValue(value); if (path !== undefined) setScanPath(path); }),
controller.signal,
);
toast.success(`Indexed ${count.toLocaleString()} items · ${humanFileSize(bytes)}`);
setDbPhase('ready');
} 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 {
scanAbortRef.current = null;
setIsScanning(false);
setScanPhase(null);
setScanPath(null);
}
};
const handleStopScan = () => scanAbortRef.current?.abort();
const handleRefreshDb = () => {
resetLocateDb();
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>
)}
<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>
)}

View File

@ -175,14 +175,18 @@ export type ScanPhase = 'scanning' | 'building' | 'saving' | 'compressing';
*/
export async function buildLocateDb(
onProgress?: (phase: ScanPhase, value: number, path?: string) => void,
signal?: AbortSignal,
): Promise<{ count: number; bytes: number }> {
signal?.throwIfAborted();
const sqlite3 = await getSqlite3();
// ── 1. Recursive PROPFIND on /sd ────────────────────────────────────────
const entries = await listDirectory(
'/sd', true,
bytes => onProgress?.('scanning', bytes),
signal,
);
signal?.throwIfAborted();
// ── 2. Build in-memory DB ───────────────────────────────────────────────
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.type === 'folder' ? 1 : 0,
]).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 {
stmt.finalize();
}
db.exec('COMMIT');
signal?.throwIfAborted();
onProgress?.('building', entries.length);
// ── 3. Serialize to bytes ────────────────────────────────────────────────

View File

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