@@ -3,7 +3,7 @@ import { zodResolver } from '@hookform/resolvers/zod'
33import { Box , Button , Dialog , Flex , ScrollArea , Tabs } from '@radix-ui/themes'
44import { ExclamationTriangleIcon } from '@radix-ui/react-icons'
55import { findIndex , sortBy } from 'lodash-es'
6- import { useState } from 'react'
6+ import { useEffect , useState } from 'react'
77import { FormProvider , useForm } from 'react-hook-form'
88
99import { ProxySettings } from './ProxySettings'
@@ -16,8 +16,13 @@ import { AppearanceSettings } from './AppearanceSettings'
1616import { LogsSettings } from './LogsSettings'
1717import { useStudioUIStore } from '@/store/ui'
1818import { useSaveSettings , useSettings } from '@/hooks/useSettings'
19+ import { SettingsTabValue } from './types'
1920
20- const tabs = [
21+ const tabs : Array < {
22+ label : string
23+ value : SettingsTabValue
24+ component : ( ) => React . ReactNode
25+ } > = [
2126 { label : 'Proxy' , value : 'proxy' , component : ProxySettings } ,
2227 { label : 'Recorder' , value : 'recorder' , component : RecorderSettings } ,
2328 {
@@ -39,12 +44,19 @@ const tabs = [
3944
4045export const SettingsDialog = ( ) => {
4146 const { data : settings } = useSettings ( )
42- const isOpen = useStudioUIStore ( ( state ) => state . isSettingsDialogOpen )
43- const setIsOpen = useStudioUIStore ( ( state ) => state . setIsSettingsDialogOpen )
47+ const {
48+ isSettingsDialogOpen : isOpen ,
49+ closeSettingsDialog,
50+ selectedSettingsTab,
51+ } = useStudioUIStore ( ( state ) => state )
4452 const { mutateAsync : saveSettings , isPending } = useSaveSettings ( ( ) => {
45- setIsOpen ( false )
53+ closeSettingsDialog ( )
4654 } )
47- const [ selectedTab , setSelectedTab ] = useState ( 'proxy' )
55+ const [ selectedTab , setSelectedTab ] = useState ( selectedSettingsTab )
56+
57+ useEffect ( ( ) => {
58+ setSelectedTab ( selectedSettingsTab )
59+ } , [ isOpen , selectedSettingsTab ] )
4860
4961 const formMethods = useForm < AppSettings > ( {
5062 resolver : zodResolver ( AppSettingsSchema ) ,
@@ -61,15 +73,16 @@ export const SettingsDialog = () => {
6173 const onInvalid = ( errors : Record < string , unknown > ) => {
6274 // Sort tabs by the order they appear in the UI
6375 const tabsWithError = sortBy ( Object . keys ( errors ) , ( key ) =>
64- findIndex ( tabs , { value : key } )
76+ findIndex ( tabs , { value : key as SettingsTabValue } )
6577 )
66- setSelectedTab ( tabsWithError [ 0 ] || 'proxy' )
78+ setSelectedTab ( ( tabsWithError [ 0 ] as SettingsTabValue ) || 'proxy' )
6779 }
6880
6981 const handleOpenChange = ( ) => {
7082 reset ( settings )
71- setSelectedTab ( 'proxy' )
72- setIsOpen ( ! isOpen )
83+ if ( isOpen ) {
84+ closeSettingsDialog ( )
85+ }
7386 }
7487
7588 return (
@@ -89,7 +102,7 @@ export const SettingsDialog = () => {
89102 < FormProvider { ...formMethods } >
90103 < Tabs . Root
91104 value = { selectedTab }
92- onValueChange = { ( value ) => setSelectedTab ( value ) }
105+ onValueChange = { ( value ) => setSelectedTab ( value as SettingsTabValue ) }
93106 css = { css `
94107 height : 100% ;
95108 display : flex;
0 commit comments