"use client"; import Image from "next/image"; import { useRouter } from "next/navigation"; import { ArrowDownIcon, ArrowUpIcon, PlusIcon, SaveIcon, Trash2Icon } from "lucide-react"; import { useState, useTransition } from "react"; import { toast } from "sonner"; import { saveArtifactSection, saveConstellationSection, saveExtraSectionList, saveTeamSection, saveWeaponSection } from "@/app/admin/actions"; import { MediaUploader } from "@/components/admin/media-uploader"; import { Button } from "@/components/ui/button"; import { Card, CardAction, CardContent, CardDescription, CardFooter, CardHeader, CardTitle } from "@/components/ui/card"; import { Combobox, ComboboxContent, ComboboxEmpty, ComboboxInput, ComboboxItem, ComboboxList } from "@/components/ui/combobox"; import { Field, FieldDescription, FieldGroup, FieldLabel, FieldLegend, FieldSet } from "@/components/ui/field"; import { Input } from "@/components/ui/input"; import { Pagination, PaginationContent, PaginationItem, PaginationLink, PaginationNext, PaginationPrevious } from "@/components/ui/pagination"; import { Spinner } from "@/components/ui/spinner"; import { Textarea } from "@/components/ui/textarea"; import { ToggleGroup, ToggleGroupItem } from "@/components/ui/toggle-group"; import { mainStatOptions, substatOptions } from "@/lib/guides/schemas"; type CatalogItem = { key: string; name: string; imageUrl: string }; type Guide = { id: string; characterKey: string; version: number; name: string }; type WeaponValue = { weaponKey: string; refinement: number; overallPercent: string | null; personalPercent: string | null; weapon: CatalogItem }; type ArtifactValue = { id: string; name: string; rows: Array<{ firstArtifactKey: string; secondArtifactKey: string | null; percent: string }> }; type ArtifactProfile = { sands: string[]; goblet: string[]; circlet: string[]; substats: string[]; targets: Array<{ stat: string; value: string }>; talents: Array<{ type: "NA" | "E" | "Q"; priority: number; level?: string }>; note: string | null } | null; type ConstellationValue = { level: number; detail: string; cumulativePercent: string }; type TeamValue = { title: string; rotationSeconds: string; combo: string; members: Array<{ characterKey: string; damage: string }> }; export interface GuideSectionEditorProps { section: "weapon" | "artifact" | "constellations" | "team" | "extra"; guide: Guide; weapons: CatalogItem[]; artifacts: CatalogItem[]; characters: CatalogItem[]; initial: { weapons: WeaponValue[]; artifactGroups: ArtifactValue[]; artifactProfile: ArtifactProfile; constellations: ConstellationValue[]; teams: TeamValue[]; extras: Array<{ title: string; rows: Array<{ mediaId: string | null; heading: string; body: string; percent: string | null; imageUrl: string | null }> }>; }; } const SECTION_ORDER = ["weapon", "artifact", "constellations", "team", "extra"] as const; const SECTION_LABELS = { weapon: "Weapons", artifact: "Artifacts & Stats", constellations: "Constellations", team: "Team Calc", extra: "Extra" }; function CatalogPicker({ items, value, onChange, placeholder }: { items: CatalogItem[]; value: string; onChange: (value: string) => void; placeholder: string }) { return ( item.key)} value={value || null} onValueChange={(next) => onChange(next ?? "")}> ไม่พบรายการ{items.map((item) => {item.name})} ); } function RowActions({ index, length, move, remove }: { index: number; length: number; move: (from: number, to: number) => void; remove?: () => void }) { return
{remove ? : null}
; } function moveItem(items: T[], from: number, to: number): T[] { const next = [...items]; const [item] = next.splice(from, 1); next.splice(to, 0, item); return next; } export function GuideSectionEditor(props: GuideSectionEditorProps) { const router = useRouter(); const [version, setVersion] = useState(props.guide.version); const [pending, startTransition] = useTransition(); const index = SECTION_ORDER.indexOf(props.section); function save(action: () => Promise<{ status: string; message?: string; version?: number }>) { startTransition(async () => { const result = await action(); if (result.status === "error") { toast.error(result.message); return; } if (result.version) setVersion(result.version); toast.success("บันทึกขึ้นเว็บไซต์แล้ว"); router.refresh(); }); } return (
{props.section === "weapon" ? : null} {props.section === "artifact" ? : null} {props.section === "constellations" ? : null} {props.section === "team" ? : null} {props.section === "extra" ? : null} {index > 0 ? : Overview}{SECTION_LABELS[props.section]}{index < SECTION_ORDER.length - 1 ? : null}
); } type EditorBase = GuideSectionEditorProps & { version: number; pending: boolean; save: (action: () => Promise<{ status: string; message?: string; version?: number }>) => void }; function WeaponEditor({ guide, weapons, initial, version, pending, save }: EditorBase) { const [rows, setRows] = useState(initial.weapons.map((row) => ({ weaponKey: row.weaponKey, refinement: row.refinement, overallPercent: row.overallPercent ?? "", personalPercent: row.personalPercent ?? "" }))); return Weapon tier listเรียงอาวุธจากดีที่สุดลงมา และกรอกเปอร์เซ็นต์อย่างน้อยหนึ่งช่อง{rows.map((row, rowIndex) => อันดับ {rowIndex + 1} setRows(moveItem(rows, from, to))} remove={() => setRows(rows.filter((_, index) => index !== rowIndex))} />อาวุธ setRows(rows.map((item, index) => index === rowIndex ? { ...item, weaponKey } : item))} />Refinement { const next = value[0]; if (next) setRows(rows.map((item, index) => index === rowIndex ? { ...item, refinement: Number(next.slice(1)) } : item)); }}>{[1,2,3,4,5].map((r) => R{r})}
Overall % setRows(rows.map((item, index) => index === rowIndex ? { ...item, overallPercent: event.target.value } : item))} />Personal % setRows(rows.map((item, index) => index === rowIndex ? { ...item, personalPercent: event.target.value } : item))} />
)}
; } function ArtifactEditor({ guide, artifacts, initial, version, pending, save }: EditorBase) { const profile = initial.artifactProfile; const [groups, setGroups] = useState(initial.artifactGroups.map((group) => ({ name: group.name, rows: group.rows.map((row) => ({ ...row })) }))); const [sands, setSands] = useState(profile?.sands ?? []); const [goblet, setGoblet] = useState(profile?.goblet ?? []); const [circlet, setCirclet] = useState(profile?.circlet ?? []); const [substats, setSubstats] = useState(profile?.substats ?? []); const [targets, setTargets] = useState(profile?.targets ?? []); const [talents, setTalents] = useState(profile?.talents ?? (["NA","E","Q"] as const).map((type, priority) => ({ type, priority: priority + 1, level: "" }))); const [note, setNote] = useState(profile?.note ?? ""); function statToggle(label: string, options: readonly string[], value: string[], setter: (value: string[]) => void) { return {label} { if (next.length <= 2) setter(next); }} className="flex-wrap justify-start">{options.map((option) => {option})}เลือกได้สูงสุด 2 ค่า; } return
Artifact tier listสร้างกลุ่ม เช่น Main, Domain หรือ Special แล้วเรียงชุดในแต่ละกลุ่ม{groups.map((group, groupIndex) => setGroups(groups.map((item, index) => index === groupIndex ? { ...item, name: event.target.value } : item))} />{group.rows.map((row, rowIndex) =>
setGroups(groups.map((item, index) => index === groupIndex ? { ...item, rows: item.rows.map((candidate, inner) => inner === rowIndex ? { ...candidate, firstArtifactKey: value } : candidate) } : item))} /> setGroups(groups.map((item, index) => index === groupIndex ? { ...item, rows: item.rows.map((candidate, inner) => inner === rowIndex ? { ...candidate, secondArtifactKey: value || null } : candidate) } : item))} /> setGroups(groups.map((item, index) => index === groupIndex ? { ...item, rows: item.rows.map((candidate, inner) => inner === rowIndex ? { ...candidate, percent: event.target.value } : candidate) } : item))} />
)}
)}
Recommended Statsเลือก Main Stat ได้สูงสุด 2 ค่าต่อชิ้น{statToggle("Sands / นาฬิกา", mainStatOptions.sands, sands, setSands)}{statToggle("Goblet / ถ้วย", mainStatOptions.goblet, goblet, setGoblet)}{statToggle("Circlet / หมวก", mainStatOptions.circlet, circlet, setCirclet)}Substats{substatOptions.map((option) => {option})}
Target Stats{targets.map((target, index) =>
setTargets(targets.map((item, itemIndex) => itemIndex === index ? { ...item, stat: event.target.value } : item))} /> setTargets(targets.map((item, itemIndex) => itemIndex === index ? { ...item, value: event.target.value } : item))} />
)}
Talent Priority{talents.sort((a,b) => a.priority-b.priority).map((talent, index) =>
{index + 1}. {talent.type} setTalents(talents.map((item) => item.type === talent.type ? { ...item, level: event.target.value } : item))} /> setTalents(moveItem([...talents].sort((a,b) => a.priority-b.priority),from,to).map((item, priority) => ({ ...item, priority: priority + 1 })))} />
)}
Note (optional)