File size: 4,915 Bytes
f59fbe2 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 | import { Fragment, useEffect, useRef } from "react";
import { IconFolder, IconFile, IconPhoto } from "@tabler/icons-react";
import { cn } from "@/lib/utils";
import { mentionMatchSpans, type MentionMatchSpan } from "@/lib/mention-match";
export interface FileItem {
name: string;
path: string;
isDirectory: boolean;
matchPositions?: number[];
}
interface FilePickerMenuProps {
items: FileItem[];
selectedIndex: number;
isLoading?: boolean;
isStale?: boolean;
showMediaOption?: boolean;
onSelectMedia?: () => void;
onSelectItem: (item: FileItem) => void;
onHover: (index: number) => void;
}
function parentDir(path: string): string {
const trimmed = path.endsWith("/") ? path.slice(0, -1) : path;
const idx = trimmed.lastIndexOf("/");
return idx === -1 ? "" : trimmed.slice(0, idx);
}
function nameSpans(item: FileItem): MentionMatchSpan[] {
const path = item.path.endsWith("/") ? item.path.slice(0, -1) : item.path;
return mentionMatchSpans(item.name, item.matchPositions, Math.max(0, path.length - item.name.length));
}
function dirSpans(item: FileItem): MentionMatchSpan[] {
return mentionMatchSpans(parentDir(item.path), item.matchPositions, 0);
}
export function FilePickerMenu({
items,
selectedIndex,
isLoading,
isStale = false,
showMediaOption = true,
onSelectMedia,
onSelectItem,
onHover,
}: FilePickerMenuProps) {
const selectedRef = useRef<HTMLButtonElement>(null);
const hoverSelectionRef = useRef<number | null>(null);
useEffect(() => {
if (hoverSelectionRef.current === selectedIndex) {
hoverSelectionRef.current = null;
return;
}
hoverSelectionRef.current = null;
selectedRef.current?.scrollIntoView({ block: "nearest" });
}, [selectedIndex]);
const handleHover = (index: number) => {
if (isStale) return;
hoverSelectionRef.current = index;
onHover(index);
};
const headerCount = showMediaOption ? 1 : 0;
return (
<div className="rounded-md border bg-popover shadow-md overflow-hidden">
{showMediaOption && onSelectMedia && (
<button
ref={selectedIndex === 0 ? selectedRef : null}
onMouseDown={(e) => e.preventDefault()}
onClick={onSelectMedia}
onMouseMove={() => handleHover(0)}
className={cn("w-full px-2 py-1.5 text-left flex items-center gap-2 border-b border-border", selectedIndex === 0 ? "bg-accent" : "hover:bg-accent/50")}
>
<IconPhoto className="size-3.5 text-muted-foreground" />
<span className="text-xs">Select images or videos…</span>
</button>
)}
<div className={cn("max-h-64 overflow-y-auto", isStale && "opacity-60")}>
{isLoading ? (
<div className="px-2 py-4 text-center text-xs text-muted-foreground">Loading…</div>
) : items.length === 0 ? (
<div className="px-2 py-4 text-center text-xs text-muted-foreground">No files found</div>
) : (
items.map((item, idx) => {
const itemIndex = idx + headerCount;
const dir = parentDir(item.path);
return (
<button
key={item.path}
ref={itemIndex === selectedIndex ? selectedRef : null}
onMouseDown={(e) => e.preventDefault()}
onClick={() => onSelectItem(item)}
onMouseMove={() => handleHover(itemIndex)}
className={cn("w-full px-2 py-1.5 text-left flex items-center justify-between gap-3", itemIndex === selectedIndex ? "bg-accent" : "hover:bg-accent/50")}
>
<span className="flex items-center gap-1.5 text-xs shrink-0">
{item.isDirectory ? <IconFolder className="size-3 text-muted-foreground" /> : <IconFile className="size-3 text-muted-foreground" />}
<span className={cn(item.isDirectory && "font-medium")}>
{nameSpans(item).map((span, spanIdx) =>
span.hit ? (
<span key={spanIdx} className="text-foreground font-semibold">{span.text}</span>
) : (
<Fragment key={spanIdx}>{span.text}</Fragment>
),
)}
{item.isDirectory && "/"}
</span>
</span>
{dir && (
<span className="text-[10px] text-muted-foreground truncate max-w-32">
{dirSpans(item).map((span, spanIdx) =>
span.hit ? (
<span key={spanIdx} className="text-foreground">{span.text}</span>
) : (
<Fragment key={spanIdx}>{span.text}</Fragment>
),
)}
</span>
)}
</button>
);
})
)}
</div>
</div>
);
}
|