2026-07-04 20:39:30 -05:00
|
|
|
import { useState, useEffect, useRef } from 'react';
|
2026-06-14 15:15:31 -05:00
|
|
|
import { Tag } from 'lucide-react';
|
2026-07-04 20:39:30 -05:00
|
|
|
import type { Category } from '@/types';
|
2026-06-14 15:15:31 -05:00
|
|
|
|
|
|
|
|
// ── Category picker dropdown ─────────────────────────────────────────────────
|
|
|
|
|
|
2026-07-04 20:39:30 -05:00
|
|
|
interface CategoryPickerProps {
|
|
|
|
|
categories: Category[];
|
|
|
|
|
current?: number | string | null;
|
|
|
|
|
currentLabel?: string | null;
|
|
|
|
|
onSelect: (id: number | string | null, replace: boolean) => void;
|
|
|
|
|
}
|
|
|
|
|
|
2026-07-06 14:23:53 -05:00
|
|
|
export function CategoryPicker({
|
|
|
|
|
categories,
|
|
|
|
|
current,
|
|
|
|
|
currentLabel,
|
|
|
|
|
onSelect,
|
|
|
|
|
}: CategoryPickerProps) {
|
2026-06-14 15:15:31 -05:00
|
|
|
const [open, setOpen] = useState(false);
|
2026-07-04 20:39:30 -05:00
|
|
|
const ref = useRef<HTMLDivElement>(null);
|
2026-06-14 15:15:31 -05:00
|
|
|
|
|
|
|
|
useEffect(() => {
|
|
|
|
|
if (!open) return;
|
2026-07-06 14:23:53 -05:00
|
|
|
const close = (e: MouseEvent) => {
|
|
|
|
|
if (ref.current && !ref.current.contains(e.target as Node)) setOpen(false);
|
|
|
|
|
};
|
2026-06-14 15:15:31 -05:00
|
|
|
document.addEventListener('mousedown', close);
|
|
|
|
|
return () => document.removeEventListener('mousedown', close);
|
|
|
|
|
}, [open]);
|
|
|
|
|
|
2026-07-06 14:23:53 -05:00
|
|
|
const currentCat = categories.find((c) => c.id === current);
|
2026-06-14 15:15:31 -05:00
|
|
|
|
|
|
|
|
return (
|
|
|
|
|
<div ref={ref} className="relative">
|
|
|
|
|
<button
|
|
|
|
|
type="button"
|
2026-07-06 14:23:53 -05:00
|
|
|
onClick={() => setOpen((v) => !v)}
|
2026-06-14 15:15:31 -05:00
|
|
|
className="flex items-center gap-1.5 rounded-md border border-border/60 bg-background px-2 py-1 text-xs text-muted-foreground hover:border-primary/40 hover:text-foreground transition-colors"
|
|
|
|
|
>
|
|
|
|
|
<Tag className="h-3 w-3 shrink-0" />
|
2026-07-06 14:23:53 -05:00
|
|
|
<span className="max-w-[100px] truncate">
|
|
|
|
|
{currentCat?.name ?? currentLabel ?? 'Uncategorized'}
|
|
|
|
|
</span>
|
2026-06-14 15:15:31 -05:00
|
|
|
</button>
|
|
|
|
|
|
|
|
|
|
{open && (
|
|
|
|
|
<div className="absolute right-0 top-full z-50 mt-1 w-48 rounded-lg border border-border/60 bg-popover shadow-lg overflow-hidden">
|
|
|
|
|
<button
|
|
|
|
|
type="button"
|
2026-07-06 14:23:53 -05:00
|
|
|
onMouseDown={(e) => {
|
|
|
|
|
e.preventDefault();
|
|
|
|
|
onSelect(null, false);
|
|
|
|
|
setOpen(false);
|
|
|
|
|
}}
|
2026-06-14 15:15:31 -05:00
|
|
|
className="w-full px-3 py-2 text-left text-xs text-muted-foreground hover:bg-muted/50 transition-colors"
|
|
|
|
|
>
|
|
|
|
|
— Uncategorized
|
|
|
|
|
</button>
|
|
|
|
|
{categories.length === 0 ? (
|
|
|
|
|
<p className="px-3 py-2 text-xs text-muted-foreground italic">
|
|
|
|
|
No spending categories. Enable some in Categories.
|
|
|
|
|
</p>
|
2026-07-06 14:23:53 -05:00
|
|
|
) : (
|
|
|
|
|
categories.map((cat) => (
|
|
|
|
|
<button
|
|
|
|
|
key={cat.id}
|
|
|
|
|
type="button"
|
|
|
|
|
onMouseDown={(e) => {
|
|
|
|
|
e.preventDefault();
|
|
|
|
|
onSelect(cat.id, false);
|
|
|
|
|
setOpen(false);
|
|
|
|
|
}}
|
|
|
|
|
className={`w-full px-3 py-2 text-left text-xs hover:bg-muted/50 transition-colors ${cat.id === current ? 'text-primary font-medium' : ''}`}
|
|
|
|
|
>
|
|
|
|
|
{cat.name}
|
|
|
|
|
</button>
|
|
|
|
|
))
|
|
|
|
|
)}
|
2026-06-14 15:15:31 -05:00
|
|
|
</div>
|
|
|
|
|
)}
|
|
|
|
|
</div>
|
|
|
|
|
);
|
|
|
|
|
}
|