mirror of
https://github.com/kilyabin/psysonic.git
synced 2026-07-22 14:35:41 +00:00
7a7a9f5e6b
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.
65 lines
2.2 KiB
TypeScript
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 };
|
|
}
|