"use client"; import { useState } from "react"; import { addItem, deleteItem, moveItem, updateItem } from "@/app/admin/item-actions"; import type { ActionResult } from "@/app/admin/actions"; import { Button } from "@/components/ui/button"; import { Field, inputClass } from "@/components/ui/field"; import type { Tables } from "@/lib/database.types"; import type { GameWithCriteria } from "@/lib/queries/event"; import { MEDIA_BUCKET, parseYouTube, storagePathFor, uploadAccept, uploadProblem, type ItemKind, } from "@/lib/screen"; import { createClient } from "@/lib/supabase/browser"; import { ErrorText } from "./error-text"; import { useAction } from "./use-action"; type Item = Tables<"game_items">; export const kindLabel: Record = { question: "Question", sound: "Sound", video: "Video" }; const titleLabel: Record = { question: "Question", sound: "Name (only you see it)", video: "Name (only you see it)", }; const linkLabel: Record = { question: "Picture link (optional)", sound: "Link to the sound", video: "Link to the video", }; const linkHint: Record = { question: "A link to an image, starting with https://", sound: "A YouTube link (only the sound plays) or a link to an audio file.", video: "A YouTube link, or a link to a video file.", }; function sourceLabel(item: Item): string | null { if (item.storage_path) return "Uploaded file"; if (!item.media_url) return null; return parseYouTube(item.media_url) ? "YouTube" : "Link"; } /** A game's questions, sounds and videos: what the host can put on the scoreboard. */ export function ItemsEditor({ eventId, game }: { eventId: string; game: GameWithCriteria }) { const items = game.game_items; return (

On the scoreboard: questions, sounds and videos

In the control room you click one to show it. A question appears above the scores, a sound plays through the scoreboard's speakers, and a video (like a karaoke track) takes over the whole screen.

{items.length > 0 && (
    {items.map((item, index) => ( ))}
)}
); } function ItemRow({ eventId, gameId, item, isFirst, isLast, }: { eventId: string; gameId: string; item: Item; isFirst: boolean; isLast: boolean; }) { const [editing, setEditing] = useState(false); const [draft, setDraft] = useState({ title: item.title, answer: item.answer ?? "", mediaUrl: item.media_url ?? "" }); const { pending, error, run } = useAction(); const source = sourceLabel(item); return (
  • {kindLabel[item.kind]} {item.title} {item.answer && Answer: {item.answer}} {source && {source}}
    {editing && (
    { event.preventDefault(); run(() => updateItem(eventId, gameId, item.id, draft), () => setEditing(false)); }} > setDraft({ ...draft, title: event.target.value })} required maxLength={500} className={inputClass} /> {item.kind !== "video" && ( setDraft({ ...draft, answer: event.target.value })} maxLength={500} className={inputClass} /> )} {item.storage_path ? (

    Uses an uploaded file. To change the file, delete this and add it again.

    ) : ( setDraft({ ...draft, mediaUrl: event.target.value })} maxLength={2000} className={inputClass} /> )}
    )} {error}
  • ); } function AddItemForm({ eventId, gameId }: { eventId: string; gameId: string }) { const [kind, setKind] = useState("question"); const [title, setTitle] = useState(""); const [answer, setAnswer] = useState(""); const [mediaUrl, setMediaUrl] = useState(""); const [file, setFile] = useState(null); // Bumped to empty the file input, which React can't control directly. const [fileKey, setFileKey] = useState(0); const { pending, error, run } = useAction(); const clearFile = () => { setFile(null); setFileKey((key) => key + 1); }; /** Uploads the chosen file (if any) straight to storage, then records the item. */ const save = async (): Promise => { const draft = { kind, title, answer, mediaUrl: file ? "" : mediaUrl }; if (!file) return addItem(eventId, gameId, { ...draft, storagePath: null }); const problem = uploadProblem(kind, file); const storagePath = storagePathFor(eventId, file.name, crypto.randomUUID()); if (problem || !storagePath) return { ok: false, error: problem ?? "That file can't be uploaded." }; const storage = createClient().storage.from(MEDIA_BUCKET); const upload = await storage.upload(storagePath, file, { contentType: file.type }); if (upload.error) return { ok: false, error: `The upload failed: ${upload.error.message}. Try again.` }; const result = await addItem(eventId, gameId, { ...draft, storagePath }); // Don't leave a file behind that no item points to. if (!result.ok) await storage.remove([storagePath]); return result; }; return (
    { event.preventDefault(); run(save, () => { setTitle(""); setAnswer(""); setMediaUrl(""); clearFile(); }); }} > setTitle(event.target.value)} required maxLength={500} className={inputClass} placeholder={kind === "question" ? "Who built the ark?" : kind === "sound" ? "Mystery song 1" : "Oceans — karaoke"} /> {kind !== "video" && ( setAnswer(event.target.value)} maxLength={500} className={inputClass} /> )} setMediaUrl(event.target.value)} maxLength={2000} disabled={file !== null} className={`${inputClass} disabled:bg-ink/5`} placeholder="https://" /> setFile(event.target.files?.[0] ?? null)} className={`${inputClass} py-2`} />
    {error}
    ); }