1- import type { EnvironmentId } from "@t3tools/contracts" ;
1+ import {
2+ isSecretReference ,
3+ type EnvironmentId ,
4+ type SecretReference ,
5+ type SettingsSecretValue ,
6+ } from "@t3tools/contracts" ;
27import { ExternalLinkIcon } from "lucide-react" ;
38import { useState } from "react" ;
49
@@ -12,8 +17,11 @@ import { serverEnvironment } from "../../state/server";
1217import { useAtomCommand } from "../../state/use-atom-command" ;
1318import { Button , InlineButton } from "../ui/button" ;
1419import { Input } from "../ui/input" ;
15- import { SecretInput } from "./SecretInput" ;
1620import { Label } from "../ui/label" ;
21+ import { SecretSourceInput , useSecretSources } from "./SecretSourceInput" ;
22+
23+ const hasToken = ( token : SettingsSecretValue | undefined ) =>
24+ typeof token === "string" ? token . length > 0 : token !== undefined ;
1725
1826export function SourceControlTokenSettings ( {
1927 environmentId,
@@ -34,15 +42,24 @@ export function SourceControlTokenSettings({
3442 } ) ;
3543 const defaultHost = definition . publicHost ?? "" ;
3644 const [ host , setHost ] = useState ( defaultHost ) ;
37- const [ draft , setDraft ] = useState ( "" ) ;
45+ const secretSources = useSecretSources ( environmentId ) ;
46+ const [ pending , setPending ] = useState < {
47+ readonly key : string ;
48+ readonly draft : string | SecretReference ;
49+ } | null > ( null ) ;
3850 const [ saving , setSaving ] = useState ( false ) ;
3951 const normalizedHost = host . trim ( ) . toLowerCase ( ) ;
40- const isSaved = ( tokens [ normalizedHost ] ?? "" ) . length > 0 ;
52+ const savedToken = tokens [ normalizedHost ] ;
53+ const isSaved = hasToken ( savedToken ) ;
4154 const savedHosts = Object . entries ( tokens )
42- . filter ( ( [ , value ] ) => value . length > 0 )
55+ . filter ( ( [ , value ] ) => hasToken ( value ) )
4356 . map ( ( [ saved ] ) => saved ) ;
57+ const sourceKey = JSON . stringify ( [ normalizedHost , savedToken ?? "" ] ) ;
58+ const draft =
59+ pending ?. key === sourceKey ? pending . draft : isSecretReference ( savedToken ) ? savedToken : "" ;
60+ const submission = typeof draft === "string" ? draft . trim ( ) : draft ;
4461
45- const save = async ( target : string , token : string ) => {
62+ const save = async ( target : string , token : SettingsSecretValue ) => {
4663 setSaving ( true ) ;
4764 const result = await updateSettings ( {
4865 environmentId,
@@ -55,7 +72,7 @@ export function SourceControlTokenSettings({
5572 } ) ;
5673 setSaving ( false ) ;
5774 if ( result . _tag === "Success" ) {
58- setDraft ( "" ) ;
75+ setPending ( null ) ;
5976 onSaved ( ) ;
6077 }
6178 } ;
@@ -65,7 +82,7 @@ export function SourceControlTokenSettings({
6582 className = "grid gap-3"
6683 onSubmit = { ( event ) => {
6784 event . preventDefault ( ) ;
68- if ( normalizedHost && draft . trim ( ) ) void save ( normalizedHost , draft . trim ( ) ) ;
85+ if ( normalizedHost && submission !== "" ) void save ( normalizedHost , submission ) ;
6986 } }
7087 >
7188 { /* Locked while saving: a successful save clears the draft, which would drop edits made mid-request. */ }
@@ -106,12 +123,15 @@ export function SourceControlTokenSettings({
106123 </ div >
107124 < div className = "grid gap-1.5" >
108125 < Label htmlFor = { `${ definition . kind } -token-${ environmentId } ` } > Token</ Label >
109- < SecretInput
126+ < SecretSourceInput
110127 id = { `${ definition . kind } -token-${ environmentId } ` }
111128 size = "sm"
112- configured = { isSaved }
129+ aria-label = { `${ definition . label } token` }
130+ configured = { isSaved && ! isSecretReference ( savedToken ) }
113131 value = { draft }
114- onChange = { ( event ) => setDraft ( event . target . value ) }
132+ sources = { secretSources }
133+ onChange = { ( event ) => setPending ( { key : sourceKey , draft : event . target . value } ) }
134+ onSecretChange = { ( secret ) => setPending ( { key : sourceKey , draft : secret ?? "" } ) }
115135 />
116136 </ div >
117137 </ div >
@@ -132,7 +152,11 @@ export function SourceControlTokenSettings({
132152 Remove
133153 </ Button >
134154 ) : null }
135- < Button type = "submit" size = "xs" disabled = { ! normalizedHost || ! draft . trim ( ) || saving } >
155+ < Button
156+ type = "submit"
157+ size = "xs"
158+ disabled = { ! normalizedHost || submission === "" || saving }
159+ >
136160 Save
137161 </ Button >
138162 </ div >
0 commit comments