(null);
const requestGeneration = useRef(0);
const load = useCallback(async (path: string) => {
const generation = ++requestGeneration.current;
setLoading(true);
setError(null);
setListing(null);
try {
const nextListing = await listDirectory(path);
if (generation !== requestGeneration.current) return;
setListing(nextListing);
} catch (err) {
if (generation !== requestGeneration.current) return;
setError(err instanceof Error ? err.message : "Failed to load directory");
} finally {
if (generation === requestGeneration.current) setLoading(false);
}
}, []);
useEffect(() => {
// Reset cached listing when the popover closes so the next open reloads
// from `value`. Without this, a user who browsed deep into the tree,
// closed without committing, and reopened would still see the stale
// last-browsed directory instead of their picked folder (or the root).
if (!open) {
requestGeneration.current++;
setListing(null);
setLoading(false);
setError(null);
return;
}
void load(value || "");
return () => {
requestGeneration.current++;
};
}, [open, load, value]);
return { listing, loading, error, load };
}
/** Splits an absolute path into clickable breadcrumb segments. */
function pathSegments(path: string): Array<{ name: string; path: string }> {
if (!path) return [];
const segs: Array<{ name: string; path: string }> = [{ name: "/", path: "/" }];
const driveMatch = /^([A-Za-z]:)[\\/]*(.*)$/.exec(path);
if (driveMatch) {
const driveRoot = `${driveMatch[1]}\\`;
segs.push({ name: driveRoot, path: driveRoot });
appendPathSegments(segs, driveRoot, driveMatch[2], "\\");
return segs;
}
const uncMatch = /^\\\\([^\\/]+)[\\/]([^\\/]+)[\\/]*(.*)$/.exec(path);
if (uncMatch) {
const shareRoot = `\\\\${uncMatch[1]}\\${uncMatch[2]}\\`;
segs.push({ name: shareRoot, path: shareRoot });
appendPathSegments(segs, shareRoot, uncMatch[3], "\\");
return segs;
}
appendPathSegments(segs, "", path, "/");
return segs;
}
function appendPathSegments(
segs: Array<{ name: string; path: string }>,
root: string,
path: string,
separator: "/" | "\\",
) {
const parts = path.split(/[\\/]+/).filter(Boolean);
let acc = root.replace(/[\\/]+$/, "");
for (const part of parts) {
acc += `${separator}${part}`;
segs.push({ name: part, path: acc });
}
}
function Breadcrumb({ path, onNavigate }: { path: string; onNavigate: (p: string) => void }) {
const segs = pathSegments(path);
return (
{segs.length === 0 && (
Loading…
)}
{segs.map((seg, i) => {
const last = i === segs.length - 1;
return (
{i > 0 && (
)}
);
})}
);
}
function Entries({
listing,
loading,
error,
onDescend,
}: {
listing: DirectoryListing | null;
loading: boolean;
error: string | null;
onDescend: (path: string) => void;
}) {
if (loading) return ;
if (error) return ;
if (!listing || listing.entries.length === 0) {
return ;
}
return (
e.stopPropagation()}
>
{listing.entries.map((entry) => (
))}
);
}
function EmptyRow({ text, variant, testId }: { text: string; variant?: "error"; testId?: string }) {
return (
{text}
);
}
function Footer({
choosable,
onUseScratch,
onChoose,
}: {
choosable: boolean;
onUseScratch: () => void;
onChoose: () => void;
}) {
return (
);
}