"use client"; import Image from "next/image"; import { closestCenter, DndContext, KeyboardSensor, PointerSensor, useSensor, useSensors, type DragEndEvent } from "@dnd-kit/core"; import { SortableContext, arrayMove, sortableKeyboardCoordinates, useSortable, verticalListSortingStrategy } from "@dnd-kit/sortable"; import { CSS } from "@dnd-kit/utilities"; import { GripVerticalIcon, Trash2Icon } from "lucide-react"; import { useState } from "react"; import { saveGuideOverview } from "@/app/admin/actions"; import { AutosaveStatus } from "@/components/admin/autosave-status"; import { MediaUploader } from "@/components/admin/media-uploader"; import { MaterialPoster, type PosterBlendMode } from "@/components/public/material-poster"; import { GuideImageExport } from "@/components/public/guide-image-export"; import { Button } from "@/components/ui/button"; import { Card, CardContent, CardDescription, CardFooter, CardHeader, CardTitle, } from "@/components/ui/card"; import { Field, FieldDescription, FieldError, FieldGroup, FieldLabel, } from "@/components/ui/field"; import { Input } from "@/components/ui/input"; import { Textarea } from "@/components/ui/textarea"; import { useAutosave } from "@/hooks/use-autosave"; import { adminDraftKey } from "@/lib/editor/local-draft"; import { parseYouTubeUrl } from "@/lib/guides/youtube"; import { detectThemeColorsFromPixels } from "@/lib/overview-image"; import type { PublicGuide } from "@/lib/guides/queries"; import { cn } from "@/lib/utils"; type BleedingLayer = { id: string; mediaId: string; imageUrl: string | null; amount: number; scale: number; blendMode: PosterBlendMode; }; const blendModes: PosterBlendMode[] = ["normal", "multiply", "screen", "overlay", "darken", "lighten", "color-dodge", "color-burn", "hard-light", "soft-light", "difference", "exclusion", "hue", "saturation", "color", "luminosity"]; function SortableBleedingLayer({ layer, onChange, onRemove }: { layer: BleedingLayer; onChange: (patch: Partial>) => void; onRemove: () => void }) { const { attributes, listeners, setNodeRef, transform, transition, isDragging } = useSortable({ id: layer.id }); return (
{layer.imageUrl ? :
}
Amount{layer.amount}%
onChange({ amount: Number(event.target.value) })} aria-label="Bleeding amount" />
Scale{layer.scale}%
onChange({ scale: Number(event.target.value) })} aria-label="Bleeding scale" />
); } export function GuideOverviewForm({ guide, }: { guide: { id: string; version: number; overview: string; youtubeUrl: string | null; coverMediaId: string; coverUrl: string; name: string; posterArtMediaId: string | null; posterArtUrl: string | null; posterArtOffsetX: number; posterArtOffsetY: number; posterArtScale: number; posterTopColors: [string, string]; posterBottomColors: [string, string]; posterBleeding: Array<{ id: string; mediaId: string; imageUrl: string | null; amount: number; scale: number; blendMode: PosterBlendMode; sortOrder: number }>; character: Omit; }; }) { const [overview, setOverview] = useState(guide.overview); const [youtubeUrl, setYoutubeUrl] = useState(guide.youtubeUrl ?? ""); const [cover, setCover] = useState({ id: guide.coverMediaId, url: guide.coverUrl, }); const [posterArt, setPosterArt] = useState({ id: guide.posterArtMediaId, url: guide.posterArtUrl, }); const [posterArtOffset, setPosterArtOffset] = useState({ x: guide.posterArtOffsetX, y: guide.posterArtOffsetY, }); const [posterArtScale, setPosterArtScale] = useState(guide.posterArtScale); const [posterTopColors, setPosterTopColors] = useState(guide.posterTopColors); const [posterBottomColors, setPosterBottomColors] = useState(guide.posterBottomColors); const [bleeding, setBleeding] = useState(guide.posterBleeding.map(({ id, mediaId, imageUrl, amount, scale, blendMode }) => ({ id, mediaId, imageUrl, amount, scale, blendMode }))); const sensors = useSensors(useSensor(PointerSensor), useSensor(KeyboardSensor, { coordinateGetter: sortableKeyboardCoordinates })); const youtubeVideo = parseYouTubeUrl(youtubeUrl.trim()); const invalidYoutubeUrl = Boolean(youtubeUrl.trim() && !youtubeVideo); const value = { coverMediaId: cover.id, overview, youtubeUrl, posterArtMediaId: posterArt.id, posterArtOffsetX: posterArtOffset.x, posterArtOffsetY: posterArtOffset.y, posterArtScale, posterTopColors, posterBottomColors, posterBleeding: bleeding.map(({ mediaId, amount, scale, blendMode }) => ({ mediaId, amount, scale, blendMode })), }; const incompleteIssues = [ !cover.id ? "กรุณาอัปโหลด Guide Image" : null, !overview.trim() ? "กรุณากรอก Overview" : null, invalidYoutubeUrl ? "กรุณาใส่ลิงก์วิดีโอ YouTube ที่ถูกต้อง" : null, ].filter((issue): issue is string => Boolean(issue)); const invalidCover = !cover.id; const invalidOverview = !overview.trim(); const localDraft = { overview, youtubeUrl, cover, posterArt, posterArtOffset, posterArtScale, posterTopColors, posterBottomColors, bleeding }; const autosave = useAutosave({ value, valid: Boolean(cover.id && overview.trim() && !invalidYoutubeUrl), initialVersion: guide.version, persist: (next, expectedVersion) => saveGuideOverview(guide.id, { ...next, expectedVersion }), storageKey: adminDraftKey(guide.id, "overview"), localDraft, parseLocalDraft: (candidate) => { if (!candidate || typeof candidate !== "object") return null; const draft = candidate as Record; if ( typeof draft.overview !== "string" || !draft.cover || typeof draft.cover !== "object" ) return null; const draftCover = draft.cover as Record; if ( typeof draftCover.id !== "string" || typeof draftCover.url !== "string" ) return null; if ( draft.youtubeUrl !== undefined && typeof draft.youtubeUrl !== "string" ) return null; return { overview: draft.overview, youtubeUrl: typeof draft.youtubeUrl === "string" ? draft.youtubeUrl : "", cover: { id: draftCover.id, url: draftCover.url }, posterArt: draft.posterArt && typeof draft.posterArt === "object" ? { id: typeof (draft.posterArt as Record).id === "string" ? (draft.posterArt as Record).id as string : null, url: typeof (draft.posterArt as Record).url === "string" ? (draft.posterArt as Record).url as string : null, } : { id: guide.posterArtMediaId, url: guide.posterArtUrl }, posterArtOffset: draft.posterArtOffset && typeof draft.posterArtOffset === "object" ? { x: typeof (draft.posterArtOffset as Record).x === "number" ? (draft.posterArtOffset as Record).x as number : 0, y: typeof (draft.posterArtOffset as Record).y === "number" ? (draft.posterArtOffset as Record).y as number : 0, } : { x: guide.posterArtOffsetX, y: guide.posterArtOffsetY }, posterArtScale: typeof draft.posterArtScale === "number" ? Math.max(50, Math.min(200, draft.posterArtScale)) : guide.posterArtScale, posterTopColors: Array.isArray(draft.posterTopColors) && draft.posterTopColors.length === 2 ? draft.posterTopColors as [string, string] : guide.posterTopColors, posterBottomColors: Array.isArray(draft.posterBottomColors) && draft.posterBottomColors.length === 2 ? draft.posterBottomColors as [string, string] : guide.posterBottomColors, bleeding: Array.isArray(draft.bleeding) ? draft.bleeding.flatMap((item) => { if (!item || typeof item !== "object") return []; const layer = item as Record; return typeof layer.id === "string" && typeof layer.mediaId === "string" && typeof layer.imageUrl === "string" ? [{ id: layer.id, mediaId: layer.mediaId, imageUrl: layer.imageUrl, amount: typeof layer.amount === "number" ? Math.max(0, Math.min(100, layer.amount)) : 100, scale: typeof layer.scale === "number" ? Math.max(25, Math.min(300, layer.scale)) : 100, blendMode: typeof layer.blendMode === "string" && blendModes.includes(layer.blendMode as PosterBlendMode) ? layer.blendMode as PosterBlendMode : "multiply" }] : []; }) : guide.posterBleeding.map(({ id, mediaId, imageUrl, amount, scale, blendMode }) => ({ id, mediaId, imageUrl, amount, scale, blendMode })), }; }, restoreLocalDraft: (draft) => { setOverview(draft.overview); setYoutubeUrl(draft.youtubeUrl); setCover(draft.cover); setPosterArt(draft.posterArt); setPosterArtOffset(draft.posterArtOffset); setPosterArtScale(draft.posterArtScale); setPosterTopColors(draft.posterTopColors); setPosterBottomColors(draft.posterBottomColors); setBleeding(draft.bleeding); }, }); async function detectPosterColors(url: string) { try { const image = await new Promise((resolve, reject) => { const candidate = new window.Image(); candidate.crossOrigin = "anonymous"; candidate.onload = () => resolve(candidate); candidate.onerror = reject; candidate.src = url; }); const scale = Math.min(1, 512 / Math.max(image.naturalWidth, image.naturalHeight)); const canvas = document.createElement("canvas"); canvas.width = Math.max(1, Math.round(image.naturalWidth * scale)); canvas.height = Math.max(1, Math.round(image.naturalHeight * scale)); const context = canvas.getContext("2d"); if (!context) return; context.drawImage(image, 0, 0, canvas.width, canvas.height); const colors = detectThemeColorsFromPixels( context.getImageData(0, 0, canvas.width, canvas.height).data, canvas.width, canvas.height, ); setPosterTopColors(colors.top); setPosterBottomColors(colors.bottom); } catch { // Keep the current colors when the uploaded media host disallows pixel reads. } } function setUploadedPoster(asset: { id: string; url: string }) { setPosterArt(asset); setPosterArtOffset({ x: 0, y: 0 }); setBleeding((current) => current.length ? current.map((layer) => ({ ...layer, mediaId: asset.id, imageUrl: asset.url })) : [{ id: crypto.randomUUID(), mediaId: asset.id, imageUrl: asset.url, amount: 65, scale: 100, blendMode: "multiply" }]); } function detectPosterColorsFromFile(file: File) { const url = URL.createObjectURL(file); void detectPosterColors(url).finally(() => URL.revokeObjectURL(url)); } function addBleedingEffect() { if (!posterArt.id || !posterArt.url) return; const mediaId = posterArt.id; const imageUrl = posterArt.url; setBleeding((current) => [...current, { id: crypto.randomUUID(), mediaId, imageUrl, amount: 65, scale: 100, blendMode: "multiply" }]); } function handleBleedingDragEnd(event: DragEndEvent) { if (!event.over || event.active.id === event.over.id) return; setBleeding((current) => { const oldIndex = current.findIndex((layer) => layer.id === event.active.id); const newIndex = current.findIndex((layer) => layer.id === event.over?.id); return oldIndex < 0 || newIndex < 0 ? current : arrayMove(current, oldIndex, newIndex); }); } return ( Overview ภาพ ข้อความ และวิดีโอนี้แสดงบนหน้าแรกของ Guide Guide Image
{`Guide
setCover(asset)} />
Overview