feat(cms): copyable legal validation prompt in the legal page editor (MITHOME-113)
buildLegalValidationPrompt() is the single review template: the criteria (meaning, GDPR terminology, obligation strength, preserved markers, clarity), a severity table output with a final verdict, and the Hungarian/English text of every section paired by id, missing English flagged as high severity. The legal page sidebar shows it through the LegalValidationPrompt UI field (hu + en read from REST with draft=true and no locale fallback) with copy and refresh buttons. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
co-authored by
Claude Opus 5.5
parent
329e2e3403
commit
acd1ff2ed9
@@ -0,0 +1,105 @@
|
||||
'use client'
|
||||
|
||||
import { useEffect, useState } from 'react'
|
||||
import { buildLegalValidationPrompt, type LegalText } from '@/lib/legal-validation-prompt'
|
||||
|
||||
/**
|
||||
* MITHOME-113 (design D8) — "Validation prompt" panel in the legal page
|
||||
* editor sidebar: a ready-to-copy prompt for reviewing the English
|
||||
* translation with a high-capability model. The prompt comes from the same
|
||||
* template the project's own validation uses (src/lib/legal-validation-prompt.ts).
|
||||
*
|
||||
* WHY REST reads instead of the form state: the edit form holds one locale
|
||||
* at a time; the prompt needs both. `draft=true` reads the latest saved
|
||||
* version, `fallback-locale=none` keeps missing English empty instead of
|
||||
* silently showing Hungarian as "English".
|
||||
* WHY the id comes from the URL: avoids adding @payloadcms/ui as a direct
|
||||
* dependency just for useDocumentInfo (same approach as QuickSearch).
|
||||
*/
|
||||
|
||||
type State =
|
||||
| { kind: 'loading' }
|
||||
| { kind: 'unsaved' }
|
||||
| { kind: 'error'; message: string }
|
||||
| { kind: 'empty' }
|
||||
| { kind: 'ready'; prompt: string }
|
||||
|
||||
function docIdFromPath(pathname: string): string | undefined {
|
||||
const id = pathname.match(/\/collections\/legal-pages\/([^/?#]+)/)?.[1]
|
||||
return id && id !== 'create' ? id : undefined
|
||||
}
|
||||
|
||||
async function readLocale(id: string, locale: 'hu' | 'en'): Promise<LegalText> {
|
||||
const res = await fetch(`/api/legal-pages/${id}?locale=${locale}&fallback-locale=none&draft=true&depth=0`, {
|
||||
credentials: 'include',
|
||||
})
|
||||
if (!res.ok) throw new Error(`HTTP ${res.status}`)
|
||||
return res.json()
|
||||
}
|
||||
|
||||
async function fetchPromptState(): Promise<State> {
|
||||
const id = docIdFromPath(window.location.pathname)
|
||||
if (!id) return { kind: 'unsaved' }
|
||||
try {
|
||||
const [hu, en] = await Promise.all([readLocale(id, 'hu'), readLocale(id, 'en')])
|
||||
const prompt = buildLegalValidationPrompt(hu, en)
|
||||
return prompt ? { kind: 'ready', prompt } : { kind: 'empty' }
|
||||
} catch (error) {
|
||||
return { kind: 'error', message: error instanceof Error ? error.message : String(error) }
|
||||
}
|
||||
}
|
||||
|
||||
export function LegalValidationPrompt() {
|
||||
const [state, setState] = useState<State>({ kind: 'loading' })
|
||||
const [copied, setCopied] = useState(false)
|
||||
|
||||
useEffect(() => {
|
||||
let active = true
|
||||
void fetchPromptState().then((next) => active && setState(next))
|
||||
return () => {
|
||||
active = false
|
||||
}
|
||||
}, [])
|
||||
|
||||
const reload = () => {
|
||||
setState({ kind: 'loading' })
|
||||
void fetchPromptState().then(setState)
|
||||
}
|
||||
|
||||
const copy = async (prompt: string) => {
|
||||
await navigator.clipboard.writeText(prompt)
|
||||
setCopied(true)
|
||||
setTimeout(() => setCopied(false), 2000)
|
||||
}
|
||||
|
||||
return (
|
||||
<div className="field-type" style={{ marginBottom: 'var(--base)' }}>
|
||||
<label className="field-label">Validációs prompt (angol fordítás)</label>
|
||||
<p style={{ color: 'var(--theme-elevation-500)', fontSize: '0.8rem', margin: '0 0 0.5rem' }}>
|
||||
Másold be egy magas szintű modellbe (legalább kettőbe, egymástól függetlenül), majd rögzítsd az eredményt a
|
||||
„Jogi validáció” mezőkben. A prompt a legutóbb mentett változatból készül.
|
||||
</p>
|
||||
{state.kind === 'loading' && <p>Betöltés…</p>}
|
||||
{state.kind === 'unsaved' && <p>Előbb mentsd el a dokumentumot.</p>}
|
||||
{state.kind === 'empty' && <p>Még nincs angol szöveg — nincs mit validálni.</p>}
|
||||
{state.kind === 'error' && <p role="alert">A prompt nem tölthető be ({state.message}).</p>}
|
||||
{state.kind === 'ready' && (
|
||||
<>
|
||||
<textarea
|
||||
readOnly
|
||||
value={state.prompt}
|
||||
rows={8}
|
||||
aria-label="Validációs prompt"
|
||||
style={{ width: '100%', fontFamily: 'monospace', fontSize: '0.75rem' }}
|
||||
/>
|
||||
<button type="button" className="btn btn--style-primary btn--size-small" onClick={() => void copy(state.prompt)}>
|
||||
{copied ? 'Másolva ✓' : 'Prompt másolása'}
|
||||
</button>
|
||||
</>
|
||||
)}
|
||||
<button type="button" className="btn btn--style-secondary btn--size-small" onClick={reload}>
|
||||
Frissítés
|
||||
</button>
|
||||
</div>
|
||||
)
|
||||
}
|
||||
Reference in New Issue
Block a user