- Clipador em subpáginas (Cortes, Sessões, Fontes, Jogos, Ajustes) com moldura comum, lista + diálogo dos cortes e submenu na barra lateral. - Perfis de jogo (o que a IA sabe de cada jogo), parar pausa o monitor e idioma por fonte e por canal de publicação (migração v9: accounts.language). - Juiz: Gemini CLI como terceiro provedor e IA reserva por chamada — se a IA escolhida cair por cota, login, chave ou rede, a próxima da ordem de Configurações → IA responde (com pausa da que caiu). Lê data/ai/settings.json e o cofre data/ai/vault.bin (DPAPI) escritos pelo painel; contrato em doc/ia.md. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
214 lines
12 KiB
TypeScript
214 lines
12 KiB
TypeScript
"use client";
|
|
// Contexto do jogo e emotes do Clipador: o jogo da sessão (com correção à mão e aviso quando a tela
|
|
// diverge da categoria), o atalho "O que a IA sabe do jogo" (abre o modal do jogo, clipper-game-dialog.tsx) e a
|
|
// tabela de emotes do canal com o peso que eles aprenderam com as suas aprovações.
|
|
// A página Jogos (lista + modal) fica em clipper-games-page.tsx.
|
|
import { useEffect, useMemo, useRef, useState, type ReactNode } from "react";
|
|
import {
|
|
GAME_SOURCE_LABEL, clipperCall, gameIdOf, useClipperAction, type ClipperSession, type Emote, type Game,
|
|
} from "./clipper-client";
|
|
import { GameDialog } from "./clipper-game-dialog";
|
|
import { AlertIcon, BookIcon, ChatIcon, CheckIcon, ChevronDownIcon, PenIcon } from "./icons";
|
|
import { Badge, Button, Feedback, Input, Spinner, Table, Td, Th, THead, Tr, cx, formatRelative } from "./ui";
|
|
|
|
/* ---------- Jogo da sessão ---------- */
|
|
|
|
export function SessionGame({ session, games, onChanged, onGamesChanged }: {
|
|
session: ClipperSession; games: Game[]; onChanged: () => void; onGamesChanged: (games: Game[]) => void;
|
|
}) {
|
|
const { pending, message, run } = useClipperAction(onChanged);
|
|
const [choosing, setChoosing] = useState(false);
|
|
const [open, setOpen] = useState(false);
|
|
const game = session.game ?? "";
|
|
const screen = session.screenGame ?? "";
|
|
const confirmed = session.gameSource === "user";
|
|
const mismatch = Boolean(game && screen && !confirmed && gameIdOf(screen) !== gameIdOf(game));
|
|
const profile = game ? findGame(games, game) : undefined;
|
|
|
|
/** Confirma o jogo: trava a troca automática e a IA deixa de questionar o que vê na tela. */
|
|
async function confirmGame(name: string) {
|
|
const ok = await run("game", () => clipperCall<ClipperSession>(`sessions/${session.id}/game`, "PUT", { name }),
|
|
`Jogo confirmado: “${name}”. A IA não questiona mais o jogo nesta sessão.`);
|
|
if (ok) setChoosing(false);
|
|
}
|
|
|
|
return (
|
|
<div className="grid gap-2">
|
|
<div className="flex flex-wrap items-center gap-x-3 gap-y-2 text-xs">
|
|
<span className="text-eyebrow font-semibold text-fg-subtle uppercase">Jogo</span>
|
|
<span className="text-sm font-medium text-fg">{game || <span className="text-fg-subtle">não identificado</span>}</span>
|
|
{session.gameSource && (
|
|
<Badge tone={confirmed ? "success" : "neutral"} dot={false}>
|
|
{confirmed && <CheckIcon size={12} />}{GAME_SOURCE_LABEL[session.gameSource]}
|
|
</Badge>
|
|
)}
|
|
{game && !confirmed && (
|
|
<Button variant="secondary" size="sm" disabled={pending !== null} onClick={() => void confirmGame(game)}>
|
|
{pending === "game" ? <Spinner className="size-3.5" /> : <CheckIcon size={14} />}Sim, é {game}
|
|
</Button>
|
|
)}
|
|
<Button variant="ghost" size="sm" onClick={() => setChoosing(true)}><PenIcon size={14} />{confirmed ? "Trocar jogo" : "Escolher o jogo"}</Button>
|
|
{game && (
|
|
<Button variant="ghost" size="sm" aria-haspopup="dialog" onClick={() => setOpen(true)}>
|
|
<BookIcon size={14} />O que a IA sabe do jogo
|
|
{profile?.status === "pending" && <Spinner className="size-3" />}
|
|
</Button>
|
|
)}
|
|
</div>
|
|
{mismatch && (
|
|
<div className="flex flex-wrap items-center gap-x-3 gap-y-2 rounded-control border border-warning/25 bg-warning-soft px-3 py-2 text-xs text-warning">
|
|
<AlertIcon size={14} />
|
|
<span className="min-w-0 flex-1">A categoria diz <strong>{game}</strong>, mas a tela parece <strong>{screen}</strong>. Jogo novo costuma lembrar outro na tela — qual é o certo?</span>
|
|
<Button size="sm" variant="secondary" disabled={pending !== null} onClick={() => void confirmGame(game)}>Sim, é {game}</Button>
|
|
<Button size="sm" variant="ghost" onClick={() => setChoosing(true)}>Escolher o jogo</Button>
|
|
</div>
|
|
)}
|
|
<GameChoiceDialog open={choosing} session={session} pending={pending === "game"}
|
|
onClose={() => setChoosing(false)} onConfirm={(name) => void confirmGame(name)} />
|
|
{game && <GameDialog game={profile} name={game} open={open} onClose={() => setOpen(false)} onGamesChanged={onGamesChanged} />}
|
|
<div aria-live="polite" className="empty:hidden">{message && <Feedback ok={message.ok}>{message.text}</Feedback>}</div>
|
|
</div>
|
|
);
|
|
}
|
|
|
|
/** Jogo cadastrado com esse nome: pelo id ou por um dos outros nomes (aliases). */
|
|
function findGame(games: Game[], name: string): Game | undefined {
|
|
const id = gameIdOf(name);
|
|
return games.find((g) => g.id === id)
|
|
?? games.find((g) => [...(g.aliases ?? []), ...(g.profile?.aliases ?? []), ...(g.profileAi?.aliases ?? [])].some((a) => gameIdOf(a) === id));
|
|
}
|
|
|
|
/* ---------- Escolher/confirmar o jogo ---------- */
|
|
|
|
/** Modal nativo: o usuário confirma qual jogo está sendo jogado (categoria, o que a IA viu na tela ou outro nome). */
|
|
function GameChoiceDialog({ open, session, pending, onClose, onConfirm }: {
|
|
open: boolean; session: ClipperSession; pending: boolean; onClose: () => void; onConfirm: (name: string) => void;
|
|
}) {
|
|
const ref = useRef<HTMLDialogElement>(null);
|
|
const options = useMemo(() => {
|
|
const list: { name: string; from: string }[] = [];
|
|
const add = (name: string | undefined, from: string) => {
|
|
const clean = (name ?? "").trim();
|
|
if (clean && !list.some((o) => gameIdOf(o.name) === gameIdOf(clean))) list.push({ name: clean, from });
|
|
};
|
|
add(session.game, session.gameSource === "user" ? "confirmado por você" : "categoria da plataforma");
|
|
for (const h of [...(session.gameHistory ?? [])].reverse()) add(h.game, GAME_SOURCE_LABEL[h.source as keyof typeof GAME_SOURCE_LABEL] || "histórico");
|
|
add(session.screenGame, "a IA viu na tela");
|
|
return list;
|
|
}, [session]);
|
|
const [choice, setChoice] = useState(0);
|
|
const [other, setOther] = useState("");
|
|
|
|
useEffect(() => {
|
|
const dialog = ref.current;
|
|
if (!dialog) return;
|
|
if (open && !dialog.open) {
|
|
setChoice(0);
|
|
setOther("");
|
|
dialog.showModal();
|
|
}
|
|
if (!open && dialog.open) dialog.close();
|
|
}, [open]);
|
|
|
|
const chosen = choice === -1 ? other.trim() : options[choice]?.name ?? "";
|
|
|
|
return (
|
|
<dialog ref={ref} aria-labelledby={`game-choice-${session.id}`} onCancel={(e) => { e.preventDefault(); onClose(); }}
|
|
className="m-auto w-[min(92vw,30rem)] rounded-panel border border-line-strong bg-canvas p-0 text-fg shadow-pop backdrop:bg-black/70">
|
|
<form className="grid gap-4 p-5" onSubmit={(e) => { e.preventDefault(); if (chosen) onConfirm(chosen); }}>
|
|
<div className="grid gap-1">
|
|
<h2 id={`game-choice-${session.id}`} className="text-base font-semibold">Qual jogo está sendo jogado?</h2>
|
|
<p className="text-xs leading-relaxed text-fg-muted">
|
|
O jogo confirmado vai para a IA como certo: ela usa o perfil dele para entender os momentos e deixa de
|
|
questionar o que vê na tela. A troca automática pela categoria fica travada nesta sessão.
|
|
</p>
|
|
</div>
|
|
<fieldset className="grid gap-2">
|
|
<legend className="sr-only">Jogo</legend>
|
|
{options.map((o, i) => (
|
|
<label key={o.name} className={cx("flex cursor-pointer items-center gap-3 rounded-control border px-3 py-2.5 text-sm",
|
|
choice === i ? "border-accent/60 bg-accent-soft" : "border-line hover:border-line-strong")}>
|
|
<input type="radio" name="game" checked={choice === i} onChange={() => setChoice(i)} className="accent-[var(--color-accent)]" />
|
|
<span className="min-w-0 flex-1 font-medium">{o.name}</span>
|
|
<span className="text-xs text-fg-subtle">{o.from}</span>
|
|
</label>
|
|
))}
|
|
<label className={cx("flex cursor-pointer items-center gap-3 rounded-control border px-3 py-2.5 text-sm",
|
|
choice === -1 ? "border-accent/60 bg-accent-soft" : "border-line hover:border-line-strong")}>
|
|
<input type="radio" name="game" checked={choice === -1} onChange={() => setChoice(-1)} className="accent-[var(--color-accent)]" />
|
|
<span className="shrink-0">Outro:</span>
|
|
<Input value={other} maxLength={120} placeholder="nome do jogo" aria-label="Outro jogo" className="h-8 min-w-0 flex-1" size={10}
|
|
onFocus={() => setChoice(-1)} onChange={(e) => { setOther(e.target.value); setChoice(-1); }} />
|
|
</label>
|
|
</fieldset>
|
|
<div className="flex justify-end gap-2">
|
|
<Button type="button" variant="ghost" size="sm" onClick={onClose}>Cancelar</Button>
|
|
<Button type="submit" size="sm" disabled={!chosen || pending}>
|
|
{pending ? <Spinner className="size-3.5" /> : <CheckIcon size={15} />}Confirmar {chosen ? `“${chosen}”` : "jogo"}
|
|
</Button>
|
|
</div>
|
|
</form>
|
|
</dialog>
|
|
);
|
|
}
|
|
|
|
/* ---------- Emotes do canal ---------- */
|
|
|
|
const EMOTE_SOURCE: Record<string, string> = { twitch: "Twitch", "7tv": "7TV", bttv: "BTTV", ffz: "FFZ", kick: "Kick", emoji: "emoji" };
|
|
|
|
export function ChannelEmotes({ channelKey, title = "Emotes da fonte" }: { channelKey: string; title?: ReactNode }) {
|
|
const [open, setOpen] = useState(false);
|
|
const [emotes, setEmotes] = useState<Emote[] | null>(null);
|
|
const [error, setError] = useState<string | null>(null);
|
|
|
|
useEffect(() => {
|
|
if (!open || !channelKey) return;
|
|
let cancelled = false;
|
|
void clipperCall<{ emotes: Emote[] }>(`emotes?channel=${encodeURIComponent(channelKey)}`).then((r) => {
|
|
if (cancelled) return;
|
|
if (r.ok) { setEmotes(r.data.emotes); setError(null); } else setError(r.error);
|
|
});
|
|
return () => { cancelled = true; };
|
|
}, [open, channelKey]);
|
|
|
|
if (!channelKey) return null;
|
|
return (
|
|
<div className="grid gap-3">
|
|
<div>
|
|
<Button variant="ghost" size="sm" aria-expanded={open} onClick={() => setOpen((o) => !o)}>
|
|
<ChatIcon size={15} />{title}
|
|
<ChevronDownIcon size={14} className={cx("transition-transform", open && "rotate-180")} />
|
|
</Button>
|
|
</div>
|
|
{open && (
|
|
<div className="grid gap-3">
|
|
<p className="max-w-[90ch] text-xs leading-relaxed text-fg-subtle">
|
|
Emotes nos cortes que você aprova ganham peso; nos rejeitados, perdem. O chat pesa mais quando reage com os emotes de peso positivo.
|
|
</p>
|
|
{error ? <Feedback ok={false}>{error}</Feedback>
|
|
: !emotes ? <p role="status" className="flex items-center gap-2 text-xs text-fg-muted"><Spinner className="size-3.5" />Carregando emotes…</p>
|
|
: emotes.length === 0 ? <p className="text-xs text-fg-subtle">Nenhum emote registrado ainda para <code>{channelKey}</code>.</p>
|
|
: (
|
|
<Table className="max-h-80 overflow-y-auto">
|
|
<THead><Th>Emote</Th><Th>Origem</Th><Th className="text-right">Usos</Th><Th className="text-right">Peso aprendido</Th><Th>Visto</Th></THead>
|
|
<tbody>
|
|
{emotes.map((e) => (
|
|
<Tr key={`${e.source}-${e.name}`}>
|
|
<Td className="font-mono text-[0.8125rem] text-fg">{e.name}</Td>
|
|
<Td className="text-xs text-fg-subtle">{EMOTE_SOURCE[e.source] ?? e.source}</Td>
|
|
<Td className="text-right tabular-nums text-fg-muted">{e.uses}</Td>
|
|
<Td className={cx("text-right font-mono text-xs tabular-nums", e.weight > 0 ? "text-success" : e.weight < 0 ? "text-danger" : "text-fg-subtle")}>
|
|
{e.weight > 0 ? "+" : ""}{e.weight.toLocaleString("pt-BR", { maximumFractionDigits: 2 })}
|
|
</Td>
|
|
<Td className="text-xs text-fg-subtle">{formatRelative(e.lastSeen)}</Td>
|
|
</Tr>
|
|
))}
|
|
</tbody>
|
|
</Table>
|
|
)}
|
|
</div>
|
|
)}
|
|
</div>
|
|
);
|
|
}
|