11"use client" ;
22
33import { useMemo , type ReactNode } from "react" ;
4+ import { Schema } from "effect" ;
45
56import { cn } from "../../lib/utils" ;
67import { DraftInput } from "../ui/draft-input" ;
@@ -33,6 +34,16 @@ function fieldControlFromUi(ui: ProviderSettingsFieldUi | undefined): ProviderSe
3334 return ui ?. control ?? "text" ;
3435}
3536
37+ function readFieldAnnotationString (
38+ fieldSchema : ProviderClientDefinition [ "settingsSchema" ] [ "fields" ] [ string ] ,
39+ key : "title" | "description" ,
40+ ) : string | undefined {
41+ const annotations =
42+ Schema . resolveAnnotationsKey ( fieldSchema ) ?? Schema . resolveAnnotations ( fieldSchema ) ;
43+ const value = annotations ?. [ key ] ;
44+ return typeof value === "string" ? value : undefined ;
45+ }
46+
3647export function deriveProviderSettingsFields (
3748 definition : ProviderClientDefinition ,
3849) : ReadonlyArray < ProviderSettingsFieldModel > {
@@ -46,12 +57,19 @@ export function deriveProviderSettingsFields(
4657 return orderedKeys . flatMap ( ( key ) => {
4758 const ui = definition . settingsUi . fields ?. [ key ] ;
4859 if ( ui ?. hidden ) return [ ] ;
60+ const fieldSchema = definition . settingsSchema . fields [ key ] ! ;
61+ const annotatedTitle = readFieldAnnotationString ( fieldSchema , "title" ) ;
62+ const annotatedDescription = readFieldAnnotationString ( fieldSchema , "description" ) ;
4963 return [
5064 {
5165 key,
5266 control : fieldControlFromUi ( ui ) ,
53- label : ui ?. label ?? titleizeFieldKey ( key ) ,
54- ...( ui ?. description !== undefined ? { description : ui . description } : { } ) ,
67+ label : ui ?. label ?? annotatedTitle ?? titleizeFieldKey ( key ) ,
68+ ...( ui ?. description !== undefined
69+ ? { description : ui . description }
70+ : annotatedDescription !== undefined
71+ ? { description : annotatedDescription }
72+ : { } ) ,
5573 ...( ui ?. placeholder !== undefined ? { placeholder : ui . placeholder } : { } ) ,
5674 clearWhenEmpty : ui ?. clearWhenEmpty ?? "omit" ,
5775 } satisfies ProviderSettingsFieldModel ,
@@ -111,6 +129,108 @@ function FieldFrame(props: {
111129 return < div className = "grid gap-1.5" > { props . children } </ div > ;
112130}
113131
132+ interface ProviderSettingsFieldRowProps {
133+ readonly field : ProviderSettingsFieldModel ;
134+ readonly value : unknown ;
135+ readonly idPrefix : string ;
136+ readonly variant : ProviderSettingsFormProps [ "variant" ] ;
137+ readonly onChange : ProviderSettingsFormProps [ "onChange" ] ;
138+ }
139+
140+ function ProviderSettingsFieldRow ( {
141+ field,
142+ value,
143+ idPrefix,
144+ variant,
145+ onChange,
146+ } : ProviderSettingsFieldRowProps ) {
147+ const inputId = `${ idPrefix } -${ field . key } ` ;
148+ const descriptionClassName =
149+ variant === "card"
150+ ? "mt-1 block text-xs text-muted-foreground"
151+ : "text-[11px] text-muted-foreground" ;
152+ const label = < span className = "text-xs font-medium text-foreground" > { field . label } </ span > ;
153+ const description = field . description ? (
154+ < span className = { descriptionClassName } > { field . description } </ span >
155+ ) : null ;
156+
157+ if ( field . control === "switch" ) {
158+ return (
159+ < FieldFrame variant = { variant } >
160+ < div className = "flex items-center justify-between gap-3" >
161+ < div className = "min-w-0" >
162+ { label }
163+ { description }
164+ </ div >
165+ < Switch
166+ checked = { readProviderConfigBoolean ( value , field . key ) }
167+ onCheckedChange = { ( checked ) =>
168+ onChange ( nextProviderConfigWithFieldValue ( value , field , Boolean ( checked ) ) )
169+ }
170+ aria-label = { field . label }
171+ />
172+ </ div >
173+ </ FieldFrame >
174+ ) ;
175+ }
176+
177+ if ( field . control === "textarea" ) {
178+ return (
179+ < FieldFrame variant = { variant } >
180+ < label htmlFor = { inputId } className = { cn ( variant === "card" && "block" ) } >
181+ { label }
182+ < Textarea
183+ id = { inputId }
184+ className = { cn ( variant === "card" && "mt-1.5" ) }
185+ value = { readProviderConfigString ( value , field . key ) }
186+ onChange = { ( event ) =>
187+ onChange ( nextProviderConfigWithFieldValue ( value , field , event . target . value ) )
188+ }
189+ placeholder = { field . placeholder }
190+ spellCheck = { false }
191+ />
192+ { description }
193+ </ label >
194+ </ FieldFrame >
195+ ) ;
196+ }
197+
198+ const type = field . control === "password" ? "password" : undefined ;
199+ return (
200+ < FieldFrame variant = { variant } >
201+ < label htmlFor = { inputId } className = { cn ( variant === "card" && "block" ) } >
202+ { label }
203+ { variant === "card" ? (
204+ < DraftInput
205+ id = { inputId }
206+ className = "mt-1.5"
207+ type = { type }
208+ autoComplete = { field . control === "password" ? "off" : undefined }
209+ value = { readProviderConfigString ( value , field . key ) }
210+ onCommit = { ( next ) => onChange ( nextProviderConfigWithFieldValue ( value , field , next ) ) }
211+ placeholder = { field . placeholder }
212+ spellCheck = { false }
213+ />
214+ ) : (
215+ < Input
216+ id = { inputId }
217+ className = "bg-background"
218+ type = { type }
219+ autoComplete = { field . control === "password" ? "off" : undefined }
220+ value = { readProviderConfigString ( value , field . key ) }
221+ onChange = { ( event ) =>
222+ onChange ( nextProviderConfigWithFieldValue ( value , field , event . target . value ) )
223+ }
224+ placeholder = { field . placeholder }
225+ spellCheck = { false }
226+ />
227+ ) }
228+ { description }
229+ </ label >
230+ </ FieldFrame >
231+ ) ;
232+ }
233+
114234export function ProviderSettingsForm ( {
115235 definition,
116236 value,
@@ -126,95 +246,16 @@ export function ProviderSettingsForm({
126246
127247 return (
128248 < >
129- { fields . map ( ( field ) => {
130- const inputId = `${ idPrefix } -${ field . key } ` ;
131- const descriptionClassName =
132- variant === "card"
133- ? "mt-1 block text-xs text-muted-foreground"
134- : "text-[11px] text-muted-foreground" ;
135- const label = < span className = "text-xs font-medium text-foreground" > { field . label } </ span > ;
136- const description = field . description ? (
137- < span className = { descriptionClassName } > { field . description } </ span >
138- ) : null ;
139-
140- if ( field . control === "switch" ) {
141- return (
142- < FieldFrame key = { field . key } variant = { variant } >
143- < div className = "flex items-center justify-between gap-3" >
144- < div className = "min-w-0" >
145- { label }
146- { description }
147- </ div >
148- < Switch
149- checked = { readProviderConfigBoolean ( value , field . key ) }
150- onCheckedChange = { ( checked ) =>
151- onChange ( nextProviderConfigWithFieldValue ( value , field , Boolean ( checked ) ) )
152- }
153- aria-label = { field . label }
154- />
155- </ div >
156- </ FieldFrame >
157- ) ;
158- }
159-
160- if ( field . control === "textarea" ) {
161- return (
162- < FieldFrame key = { field . key } variant = { variant } >
163- < label htmlFor = { inputId } className = { cn ( variant === "card" && "block" ) } >
164- { label }
165- < Textarea
166- id = { inputId }
167- className = { cn ( variant === "card" && "mt-1.5" ) }
168- value = { readProviderConfigString ( value , field . key ) }
169- onChange = { ( event ) =>
170- onChange ( nextProviderConfigWithFieldValue ( value , field , event . target . value ) )
171- }
172- placeholder = { field . placeholder }
173- spellCheck = { false }
174- />
175- { description }
176- </ label >
177- </ FieldFrame >
178- ) ;
179- }
180-
181- const type = field . control === "password" ? "password" : undefined ;
182- return (
183- < FieldFrame key = { field . key } variant = { variant } >
184- < label htmlFor = { inputId } className = { cn ( variant === "card" && "block" ) } >
185- { label }
186- { variant === "card" ? (
187- < DraftInput
188- id = { inputId }
189- className = "mt-1.5"
190- type = { type }
191- autoComplete = { field . control === "password" ? "off" : undefined }
192- value = { readProviderConfigString ( value , field . key ) }
193- onCommit = { ( next ) =>
194- onChange ( nextProviderConfigWithFieldValue ( value , field , next ) )
195- }
196- placeholder = { field . placeholder }
197- spellCheck = { false }
198- />
199- ) : (
200- < Input
201- id = { inputId }
202- className = "bg-background"
203- type = { type }
204- autoComplete = { field . control === "password" ? "off" : undefined }
205- value = { readProviderConfigString ( value , field . key ) }
206- onChange = { ( event ) =>
207- onChange ( nextProviderConfigWithFieldValue ( value , field , event . target . value ) )
208- }
209- placeholder = { field . placeholder }
210- spellCheck = { false }
211- />
212- ) }
213- { description }
214- </ label >
215- </ FieldFrame >
216- ) ;
217- } ) }
249+ { fields . map ( ( field ) => (
250+ < ProviderSettingsFieldRow
251+ key = { field . key }
252+ field = { field }
253+ value = { value }
254+ idPrefix = { idPrefix }
255+ variant = { variant }
256+ onChange = { onChange }
257+ />
258+ ) ) }
218259 </ >
219260 ) ;
220261}
0 commit comments