/** * RFC-0033 §8: a page's or article's language versions in the editor's side column — each * language by name, its status, how it was made (original, a person, machine, machine reviewed), * whether it is out of date — and a person's review of a machine translation. Agents translate * through MCP (`save_translation`); only people review, here. */ import type { ContentEntry, Page } from "@cloudhal/core-contracts"; import { Badge, Button, Link, SectionCard, SectionEmpty, Text } from "@cloudhal/ui"; import { Translate } from "@phosphor-icons/react"; import { useMutation, useQuery, useQueryClient } from "@tanstack/react-query"; import { useState } from "react"; import { useI18n } from "../i18n/index.tsx"; import { api, type TranslationRow } from "../lib/api.ts"; import { entryHref } from "../lib/entry-href.ts"; import { languageName } from "../lib/languages.ts"; import { ConfirmDialog } from "./ConfirmDialog.tsx"; import { Status } from "./Status.tsx"; const METHOD_KEY = { original: "translations.method.original", human: "translations.method.human", machine: "translations.method.machine", "machine-reviewed": "translations.method.machineReviewed", } as const; /** Where a version is edited in Studio. */ function editorHref(siteId: string, row: TranslationRow): string { return row.kind === "page" ? `/sites/${siteId}/pages/${row.id}` : entryHref(siteId, row.id, row.collection); } export function TranslationsCard({ siteId, node, dirty, }: { siteId: string; /** The saved node (the editor's query data): its `rev` is what the review sends. */ node: Page | ContentEntry; /** Unsaved edits: reviewing would race them, so it waits for a save. */ dirty: boolean; }) { const { t, locale } = useI18n(); const qc = useQueryClient(); const [confirm, setConfirm] = useState(false); const key = ["translations", siteId, node.id, node.rev]; const group = useQuery({ queryKey: key, queryFn: () => api.translations.group(siteId, node.id) }); const review = useMutation({ mutationFn: () => api.translations.review(siteId, node.id, node.rev), onSuccess: async (saved) => { setConfirm(false); // The editor's query (`["page" | "entry", siteId, id]`) takes the reviewed node. qc.setQueryData(["sections" in saved ? "page" : "entry", siteId, saved.id], saved); await qc.invalidateQueries({ queryKey: ["translations", siteId] }); }, }); const name = (l: string) => languageName(locale, l); const rows = group.data?.items ?? []; const canReview = node.translation?.method === "machine" && !node.translation.review; return ( } actions={rows.length > 1 ? {rows.length} : undefined} id="translations" > {group.data === undefined ? ( {t("common.loading")} ) : rows.length < 2 ? ( } title={t("translations.none", { language: name(node.locale) })} description={t("translations.noneDescription")} /> ) : (
{rows.map((row) => (
{name(row.locale)}
{row.id === node.id ? ( {t("translations.current")} ) : ( {row.title} )} {row.method && {t(METHOD_KEY[row.method])}} {row.stale && {t("translations.stale")}} {row.sourceMissing && ( {t("translations.sourceMissing")} )}
))}
{canReview && (
{dirty && ( {t("translations.reviewSaveFirst")} )}
)}
)} review.mutate()} />
); }