Files
Psychotoxical-psysonic/src/hooks/useDeviceSyncDeviceScan.ts
T
Frank Stellmacher 7a7a9f5e6b refactor(utils): group utils/ files into topic folders (Phase L, part 1) (#689)
111 of 122 top-level src/utils/ files move into 16 topic folders (audio,
cache, cover, share, server, playback, playlist, deviceSync, waveform,
mix, format, export, changelog, ui, perf, componentHelpers). True
singletons with no cluster stay at the utils/ root.

Pure file-move: a path-aware codemod rewrote 539 relative-import
specifiers across 275 files; no logic touched. The hot-path coverage
gate list (.github/frontend-hot-path-files.txt) is updated to the new
paths for the 11 gated utils files — a mechanical consequence of the
move, not a CI change. tsc is green.
2026-05-14 14:27:44 +02:00

65 lines
2.2 KiB
TypeScript

import { useCallback, useEffect, useRef } from 'react';
import { invoke } from '@tauri-apps/api/core';
import type { TFunction } from 'i18next';
import { useDeviceSyncStore, type DeviceSyncSource } from '../store/deviceSyncStore';
import { showToast } from '../utils/ui/toast';
export interface DeviceSyncDeviceScanResult {
scanDevice: () => Promise<void>;
}
export function useDeviceSyncDeviceScan(
targetDir: string | null,
sourcesLength: number,
driveDetected: boolean,
t: TFunction,
): DeviceSyncDeviceScanResult {
const setDeviceFilePaths = useDeviceSyncStore.getState().setDeviceFilePaths;
const setScanning = useDeviceSyncStore.getState().setScanning;
const scanDevice = useCallback(async () => {
if (!targetDir || sourcesLength === 0) {
setDeviceFilePaths([]);
return;
}
setScanning(true);
try {
const files = await invoke<string[]>('list_device_dir_files', { dir: targetDir });
setDeviceFilePaths(files);
} catch {
setDeviceFilePaths([]);
} finally {
setScanning(false);
}
}, [targetDir, sourcesLength, setDeviceFilePaths, setScanning]);
// Scan device on mount and when targetDir changes
useEffect(() => { scanDevice(); }, [scanDevice]);
// Auto-import manifest when page loads and drive is already connected
const manifestImportedRef = useRef(false);
useEffect(() => {
if (!targetDir || !driveDetected || manifestImportedRef.current) return;
manifestImportedRef.current = true;
invoke<{ version: number; sources: DeviceSyncSource[] } | null>(
'read_device_manifest', { destDir: targetDir }
).then(manifest => {
if (manifest?.sources?.length) {
useDeviceSyncStore.getState().clearSources();
manifest.sources.forEach(s => useDeviceSyncStore.getState().addSource(s));
showToast(t('deviceSync.manifestImported', { count: manifest.sources.length }), 4000, 'info');
}
}).catch(() => {});
}, [targetDir, driveDetected, t]);
// Clear device file list and reset import flag when stick is unplugged
useEffect(() => {
if (!driveDetected) {
setDeviceFilePaths([]);
manifestImportedRef.current = false;
}
}, [driveDetected, setDeviceFilePaths]);
return { scanDevice };
}