"use client"; import { ChevronLeftIcon, ChevronRightIcon, InboxIcon, InfoIcon, PlusIcon, SearchIcon, XIcon, } from "lucide-react"; import { useEffect, useMemo, useState } from "react"; import { Button } from "@/components/ui/button"; import { Input } from "@/components/ui/input"; import { Select, SelectContent, SelectItem, SelectTrigger, SelectValue, } from "@/components/ui/select"; import type { Skill } from "@/core/skills"; import { desensitize } from "@/lib/desensitize"; import { cn } from "@/lib/utils"; const PAGE_SIZE = 5; const ALL_CATEGORIES = "__all__"; interface SkillTransferSelectorProps { /** Full pool of selectable skills (caller pre-filters by `enabled`). */ skills: Skill[]; /** Currently selected skill names — the persisted `skills: string[]`. */ selected: string[]; /** Emits the next selection (always a `string[]`, `[]` when empty). */ onChange: (next: string[]) => void; isLoading?: boolean; /** Open the skill detail dialog for a given skill name. */ onViewDetail?: (name: string) => void; } function skillLabel(skill: Skill) { return skill.name_zh?.trim() ? skill.name_zh : desensitize(skill.name); } function matchesQuery(skill: Skill, q: string) { if (!q) return true; const haystack = [ skill.name, skill.name_zh, skill.description, skill.detail, skill.category, skill.license, skill.source, skill.state, skill.owner_username, skill.created_at, skill.last_activity_at, skill.use_count != null ? `使用 ${skill.use_count}` : null, skill.view_count != null ? `查看 ${skill.view_count}` : null, skill.patch_count != null ? `变迁 ${skill.patch_count}` : null, ...(skill.tags ?? []).flatMap((tag) => [ tag.name, tag.description, tag.color, tag.usage_count != null ? `标签使用 ${tag.usage_count}` : null, ]), ] .filter((value): value is string => typeof value === "string" && value.length > 0) .join(" ") .toLowerCase(); return haystack.includes(q); } export function SkillTransferSelector({ skills, selected, onChange, isLoading, onViewDetail, }: SkillTransferSelectorProps) { const [search, setSearch] = useState(""); const [category, setCategory] = useState(ALL_CATEGORIES); const [availablePage, setAvailablePage] = useState(0); const [selectedPage, setSelectedPage] = useState(0); const selectedSet = useMemo(() => new Set(selected), [selected]); // Distinct categories for the filter dropdown (skip blank ones). const categories = useMemo(() => { const set = new Set(); for (const skill of skills) { if (skill.category?.trim()) set.add(skill.category.trim()); } return Array.from(set).sort((a, b) => a.localeCompare(b)); }, [skills]); const query = search.trim().toLowerCase(); // Left pane: unselected skills, filtered by search + category. const availableSkills = useMemo(() => { return skills.filter( (skill) => !selectedSet.has(skill.name) && matchesQuery(skill, query) && (category === ALL_CATEGORIES || skill.category?.trim() === category), ); }, [skills, selectedSet, query, category]); // Right pane: selected skills, filtered by search only (category filtering // the chosen set would just hide active selections, which is confusing). const selectedSkills = useMemo(() => { const byName = new Map(skills.map((s) => [s.name, s])); return selected .map((name) => byName.get(name)) .filter((s): s is Skill => Boolean(s) && matchesQuery(s!, query)); }, [skills, selected, query]); // Clamp pagination whenever the underlying lists shrink (search/category // changes, items moved between panes). const availablePages = Math.max(1, Math.ceil(availableSkills.length / PAGE_SIZE)); const selectedPages = Math.max(1, Math.ceil(selectedSkills.length / PAGE_SIZE)); useEffect(() => { if (availablePage > availablePages - 1) setAvailablePage(availablePages - 1); }, [availablePage, availablePages]); useEffect(() => { if (selectedPage > selectedPages - 1) setSelectedPage(selectedPages - 1); }, [selectedPage, selectedPages]); const availableView = availableSkills.slice( availablePage * PAGE_SIZE, availablePage * PAGE_SIZE + PAGE_SIZE, ); const selectedView = selectedSkills.slice( selectedPage * PAGE_SIZE, selectedPage * PAGE_SIZE + PAGE_SIZE, ); function add(name: string) { if (selectedSet.has(name)) return; onChange([...selected, name]); } function remove(name: string) { onChange(selected.filter((n) => n !== name)); } function addAllFiltered() { if (availableSkills.length === 0) return; const merged = [...selected]; for (const skill of availableSkills) { if (!selectedSet.has(skill.name)) merged.push(skill.name); } onChange(merged); } function clearSelected() { if (selected.length === 0) return; onChange([]); } if (isLoading) { return (
技能加载中...
); } if (skills.length === 0) { return (
当前没有可用技能。
); } return (
{/* Toolbar: search + category filter */}
{ setSearch(e.currentTarget.value); setAvailablePage(0); setSelectedPage(0); }} className="h-8 pl-7 text-xs" />
{categories.length > 0 && ( )}
{/* Bulk actions */}
{/* Two-column transfer panes */}
); } interface SkillPaneProps { title: string; count: number; items: Skill[]; variant: "available" | "selected"; page: number; totalPages: number; onPageChange: (page: number) => void; onPick: (name: string) => void; onViewDetail?: (name: string) => void; emptyHint: string; } function SkillPane({ title, count, items, variant, page, totalPages, onPageChange, onPick, onViewDetail, emptyHint, }: SkillPaneProps) { const isSelected = variant === "selected"; return (
{title} {count}
{items.length === 0 ? (
{emptyHint}
) : ( items.map((skill) => ( onPick(skill.name)} onViewDetail={ onViewDetail ? () => onViewDetail(skill.name) : undefined } /> )) )}
{totalPages > 1 && (
{page + 1}/{totalPages}
)}
); } interface SkillItemCardProps { skill: Skill; isSelected: boolean; onPick: () => void; onViewDetail?: () => void; } function SkillItemCard({ skill, isSelected, onPick, onViewDetail, }: SkillItemCardProps) { return (
{ if (e.key === "Enter" || e.key === " ") { e.preventDefault(); onPick(); } }} title={isSelected ? "点击移除" : "点击添加"} className={cn( "group/item relative cursor-pointer rounded-md border px-2.5 py-1.5 pr-7 transition-colors", isSelected ? "border-primary/60 bg-primary/5 hover:border-primary dark:border-[#3B82F6] dark:bg-[#0a3568]" : "hover:border-primary/40 hover:bg-accent/40 dark:border-[#213D5E] dark:bg-[#02244C] dark:hover:border-[#3B82F6]/60", )} >
{isSelected ? ( ) : ( )}
{skillLabel(skill)}
{skill.description?.trim() && (
{desensitize(skill.description)}
)}
{onViewDetail && ( )}
); }