"use client" // The enablement controls of the organization's settings page: the current state, the enable // button with its typed refusal reasons (plan, policy, quota), and disable — which preserves // every collection, document and vector. import { useTransport } from "@conusai-kit/ui" import { type ReactElement, useId, useState } from "react" export interface SettingsSectionLabels { readonly heading: string readonly state: string readonly stateOn: string readonly stateOff: string readonly backend: string readonly backendVectors: string readonly source: string readonly sourceOperator: string readonly sourceManaged: string readonly endpoint: string readonly enable: string readonly enabling: string readonly disable: string readonly disabling: string readonly enabledDone: string readonly disabledDone: string readonly planRequired: string readonly featureDisabled: string readonly quotaExhausted: string readonly upgrade: string readonly hint: string readonly failed: string } type View = { kind: "idle" } | { kind: "busy" } | { kind: "message"; text: string; alert: boolean } export function SettingsSection({ org, initial, labels, }: { org: string initial: { enabled: boolean; backend: string | null; source: string | null; url: string | null } labels: SettingsSectionLabels }): ReactElement { const { call, describe } = useTransport() const id = useId() const [enabled, setEnabled] = useState(initial.enabled) const [view, setView] = useState({ kind: "idle" }) const enable = async (): Promise => { setView({ kind: "busy" }) try { await call("knowledge.settings.enableDocumentAi", { org }) setEnabled(true) setView({ kind: "message", text: labels.enabledDone, alert: false }) } catch (error) { const code = (error as { code?: string }).code ?? "unknown" if (code === "plan_required") { setView({ kind: "message", text: `${labels.planRequired} ${labels.upgrade}`, alert: true }) } else if (code === "quota_exhausted") { setView({ kind: "message", text: labels.quotaExhausted, alert: true }) } else if (code === "feature_disabled") { setView({ kind: "message", text: labels.featureDisabled, alert: true }) } else { setView({ kind: "message", text: describe(error), alert: true }) } } } const disable = async (): Promise => { setView({ kind: "busy" }) try { await call("knowledge.settings.disableDocumentAi", { org }) setEnabled(false) setView({ kind: "message", text: labels.disabledDone, alert: false }) } catch (error) { setView({ kind: "message", text: describe(error), alert: true }) } } return (

{labels.heading}

{labels.hint}

{labels.state}
{enabled ? labels.stateOn : labels.stateOff}
{initial.backend !== null ? ( <>
{labels.backend}
{initial.backend === "vectors" ? labels.backendVectors : initial.backend}
) : null} {initial.source !== null ? ( <>
{labels.source}
{initial.source === "operator" ? labels.sourceOperator : labels.sourceManaged}
) : null} {initial.url !== null ? ( <>
{labels.endpoint}
{initial.url}
) : null}
{view.kind === "message" ?

{view.text}

: null} {enabled ? ( ) : ( )}
) }