fix(MediaManager): refactor folder configuration loading to improve clarity and performance
This commit is contained in:
parent
afc15134f5
commit
3d69970ea5
|
|
@ -463,12 +463,28 @@ export default function MediaManager({ initialPath, rootPath, title, config, set
|
|||
|
||||
// ── Directory loading ────────────────────────────────────────────────────
|
||||
|
||||
const [folderConfig, setFolderConfig] = useState<Record<string, string> | null>(null);
|
||||
|
||||
const load = useCallback(async (p: string) => {
|
||||
setLoading(true);
|
||||
setError(null);
|
||||
setSelected(new Set());
|
||||
setFolderConfig(null);
|
||||
try {
|
||||
setEntries(await listDirectory(p));
|
||||
const entries = await listDirectory(p);
|
||||
setEntries(entries);
|
||||
try {
|
||||
const blob = await getFileContents(joinPath(p, '.config'));
|
||||
const cfg: Record<string, string> = {};
|
||||
for (const line of (await blob.text()).split('\n')) {
|
||||
const t = line.trim();
|
||||
if (!t || t.startsWith('#')) continue;
|
||||
const eq = t.indexOf('=');
|
||||
if (eq < 0) continue;
|
||||
cfg[t.slice(0, eq).trim()] = t.slice(eq + 1).trim();
|
||||
}
|
||||
setFolderConfig(cfg);
|
||||
} catch { /* no .config or unreadable — folderConfig stays null */ }
|
||||
} catch (e: any) {
|
||||
setError(e?.message ?? 'Failed to load directory');
|
||||
setEntries([]);
|
||||
|
|
@ -486,30 +502,6 @@ export default function MediaManager({ initialPath, rootPath, title, config, set
|
|||
useEffect(() => { localStorage.setItem('fileManager.sortKey', sortKey); }, [sortKey]);
|
||||
useEffect(() => { localStorage.setItem('fileManager.sortAsc', String(sortAsc)); }, [sortAsc]);
|
||||
|
||||
// ── Folder config (.config) ──────────────────────────────────────────────
|
||||
|
||||
const [folderConfig, setFolderConfig] = useState<Record<string, string> | null>(null);
|
||||
|
||||
useEffect(() => {
|
||||
let cancelled = false;
|
||||
setFolderConfig(null);
|
||||
getFileContents(joinPath(path, '.config'))
|
||||
.then(async blob => {
|
||||
if (cancelled) return;
|
||||
const cfg: Record<string, string> = {};
|
||||
for (const line of (await blob.text()).split('\n')) {
|
||||
const t = line.trim();
|
||||
if (!t || t.startsWith('#')) continue;
|
||||
const eq = t.indexOf('=');
|
||||
if (eq < 0) continue;
|
||||
cfg[t.slice(0, eq).trim()] = t.slice(eq + 1).trim();
|
||||
}
|
||||
setFolderConfig(cfg);
|
||||
})
|
||||
.catch(() => { if (!cancelled) setFolderConfig(null); });
|
||||
return () => { cancelled = true; };
|
||||
}, [path]);
|
||||
|
||||
const navigateTo = (p: string) => {
|
||||
let norm = normalizePath(p);
|
||||
if (rootPath && !norm.startsWith(rootPath)) norm = rootPath;
|
||||
|
|
|
|||
Loading…
Reference in New Issue
Block a user