From 3d69970ea5c9a86302cf9493182fc8a03a1c515e Mon Sep 17 00:00:00 2001 From: Jaime Idolpx Date: Fri, 12 Jun 2026 23:02:11 -0400 Subject: [PATCH] fix(MediaManager): refactor folder configuration loading to improve clarity and performance --- src/app/components/MediaManager.tsx | 42 ++++++++++++----------------- 1 file changed, 17 insertions(+), 25 deletions(-) diff --git a/src/app/components/MediaManager.tsx b/src/app/components/MediaManager.tsx index 2e7f8ee..05664d1 100644 --- a/src/app/components/MediaManager.tsx +++ b/src/app/components/MediaManager.tsx @@ -463,12 +463,28 @@ export default function MediaManager({ initialPath, rootPath, title, config, set // ── Directory loading ──────────────────────────────────────────────────── + const [folderConfig, setFolderConfig] = useState | 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 = {}; + 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 | null>(null); - - useEffect(() => { - let cancelled = false; - setFolderConfig(null); - getFileContents(joinPath(path, '.config')) - .then(async blob => { - if (cancelled) return; - const cfg: Record = {}; - 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;