"use client"; import { useState } from "react"; import { toast } from "sonner"; import { Button } from "@/components/ui/button"; import { Dialog, DialogContent, DialogDescription, DialogFooter, DialogHeader, DialogTitle, } from "@/components/ui/dialog"; import { Input } from "@/components/ui/input"; import { Label } from "@/components/ui/label"; import { Textarea } from "@/components/ui/textarea"; import { useUpdateWiki } from "@/hooks/use-wikis"; import { useI18n } from "@/i18n/i18n-context"; import type { UpdateWikiRequest, Wiki, WikiScope } from "@/types/wiki"; const MAX_WIKI_NAME_BYTES = 256; const MAX_WIKI_SUMMARY_BYTES = 4096; type WikiFieldValidationError = "nameRequired" | "nameTooLong" | "summaryTooLong"; function utf8Length(value: string): number { return new TextEncoder().encode(value).byteLength; } function validateWikiFields( name: string, summary: string, ): WikiFieldValidationError | null { if (!name.trim()) return "nameRequired"; if (utf8Length(name.trim()) > MAX_WIKI_NAME_BYTES) return "nameTooLong"; if (utf8Length(summary) > MAX_WIKI_SUMMARY_BYTES) return "summaryTooLong"; return null; } export function buildWikiUpdateChanges( wiki: Pick, name: string, summary: string, ): Omit { const changes: Omit = {}; const normalizedName = name.trim(); if (normalizedName !== wiki.name) changes.name = normalizedName; if (summary !== (wiki.summary ?? "")) changes.summary = summary; return changes; } export function buildWikiUpdateVariables( memoryStoreName: string, scope: WikiScope, wiki: Pick, name: string, summary: string, ) { return { memoryStoreName, scope, ...buildWikiUpdateChanges(wiki, name, summary), }; } interface WikiEditDialogProps { memoryStoreName: string; scope: WikiScope; wiki: Wiki; open: boolean; onOpenChange: (open: boolean) => void; } export function WikiEditDialog({ memoryStoreName, scope, wiki, open, onOpenChange, }: WikiEditDialogProps) { const { t } = useI18n(); const updateWiki = useUpdateWiki(); const [validationError, setValidationError] = useState(null); async function handleSubmit(event: React.FormEvent) { event.preventDefault(); if (updateWiki.isPending) return; const formData = new FormData(event.currentTarget); const name = String(formData.get("name") ?? "").trim(); const summary = String(formData.get("summary") ?? ""); const error = validateWikiFields(name, summary); if (error) { setValidationError(error); return; } const variables = buildWikiUpdateVariables( memoryStoreName, scope, wiki, name, summary, ); if (Object.keys(variables).length === 2) { onOpenChange(false); return; } try { await updateWiki.mutateAsync(variables); toast.success(t("wiki.edit.success")); onOpenChange(false); } catch (error) { toast.error( error instanceof Error ? error.message : t("wiki.edit.failed"), ); } } const validationMessage = validationError ? t( validationError === "nameRequired" ? "wiki.edit.nameRequired" : validationError === "nameTooLong" ? "wiki.edit.nameTooLong" : "wiki.edit.summaryTooLong", ) : null; return ( { if (!nextOpen) setValidationError(null); onOpenChange(nextOpen); }} > {t("wiki.edit.title")} {t("wiki.edit.description")}
setValidationError(null)} />