11import { VSCodeButton , VSCodeCheckbox , VSCodeLink , VSCodeTextField } from "@vscode/webview-ui-toolkit/react"
2- import { memo , useCallback , useEffect , useRef , useState } from "react"
2+ import { forwardRef , memo , useCallback , useEffect , useImperativeHandle , useRef , useState } from "react"
33import { ExtensionStateContextType , useExtensionState } from "../../context/ExtensionStateContext"
44import { validateApiConfiguration , validateModelId } from "../../utils/validate"
55import { vscode } from "../../utils/vscode"
@@ -25,17 +25,22 @@ type SettingsViewProps = {
2525 onDone : ( ) => void
2626}
2727
28- const SettingsView = ( { onDone } : SettingsViewProps ) => {
28+ export interface SettingsViewRef {
29+ checkUnsaveChanges : ( then : ( ) => void ) => void
30+ }
31+
32+ const SettingsView = forwardRef < SettingsViewRef , SettingsViewProps > ( ( { onDone } , ref ) => {
2933 const extensionState = useExtensionState ( )
3034 const [ apiErrorMessage , setApiErrorMessage ] = useState < string | undefined > ( undefined )
3135 const [ modelIdErrorMessage , setModelIdErrorMessage ] = useState < string | undefined > ( undefined )
3236 const [ commandInput , setCommandInput ] = useState ( "" )
33- const prevApiConfigName = useRef ( extensionState . currentApiConfigName )
3437 const [ isDiscardDialogShow , setDiscardDialogShow ] = useState ( false )
35-
36- // TODO: Reduce WebviewMessage/ExtensionState complexity
3738 const [ cachedState , setCachedState ] = useState ( extensionState )
3839 const [ isChangeDetected , setChangeDetected ] = useState ( false )
40+ const prevApiConfigName = useRef ( extensionState . currentApiConfigName )
41+ const confirmDialogHandler = useRef < ( ) => void > ( )
42+
43+ // TODO: Reduce WebviewMessage/ExtensionState complexity
3944 const { currentApiConfigName } = extensionState
4045 const {
4146 apiConfiguration,
@@ -190,12 +195,6 @@ const SettingsView = ({ onDone }: SettingsViewProps) => {
190195 setModelIdErrorMessage ( modelIdValidationResult )
191196 } , [ apiConfiguration , extensionState . glamaModels , extensionState . openRouterModels ] )
192197
193- const confirmDialogHandler = useRef < ( ) => void > ( )
194- const onConfirmDialogResult = useCallback ( ( confirm : boolean ) => {
195- if ( confirm ) {
196- confirmDialogHandler . current ?.( )
197- }
198- } , [ ] )
199198 const checkUnsaveChanges = useCallback (
200199 ( then : ( ) => void ) => {
201200 if ( isChangeDetected ) {
@@ -208,6 +207,20 @@ const SettingsView = ({ onDone }: SettingsViewProps) => {
208207 [ isChangeDetected ] ,
209208 )
210209
210+ useImperativeHandle (
211+ ref ,
212+ ( ) => ( {
213+ checkUnsaveChanges,
214+ } ) ,
215+ [ checkUnsaveChanges ] ,
216+ )
217+
218+ const onConfirmDialogResult = useCallback ( ( confirm : boolean ) => {
219+ if ( confirm ) {
220+ confirmDialogHandler . current ?.( )
221+ }
222+ } , [ ] )
223+
211224 const handleResetState = ( ) => {
212225 vscode . postMessage ( { type : "resetState" } )
213226 }
@@ -250,7 +263,7 @@ const SettingsView = ({ onDone }: SettingsViewProps) => {
250263 < AlertDialogHeader >
251264 < AlertDialogTitle > Unsaved changes</ AlertDialogTitle >
252265 < AlertDialogDescription >
253- < span style = { { fontSize : "2em" } } className = { `codicon codicon-warning align-middle mr-1` } />
266+ < span className = { `codicon codicon-warning align-middle mr-1` } />
254267 Do you want to discard changes and continue?
255268 </ AlertDialogDescription >
256269 </ AlertDialogHeader >
@@ -890,6 +903,6 @@ const SettingsView = ({ onDone }: SettingsViewProps) => {
890903 </ div >
891904 </ div >
892905 )
893- }
906+ } )
894907
895908export default memo ( SettingsView )
0 commit comments