"use client"; import { useMemo, useState } from "react"; import { Check, ChevronDown, Search } from "lucide-react"; import { Button } from "@/components/ui/button"; import { Dialog, DialogContent, DialogHeader, DialogTitle, } from "@/components/ui/dialog"; import { Input } from "@/components/ui/input"; import { TOPIC_GOAL_LABELS, isPlacementGoal } from "@/lib/types/topic-goal"; import { cn } from "@/lib/utils"; export interface InspirationTopic { id: string; label: string; topicGoal: string; } interface TopicPickerProps { topics: InspirationTopic[]; selectedTopicId: string | null; onSelectTopic: (topicId: string | null) => void; } export function TopicPicker({ topics, selectedTopicId, onSelectTopic }: TopicPickerProps) { const [open, setOpen] = useState(false); const [query, setQuery] = useState(""); const selected = selectedTopicId ? topics.find((t) => t.id === selectedTopicId) ?? null : null; const filtered = useMemo(() => { const q = query.trim().toLowerCase(); if (!q) return topics; return topics.filter((t) => t.label.toLowerCase().includes(q)); }, [topics, query]); function pick(topicId: string | null) { onSelectTopic(topicId); setOpen(false); setQuery(""); } const triggerLabel = selected ? selected.label : `全部主題${topics.length > 0 ? ` · ${topics.length}` : ""}`; return ( <> setOpen(true)} > {triggerLabel} 選擇主題 setQuery(e.target.value)} placeholder="搜尋主題名稱" className="pl-9" autoFocus /> pick(null)} className={cn( "flex w-full items-center justify-between rounded-md px-3 py-2 text-left text-[13px] transition-colors hover:bg-secondary", selectedTopicId === null && "bg-primary/10 text-primary" )} > 全部主題 {selectedTopicId === null && } {filtered.length === 0 ? ( 找不到符合的主題 ) : ( filtered.map((topic) => ( pick(topic.id)} className={cn( "flex w-full items-center justify-between gap-2 rounded-md px-3 py-2 text-left text-[13px] transition-colors hover:bg-secondary", selectedTopicId === topic.id && "bg-primary/10 text-primary" )} > {topic.label} {TOPIC_GOAL_LABELS[isPlacementGoal(topic.topicGoal) ? "placement" : "viral"]} {selectedTopicId === topic.id && } )) )} > ); }
找不到符合的主題