11import { useCallback , useEffect , useState } from "react" ;
2+ import { useQueryClient } from "@tanstack/react-query" ;
23import { Button , cn } from "@heroui/react" ;
34import { useSettingStore } from "../../stores/setting-store" ;
45import { Settings } from "../../pkg/gen/apiclient/user/v1/user_pb" ;
56import { PlainMessage } from "../../query/types" ;
67import { useConversationStore } from "../../stores/conversation/conversation-store" ;
78import { listSupportedModels } from "../../query/api" ;
9+ import { queryKeys } from "../../query/keys" ;
810
911type SettingKey = keyof PlainMessage < Settings > ;
1012
@@ -28,6 +30,7 @@ export function createSettingsTextInput<K extends SettingKey>(settingKey: K) {
2830 multiline = true ,
2931 password = false ,
3032 } : SettingsTextInputProps ) {
33+ const queryClient = useQueryClient ( ) ;
3134 const { settings, isUpdating, updateSettings } = useSettingStore ( ) ;
3235 const { setCurrentConversation } = useConversationStore ( ) ;
3336 const [ value , setValue ] = useState < string > ( "" ) ;
@@ -55,9 +58,10 @@ export function createSettingsTextInput<K extends SettingKey>(settingKey: K) {
5558 setOriginalValue ( value . trim ( ) ) ;
5659 setIsEditing ( false ) ;
5760
58- // If openaiApiKey was updated, fetch new model list and update current model slug
61+ // If openaiApiKey was updated, fetch new model list, update React Query cache (so chat UI shows correct disabled state e.g. grey out BYOK-only models when key is removed), and update current model slug
5962 if ( settingKey === "openaiApiKey" ) {
6063 const response = await listSupportedModels ( { } ) ;
64+ queryClient . setQueryData ( queryKeys . chats . listSupportedModels ( ) . queryKey , response ) ;
6165 if ( response . models ?. length ) {
6266 const { currentConversation : latest } = useConversationStore . getState ( ) ;
6367 // try to find a model that matches the current slug
@@ -78,7 +82,7 @@ export function createSettingsTextInput<K extends SettingKey>(settingKey: K) {
7882 }
7983 }
8084 }
81- } , [ value , settingKey , updateSettings ] ) ; // settingKey is an outer scope value, not a dependency
85+ } , [ value , updateSettings , queryClient , setCurrentConversation ] ) ; // settingKey is an outer scope value, not a dependency
8286
8387 const handleEdit = useCallback ( ( ) => {
8488 setIsEditing ( true ) ;
0 commit comments