Skip to content

Commit badb82c

Browse files
fix(webview): sync cachedState on mode changes in SettingsView (#925)
* fix(webview): sync cachedState on mode changes in SettingsView * test(settings): strengthen mode sync change detection --------- Co-authored-by: Eason Liang <easonliang28@gmail.com>
1 parent 71a58a1 commit badb82c

2 files changed

Lines changed: 300 additions & 35 deletions

File tree

webview-ui/src/components/settings/SettingsView.tsx

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -130,7 +130,7 @@ const SettingsView = forwardRef<SettingsViewRef, SettingsViewProps>(({ onDone, t
130130
const { t } = useAppTranslation()
131131

132132
const extensionState = useExtensionState()
133-
const { currentApiConfigName, listApiConfigMeta, uriScheme, settingsImportedAt } = extensionState
133+
const { currentApiConfigName, listApiConfigMeta, uriScheme, settingsImportedAt, mode } = extensionState
134134

135135
const [isDiscardDialogShow, setDiscardDialogShow] = useState(false)
136136
const [isChangeDetected, setChangeDetected] = useState(false)
@@ -147,6 +147,7 @@ const SettingsView = forwardRef<SettingsViewRef, SettingsViewProps>(({ onDone, t
147147
const contentRef = useRef<HTMLDivElement | null>(null)
148148

149149
const prevApiConfigName = useRef(currentApiConfigName)
150+
const prevMode = useRef(mode)
150151
const handledSettingsImportedAt = useRef<number | undefined>(undefined)
151152
const confirmDialogHandler = useRef<() => void>()
152153

@@ -220,16 +221,17 @@ const SettingsView = forwardRef<SettingsViewRef, SettingsViewProps>(({ onDone, t
220221
const apiConfiguration = useMemo(() => cachedState.apiConfiguration ?? {}, [cachedState.apiConfiguration])
221222

222223
useEffect(() => {
223-
// Update only when currentApiConfigName is changed.
224-
// Expected to be triggered by loadApiConfiguration/upsertApiConfiguration.
225-
if (prevApiConfigName.current === currentApiConfigName) {
224+
// Update when currentApiConfigName or mode changes.
225+
// Expected to be triggered by loadApiConfiguration/upsertApiConfiguration or mode switch.
226+
if (prevApiConfigName.current === currentApiConfigName && prevMode.current === mode) {
226227
return
227228
}
228229

229230
setCachedState((prevCachedState) => ({ ...prevCachedState, ...extensionState }))
230231
prevApiConfigName.current = currentApiConfigName
232+
prevMode.current = mode
231233
setChangeDetected(false)
232-
}, [currentApiConfigName, extensionState])
234+
}, [currentApiConfigName, mode, extensionState])
233235

234236
// Bust the cache when settings are imported.
235237
useEffect(() => {

0 commit comments

Comments
 (0)