import { useMemo, useState } from "react" import rawModCodes from "@/data/modification-codes.json" type ModCodeSource = { weapon: string "modification-code": string mode?: string tags?: string[] note?: string } type ModCode = { id: string weapon: string code: string mode?: string tags: string[] note?: string } const MOD_CODES: ModCode[] = (rawModCodes as ModCodeSource[]).map((item, index) => ({ id: `mod-${index + 1}`, weapon: item.weapon, code: item["modification-code"], mode: item.mode, tags: item.tags ?? [], note: item.note, })) export default function ModCodes() { const [keyword, setKeyword] = useState("") const [activeTag, setActiveTag] = useState("全部") const [copiedId, setCopiedId] = useState(null) const [copyErrorId, setCopyErrorId] = useState(null) const handleCopy = async (item: ModCode) => { try { await navigator.clipboard.writeText(item.code) setCopyErrorId(null) setCopiedId(item.id) window.setTimeout(() => { setCopiedId((current) => (current === item.id ? null : current)) }, 1500) } catch { setCopiedId(null) setCopyErrorId(item.id) window.setTimeout(() => { setCopyErrorId((current) => (current === item.id ? null : current)) }, 1500) } } const allTags = useMemo(() => { const tags = new Set() MOD_CODES.forEach((item) => { item.tags.forEach((tag) => tags.add(tag)) }) return ["全部", ...Array.from(tags)] }, []) const filtered = useMemo(() => { const q = keyword.trim().toLowerCase() return MOD_CODES.filter((item) => { const matchTag = activeTag === "全部" || item.tags.includes(activeTag) const matchKeyword = q.length === 0 || item.weapon.toLowerCase().includes(q) || item.code.toLowerCase().includes(q) || (item.mode?.toLowerCase().includes(q) ?? false) || item.tags.some((tag) => tag.toLowerCase().includes(q)) return matchTag && matchKeyword }) }, [activeTag, keyword]) return (

支持按 tag 与关键字筛选。关键字可匹配枪械名称、改枪码或 tag。

{allTags.map((tag) => { const selected = tag === activeTag return ( ) })}
{filtered.map((item) => (

{item.weapon}

{item.mode ? ( {item.mode} ) : ( ID: {item.id} )}
{item.code}
{copyErrorId === item.id ? (

复制失败,请手动选中复制。

) : null} {item.note ?

{item.note}

: null}
{item.tags.map((tag) => ( ))}
))}
{filtered.length === 0 ? (
未找到匹配的改枪码,请尝试其他 tag 或关键字。
) : null}
) }