Files
Kaname/src/components/tag-picker.tsx
T
Pascal LinxweilerandClaude Opus 5.5 5becfc47a0
Release / release (push) Successful in 4m33s
Release 0.16.0: new look, search, remove files, easier deploy
- Dot-matrix "Creation of Adam" page banners behind a floating menu,
  glass cards and tables, library header with live stats, table toolbar
  with pagination, Cmd/Ctrl+K search across models, endpoints, runs,
  tags, settings and actions.
- Remove single files from a model (selection + disk, via a worker job).
- Containers start as root to prepare folders and drop to PUID:PGID;
  AUTH_SECRET is generated when unset; sandbox runs as nobody;
  KANAME_IMAGE and image docs in the README.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-30 14:06:46 +01:00

132 lines
4.1 KiB
TypeScript

"use client";
import { ChevronDownIcon, PlusIcon, SettingsIcon } from "lucide-react";
import Link from "next/link";
import { useEffect, useRef, useState } from "react";
import { TagDot } from "@/components/tag-badge";
import {
DropdownMenu,
DropdownMenuContent,
DropdownMenuItem,
DropdownMenuSeparator,
DropdownMenuTrigger,
} from "@/components/ui/dropdown-menu";
import { Input } from "@/components/ui/input";
import type { Tag } from "@/lib/tags";
import { cn } from "@/lib/utils";
/**
* "Add tag" dropdown: the first entry creates a new tag (inline name field), below it the existing
* tags that aren't picked yet. Calls `onPick` with the tag name.
*/
export function TagPicker({
exclude,
onPick,
disabled,
className,
}: {
/** Tag names already applied (case-insensitive), hidden from the list. */
exclude: string[];
/** Picked tag name; `tag` is set when an existing tag was chosen. */
onPick: (name: string, tag?: Tag) => void | Promise<void>;
disabled?: boolean;
className?: string;
}) {
const [known, setKnown] = useState<Tag[]>([]);
const [creating, setCreating] = useState(false);
const [name, setName] = useState("");
const inputRef = useRef<HTMLInputElement>(null);
const cancelled = useRef(false);
function load() {
fetch("/api/tags")
.then((r) => (r.ok ? r.json() : []))
.then((all: Tag[]) => setKnown(all))
.catch(() => {});
}
useEffect(load, []);
useEffect(() => {
if (!creating) return;
cancelled.current = false;
inputRef.current?.focus();
}, [creating]);
function finishCreating() {
setCreating(false);
setName("");
}
async function submit() {
const value = name.trim();
const existing = known.find((t) => t.name.toLowerCase() === value.toLowerCase());
if (value) await onPick(existing?.name ?? value, existing);
finishCreating();
}
if (creating) {
return (
<form
className={className}
onSubmit={(e) => {
e.preventDefault();
inputRef.current?.blur(); // blur submits, so Enter and click-away can't add twice
}}
>
<Input
ref={inputRef}
value={name}
onChange={(e) => setName(e.target.value)}
onBlur={() => (name.trim() && !cancelled.current ? void submit() : finishCreating())}
onKeyDown={(e) => {
if (e.key !== "Escape") return;
cancelled.current = true;
inputRef.current?.blur();
}}
placeholder="New tag name…"
disabled={disabled}
maxLength={40}
className="h-6 w-40 text-xs"
aria-label="New tag name"
/>
</form>
);
}
const hidden = new Set(exclude.map((n) => n.toLowerCase()));
const available = known.filter((t) => !hidden.has(t.name.toLowerCase()));
return (
<DropdownMenu onOpenChange={(open) => open && load()}>
<DropdownMenuTrigger
disabled={disabled}
className={cn(
"inline-flex h-6 items-center gap-1 rounded-full border border-dashed border-foreground/25 bg-background/80 px-2 text-xs text-foreground/85 outline-none backdrop-blur-sm hover:bg-muted hover:text-foreground focus-visible:ring-3 focus-visible:ring-ring/50 disabled:opacity-50",
className,
)}
>
<PlusIcon className="size-3" />
Add tag
<ChevronDownIcon className="size-3 opacity-60" />
</DropdownMenuTrigger>
<DropdownMenuContent className="max-h-72 w-auto min-w-44">
<DropdownMenuItem onClick={() => setCreating(true)}>
<PlusIcon />
Create new tag…
</DropdownMenuItem>
{available.length > 0 && <DropdownMenuSeparator />}
{available.map((t) => (
<DropdownMenuItem key={t.id} onClick={() => void onPick(t.name, t)}>
<TagDot name={t.name} color={t.color} />
{t.name}
</DropdownMenuItem>
))}
<DropdownMenuSeparator />
<DropdownMenuItem render={<Link href="/settings/tags" />} className="text-muted-foreground">
<SettingsIcon />
Manage tags…
</DropdownMenuItem>
</DropdownMenuContent>
</DropdownMenu>
);
}