import { useId, type FormEvent } from 'react'
import type { FieldDef } from '../../api/engines/registry'
import type { RoleDraft, Schema } from '../../api/engines/roles'
function Field({ field, value, onChange }: { field: FieldDef; value: string | boolean; onChange: (v: string | boolean) => void }) {
const helpId = useId()
const help = field.help ? (
{field.help}
) : null
const describedBy = field.help ? helpId : undefined
if (field.type === 'bool') {
return (
// "check" lays the box beside its text; a plain label in a form stacks them.
)
}
if (field.type === 'secret') {
const hint = 'Write-only. Leave blank to keep the stored value.'
return (
)
}
if (field.type === 'select') {
return (
)
}
if (field.type === 'textarea' || field.type === 'statements') {
return (
)
}
return (
)
}
export function RoleEditor({
kind,
noun = 'role',
showName = true,
nameLabel,
title: titleOverride,
rawJson = true,
submitLabel = 'Save',
draft,
pending,
error,
onChange,
onSubmit,
onCancel,
}: {
kind: Schema
/** What is being edited, for the form title: "role", "connection", ... */
noun?: string
/** False for single-endpoint configs that have no name. */
showName?: boolean
/** Label for the name field; defaults to " name". */
nameLabel?: string
/** Replaces the generated form title. */
title?: string
/** Show a raw JSON box when the schema has no fields (generic engines). False for forms that take no input. */
rawJson?: boolean
submitLabel?: string
draft: RoleDraft
pending: boolean
error: string | null
onChange: (d: RoleDraft) => void
onSubmit: () => void
onCancel: () => void
}) {
const title = titleOverride ?? (draft.isNew ? `New ${noun}` : showName ? `Edit ${draft.name}` : `Edit ${noun}`)
function submit(e: FormEvent) {
e.preventDefault()
onSubmit()
}
return (
)
}