'use client' import { useState } from 'react' import { Loader2, Plus, RotateCcw, Trash2 } from 'lucide-react' import { useTranslation } from 'react-i18next' import { Dialog } from '@/shared/ui/Dialog' import { usePageCustomization } from '@/context/PageCustomizationContext' import { DEFAULT_PAGE_CUSTOMIZATION, STARTER_GROUPS, starterPrompts, toggleHidden, type PageCustomization, type StarterGroup, type ComposerControls, } from '@/lib/page-customization' import type { CapabilityDef } from '@/features/capabilities/presentation' import { useCapabilityLabel } from '@/features/ui-layout/tokens' import { LEARNING_SURFACES } from '@/components/learning/surfaces' const inputClass = 'w-full rounded-lg border border-border bg-background px-3 py-2 text-sm outline-none transition-shadow focus:border-ring focus:ring-2 focus:ring-ring/15' const actionClass = 'inline-flex items-center justify-center gap-2 rounded-lg px-3 py-2 text-xs font-medium transition-colors focus-visible:outline focus-visible:outline-2 focus-visible:outline-ring disabled:opacity-50' export function ChoiceRow({ label, description, checked, onChange, }: { label: string description?: string checked: boolean onChange: () => void }) { return ( {label} {description && ( {description} )} ) } export default function PageCustomizeDialog({ page, initial, capabilities, learningKinds, onClose, }: { page: 'chat' | 'learning' initial: PageCustomization capabilities: CapabilityDef[] learningKinds?: string[] onClose: () => void }) { const { t } = useTranslation() const capabilityLabel = useCapabilityLabel() const { save, loading, error: loadError, reload } = usePageCustomization() const [draft, setDraft] = useState(() => structuredClone(initial)) const [tab, setTab] = useState<'modes' | 'controls' | 'starters'>('modes') const [group, setGroup] = useState('explain') const [saving, setSaving] = useState(false) const [error, setError] = useState(false) const prompts = starterPrompts(draft.chat, group, t) const invalid = Object.values(draft.chat.starters).some(rows => rows?.some(row => !row.label.trim() || !row.prompt.trim()) ) const updatePrompts = (rows: typeof prompts) => setDraft(previous => ({ ...previous, chat: { ...previous.chat, starters: { ...previous.chat.starters, [group]: rows } }, })) async function commit() { setSaving(true) setError(false) try { await save(page, draft[page]) onClose() } catch { setError(true) setSaving(false) } } return ( setDraft(previous => ({ ...previous, [page]: structuredClone(DEFAULT_PAGE_CUSTOMIZATION[page]), })) } > {t('customize.reset')} {t('Cancel')} void commit()} className={`${actionClass} bg-primary text-primary-foreground hover:opacity-90`} > {saving && } {t(saving ? 'Saving...' : 'Save')} } > {(error || loadError) && ( {t('customize.saveError')} {loadError && ( {t('Retry')} )} )} {loading ? ( ) : ( <> {page === 'chat' ? ( <> {(['modes', 'controls', 'starters'] as const).map(item => ( setTab(item)} className={`flex-1 rounded-lg px-3 py-2 text-sm transition-colors ${tab === item ? 'bg-background text-foreground shadow-sm' : 'text-muted-foreground hover:text-foreground'}`} > {t(`customize.${item}`)} ))} {tab === 'modes' && ( {t('customize.modesHint')} {capabilities .filter(cap => cap.value && !cap.legacy) .map(cap => ( setDraft(previous => ({ ...previous, chat: { ...previous.chat, hidden_modes: toggleHidden(previous.chat.hidden_modes, cap.value), }, })) } /> ))} )} {tab === 'controls' && ( {( [ 'skills', 'mcp', 'partners', 'preferences', 'voice', ] as (keyof ComposerControls)[] ).map(key => ( setDraft(previous => ({ ...previous, chat: { ...previous.chat, controls: { ...previous.chat.controls, [key]: !previous.chat.controls[key], }, }, })) } /> ))} )} {tab === 'starters' && ( <> {STARTER_GROUPS.map(key => ( setGroup(key)} className={`rounded-full px-3 py-1.5 text-xs transition-colors ${group === key ? 'bg-primary text-primary-foreground' : 'bg-muted/60 text-muted-foreground hover:bg-muted'}`} > {t(`starters.${key}`)} ))} {prompts.map((row, index) => ( {t('customize.promptLabel')} updatePrompts( prompts.map((item, i) => i === index ? { ...item, label: event.target.value } : item ) ) } /> updatePrompts(prompts.filter((_, i) => i !== index))} className="mt-6 rounded-lg p-2 text-muted-foreground transition-colors hover:bg-destructive/10 hover:text-destructive" > {t('customize.promptText')} updatePrompts( prompts.map((item, i) => i === index ? { ...item, prompt: event.target.value } : item ) ) } /> ))} {!prompts.length && ( {t('customize.emptyPrompts')} )} = 20} onClick={() => updatePrompts([...prompts, { label: '', prompt: '' }])} className={`${actionClass} border border-border hover:bg-muted`} > {t('customize.addPrompt')} setDraft(previous => { const starters = { ...previous.chat.starters } delete starters[group] return { ...previous, chat: { ...previous.chat, starters } } }) } > {t('customize.resetGroup')} {invalid && ( {t('customize.requiredPrompts')} )} > )} > ) : ( {t('customize.learningHint')} {LEARNING_SURFACES.filter( surface => !learningKinds || learningKinds.includes(surface.kind) ).map(surface => ( setDraft(previous => ({ ...previous, learning: { hidden_features: toggleHidden( previous.learning.hidden_features, surface.kind ), }, })) } /> ))} )} > )} ) }
{t('customize.modesHint')}
{t('customize.emptyPrompts')}
{t('customize.requiredPrompts')}
{t('customize.learningHint')}