11"use client" ;
22
3- import { CheckIcon } from "lucide-react" ;
43import { Radio as RadioPrimitive } from "@base-ui/react/radio" ;
5- import { useCallback , useMemo , useState } from "react" ;
4+ import { useMemo , useState } from "react" ;
65import {
76 ProviderInstanceId ,
87 ProviderDriverKind ,
@@ -29,6 +28,12 @@ import { toastManager } from "../ui/toast";
2928import { DRIVER_OPTION_BY_VALUE , DRIVER_OPTIONS } from "./providerDriverMeta" ;
3029import { ProviderSettingsForm , deriveProviderSettingsFields } from "./ProviderSettingsForm" ;
3130import { AnimatedHeight } from "../AnimatedHeight" ;
31+ import {
32+ ADD_PROVIDER_WIZARD_STEPS ,
33+ resolveWizardNavigation ,
34+ type WizardNavigation ,
35+ } from "./AddProviderInstanceDialog.logic" ;
36+ import { AddProviderInstanceWizardSteps } from "./AddProviderInstanceWizardSteps" ;
3237
3338const PROVIDER_ACCENT_SWATCHES = [
3439 "#2563eb" ,
@@ -108,30 +113,6 @@ function validateInstanceId(id: string, existing: ReadonlySet<string>): string |
108113 return null ;
109114}
110115
111- export type WizardAdvance =
112- | { readonly kind : "advance" ; readonly step : number }
113- | { readonly kind : "blocked" ; readonly error : string } ;
114-
115- /**
116- * Decide what clicking "Next" should do in the add-instance wizard. The
117- * Identity step (index 1) owns the Instance ID, so the wizard must not advance
118- * past it while the id is invalid — otherwise the user reaches the final step
119- * only for "Add instance" to silently no-op. Returns either the next step to
120- * move to, or the error that blocks advancing (mirrors the gate `handleSave`
121- * applies before submit).
122- */
123- export function advanceInstanceWizard (
124- currentStep : number ,
125- stepCount : number ,
126- validation : { readonly instanceIdError : string | null } ,
127- ) : WizardAdvance {
128- const blockingError = currentStep === 1 ? validation . instanceIdError : null ;
129- if ( blockingError !== null ) {
130- return { kind : "blocked" , error : blockingError } ;
131- }
132- return { kind : "advance" , step : Math . min ( stepCount - 1 , currentStep + 1 ) } ;
133- }
134-
135116interface AddProviderInstanceDialogProps {
136117 open : boolean ;
137118 onOpenChange : ( open : boolean ) => void ;
@@ -167,26 +148,37 @@ export function AddProviderInstanceDialog({ open, onOpenChange }: AddProviderIns
167148 const instanceIdError = validateInstanceId ( instanceId , existingIds ) ;
168149 const showInstanceIdError = hasAttemptedSubmit && instanceIdError !== null ;
169150 const previewLabel = label . trim ( ) || `${ driverOption . label } Workspace` ;
170- const wizardSteps = [ "Driver" , "Identity" , "Config" ] as const ;
171151 const wizardStepSummaries = [ driverOption . label , previewLabel , null ] as const ;
172152
173153 const configDraft = configByDriver [ driver ] ?? EMPTY_CONFIG_DRAFT ;
174- const setConfigDraft = useCallback (
175- ( config : Record < string , unknown > | undefined ) => {
176- setConfigByDriver ( ( existing ) => {
177- const next = { ...existing } ;
178- if ( config === undefined || Object . keys ( config ) . length === 0 ) {
179- delete next [ driver ] ;
180- } else {
181- next [ driver ] = config ;
182- }
183- return next ;
184- } ) ;
185- } ,
186- [ driver ] ,
187- ) ;
154+ const setConfigDraft = ( config : Record < string , unknown > | undefined ) => {
155+ setConfigByDriver ( ( existing ) => {
156+ const next = { ...existing } ;
157+ if ( config === undefined || Object . keys ( config ) . length === 0 ) {
158+ delete next [ driver ] ;
159+ } else {
160+ next [ driver ] = config ;
161+ }
162+ return next ;
163+ } ) ;
164+ } ;
165+
166+ const applyWizardNavigation = ( navigation : WizardNavigation ) => {
167+ if ( navigation . kind === "blocked" ) {
168+ setHasAttemptedSubmit ( true ) ;
169+ }
170+ setWizardStep ( navigation . step ) ;
171+ } ;
172+
173+ const navigateToStep = ( requestedStep : number ) => {
174+ applyWizardNavigation (
175+ resolveWizardNavigation ( wizardStep , requestedStep , ADD_PROVIDER_WIZARD_STEPS . length , {
176+ instanceIdError,
177+ } ) ,
178+ ) ;
179+ } ;
188180
189- const handleSave = useCallback ( ( ) => {
181+ const handleSave = ( ) => {
190182 setHasAttemptedSubmit ( true ) ;
191183 if ( instanceIdError !== null ) return ;
192184
@@ -225,18 +217,7 @@ export function AddProviderInstanceDialog({ open, onOpenChange }: AddProviderIns
225217 description : error instanceof Error ? error . message : "Update failed." ,
226218 } ) ;
227219 }
228- } , [
229- driver ,
230- driverOption ,
231- configByDriver ,
232- instanceId ,
233- instanceIdError ,
234- label ,
235- accentColor ,
236- onOpenChange ,
237- settings . providerInstances ,
238- updateSettings ,
239- ] ) ;
220+ } ;
240221
241222 return (
242223 < Dialog open = { open } onOpenChange = { onOpenChange } >
@@ -248,46 +229,12 @@ export function AddProviderInstanceDialog({ open, onOpenChange }: AddProviderIns
248229 Configure an additional provider instance — for example, a second Codex install
249230 pointed at a different workspace.
250231 </ DialogDescription >
251- < div className = "grid grid-cols-3 gap-2" >
252- { wizardSteps . map ( ( step , index ) => (
253- < button
254- key = { step }
255- type = "button"
256- className = { cn (
257- "grid min-w-0 grid-cols-[1rem_minmax(0,1fr)] gap-x-2 rounded-lg border px-3 py-2 text-left" ,
258- index === wizardStep
259- ? "border-primary bg-primary/10 ring-1 ring-primary/25"
260- : index < wizardStep
261- ? "border-border bg-background"
262- : "border-border bg-muted/40" ,
263- ) }
264- onClick = { ( ) => setWizardStep ( index ) }
265- >
266- < span
267- className = { cn (
268- "row-span-2 mt-0.5 grid size-4 place-items-center rounded-full border" ,
269- index < wizardStep
270- ? "border-primary bg-primary text-primary-foreground"
271- : index === wizardStep
272- ? "border-primary bg-background"
273- : "border-muted-foreground/35 bg-background" ,
274- ) }
275- aria-hidden
276- >
277- { index < wizardStep ? < CheckIcon className = "size-3" /> : null }
278- </ span >
279- < span className = "text-[10px] font-medium uppercase text-muted-foreground" >
280- Step { index + 1 }
281- </ span >
282- < span className = "truncate text-xs font-semibold text-foreground" >
283- { step }
284- { index < wizardStep && wizardStepSummaries [ index ]
285- ? `: ${ wizardStepSummaries [ index ] } `
286- : "" }
287- </ span >
288- </ button >
289- ) ) }
290- </ div >
232+ < AddProviderInstanceWizardSteps
233+ currentStep = { wizardStep }
234+ summaries = { wizardStepSummaries }
235+ instanceIdError = { instanceIdError }
236+ onNavigation = { applyWizardNavigation }
237+ />
291238 </ DialogHeader >
292239
293240 < div
@@ -476,20 +423,8 @@ export function AddProviderInstanceDialog({ open, onOpenChange }: AddProviderIns
476423 >
477424 { wizardStep === 0 ? "Cancel" : "Back" }
478425 </ Button >
479- { wizardStep < wizardSteps . length - 1 ? (
480- < Button
481- size = "sm"
482- onClick = { ( ) => {
483- const advance = advanceInstanceWizard ( wizardStep , wizardSteps . length , {
484- instanceIdError,
485- } ) ;
486- if ( advance . kind === "blocked" ) {
487- setHasAttemptedSubmit ( true ) ;
488- return ;
489- }
490- setWizardStep ( advance . step ) ;
491- } }
492- >
426+ { wizardStep < ADD_PROVIDER_WIZARD_STEPS . length - 1 ? (
427+ < Button size = "sm" onClick = { ( ) => navigateToStep ( wizardStep + 1 ) } >
493428 Next
494429 </ Button >
495430 ) : (
0 commit comments