22
33import { useMemo , type ReactNode } from "react" ;
44import { Schema } from "effect" ;
5+ import type {
6+ ProviderSettingsFormAnnotation ,
7+ ProviderSettingsFormControl ,
8+ } from "@t3tools/contracts" ;
59
610import { cn } from "../../lib/utils" ;
711import { DraftInput } from "../ui/draft-input" ;
812import { Input } from "../ui/input" ;
913import { Switch } from "../ui/switch" ;
1014import { Textarea } from "../ui/textarea" ;
11- import type {
12- ProviderClientDefinition ,
13- ProviderSettingsControl ,
14- ProviderSettingsFieldUi ,
15- } from "./providerDriverMeta" ;
15+ import type { ProviderClientDefinition } from "./providerDriverMeta" ;
1616
1717export interface ProviderSettingsFieldModel {
1818 readonly key : string ;
19- readonly control : ProviderSettingsControl ;
19+ readonly control : ProviderSettingsFormControl ;
2020 readonly label : string ;
2121 readonly description ?: string | undefined ;
2222 readonly placeholder ?: string | undefined ;
@@ -30,51 +30,62 @@ function titleizeFieldKey(key: string): string {
3030 . replace ( / ^ ./ , ( char ) => char . toUpperCase ( ) ) ;
3131}
3232
33- function fieldControlFromUi ( ui : ProviderSettingsFieldUi | undefined ) : ProviderSettingsControl {
34- return ui ?. control ?? "text" ;
33+ function readFieldAnnotations (
34+ fieldSchema : ProviderClientDefinition [ "settingsSchema" ] [ "fields" ] [ string ] ,
35+ ) {
36+ return Schema . resolveAnnotationsKey ( fieldSchema ) ?? Schema . resolveAnnotations ( fieldSchema ) ;
3537}
3638
3739function readFieldAnnotationString (
3840 fieldSchema : ProviderClientDefinition [ "settingsSchema" ] [ "fields" ] [ string ] ,
3941 key : "title" | "description" ,
4042) : string | undefined {
41- const annotations =
42- Schema . resolveAnnotationsKey ( fieldSchema ) ?? Schema . resolveAnnotations ( fieldSchema ) ;
43+ const annotations = readFieldAnnotations ( fieldSchema ) ;
4344 const value = annotations ?. [ key ] ;
4445 return typeof value === "string" ? value : undefined ;
4546}
4647
48+ function readProviderSettingsFormAnnotation (
49+ fieldSchema : ProviderClientDefinition [ "settingsSchema" ] [ "fields" ] [ string ] ,
50+ ) : ProviderSettingsFormAnnotation {
51+ const annotation = readFieldAnnotations ( fieldSchema ) ?. providerSettingsForm ;
52+ return annotation ?? { } ;
53+ }
54+
4755export function deriveProviderSettingsFields (
4856 definition : ProviderClientDefinition ,
4957) : ReadonlyArray < ProviderSettingsFieldModel > {
50- const schemaKeys = Object . keys ( definition . settingsSchema . fields ) ;
51- const schemaKeySet = new Set ( schemaKeys ) ;
52- const orderedKeys = [
53- ...( definition . settingsUi . order ?? [ ] ) . filter ( ( key ) => schemaKeySet . has ( key ) ) ,
54- ...schemaKeys . filter ( ( key ) => ! ( definition . settingsUi . order ?? [ ] ) . includes ( key ) ) ,
55- ] ;
58+ return Object . keys ( definition . settingsSchema . fields )
59+ . map ( ( key , index ) => ( { key, index } ) )
60+ . toSorted ( ( left , right ) => {
61+ const leftAnnotation = readProviderSettingsFormAnnotation (
62+ definition . settingsSchema . fields [ left . key ] ! ,
63+ ) ;
64+ const rightAnnotation = readProviderSettingsFormAnnotation (
65+ definition . settingsSchema . fields [ right . key ] ! ,
66+ ) ;
67+ return ( leftAnnotation . order ?? left . index ) - ( rightAnnotation . order ?? right . index ) ;
68+ } )
69+ . flatMap ( ( { key } ) => {
70+ const fieldSchema = definition . settingsSchema . fields [ key ] ! ;
71+ const formAnnotation = readProviderSettingsFormAnnotation ( fieldSchema ) ;
72+ if ( formAnnotation . hidden ) return [ ] ;
5673
57- return orderedKeys . flatMap ( ( key ) => {
58- const ui = definition . settingsUi . fields ?. [ key ] ;
59- if ( ui ?. hidden ) return [ ] ;
60- const fieldSchema = definition . settingsSchema . fields [ key ] ! ;
61- const annotatedTitle = readFieldAnnotationString ( fieldSchema , "title" ) ;
62- const annotatedDescription = readFieldAnnotationString ( fieldSchema , "description" ) ;
63- return [
64- {
65- key,
66- control : fieldControlFromUi ( ui ) ,
67- label : ui ?. label ?? annotatedTitle ?? titleizeFieldKey ( key ) ,
68- ...( ui ?. description !== undefined
69- ? { description : ui . description }
70- : annotatedDescription !== undefined
71- ? { description : annotatedDescription }
74+ const annotatedTitle = readFieldAnnotationString ( fieldSchema , "title" ) ;
75+ const annotatedDescription = readFieldAnnotationString ( fieldSchema , "description" ) ;
76+ return [
77+ {
78+ key,
79+ control : formAnnotation . control ?? "text" ,
80+ label : annotatedTitle ?? titleizeFieldKey ( key ) ,
81+ ...( annotatedDescription !== undefined ? { description : annotatedDescription } : { } ) ,
82+ ...( formAnnotation . placeholder !== undefined
83+ ? { placeholder : formAnnotation . placeholder }
7284 : { } ) ,
73- ...( ui ?. placeholder !== undefined ? { placeholder : ui . placeholder } : { } ) ,
74- clearWhenEmpty : ui ?. clearWhenEmpty ?? "omit" ,
75- } satisfies ProviderSettingsFieldModel ,
76- ] ;
77- } ) ;
85+ clearWhenEmpty : formAnnotation . clearWhenEmpty ?? "omit" ,
86+ } satisfies ProviderSettingsFieldModel ,
87+ ] ;
88+ } ) ;
7889}
7990
8091export function readProviderConfigString ( config : unknown , key : string ) : string {
0 commit comments