1+ import { useCallback } from 'react' ;
2+
13import {
24 Alert ,
35 Chip ,
@@ -59,7 +61,10 @@ const models = Object.entries(GPTVersion)
5961 // sort models to put deprecated last in the list
6062 . toSorted ( compareModels ) ;
6163
62- function ChatbotModelSelect ( ) {
64+ const useGptVersion = ( ) => {
65+ const { mutate : postAppSetting } = mutations . usePostAppSetting ( ) ;
66+ const { mutate : patchAppSetting } = mutations . usePatchAppSetting ( ) ;
67+
6368 const { data : chatbotPromptSettings } =
6469 hooks . useAppSettings < ChatbotPromptSettings > ( {
6570 name : SettingsKeys . ChatbotPrompt ,
@@ -68,26 +73,30 @@ function ChatbotModelSelect() {
6873 const version =
6974 chatbotPromptSettings ?. [ 0 ] ?. data ?. gptVersion ?? DEFAULT_MODEL_VERSION ;
7075
71- const { mutate : postAppSetting } = mutations . usePostAppSetting ( ) ;
72- const { mutate : patchAppSetting } = mutations . usePatchAppSetting ( ) ;
76+ const handleChange = useCallback (
77+ < T extends ChatbotPromptSettings , K extends keyof T > ( value : T [ K ] ) : void => {
78+ const settingId = chatbotPromptSettings ?. [ 0 ] ?. id ;
79+ const data = { ...chatbotPromptSettings , gptVersion : value } ;
80+ if ( settingId ) {
81+ patchAppSetting ( {
82+ data,
83+ id : settingId ,
84+ } ) ;
85+ } else {
86+ postAppSetting ( {
87+ data,
88+ name : SettingsKeys . ChatbotPrompt ,
89+ } ) ;
90+ }
91+ } ,
92+ [ chatbotPromptSettings , postAppSetting , patchAppSetting ] ,
93+ ) ;
7394
74- const handleChange = < T extends ChatbotPromptSettings , K extends keyof T > (
75- value : T [ K ] ,
76- ) : void => {
77- const settingId = chatbotPromptSettings ?. [ 0 ] ?. id ;
78- const data = { ...chatbotPromptSettings , gptVersion : value } ;
79- if ( settingId ) {
80- patchAppSetting ( {
81- data,
82- id : settingId ,
83- } ) ;
84- } else {
85- postAppSetting ( {
86- data,
87- name : SettingsKeys . ChatbotPrompt ,
88- } ) ;
89- }
90- } ;
95+ return { version, handleChange } ;
96+ } ;
97+
98+ export function ChatbotModelSelect ( ) {
99+ const { version, handleChange } = useGptVersion ( ) ;
91100
92101 return (
93102 < Stack gap = { 1 } >
@@ -129,5 +138,3 @@ function ChatbotModelSelect() {
129138 </ Stack >
130139 ) ;
131140}
132-
133- export default ChatbotModelSelect ;
0 commit comments