Skip to content

Commit caf60c4

Browse files
author
kim
committed
refactor: create custom hook
1 parent 7f51a6e commit caf60c4

File tree

3 files changed

+31
-25
lines changed

3 files changed

+31
-25
lines changed

src/modules/main/AdvancedView.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import { useTranslation } from 'react-i18next';
22

33
import { Stack, Typography } from '@mui/material';
44

5-
import ChatbotModelSelect from '../settings/ChatbotModelSelect';
5+
import { ChatbotModelSelect } from '../settings/ChatbotModelSelect';
66
import { GeneralSettingsView } from '../settings/GeneralSettingsView';
77

88
function AdvancedView() {

src/modules/settings/ChatbotModelSelect.tsx

Lines changed: 29 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
import { useCallback } from 'react';
2+
13
import {
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;

src/modules/settings/chatbot/ChatbotEditingView.tsx

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,6 @@ function ChatbotEditionView({
7676
const [prompt, setPrompt] = useState(parsedPrompt);
7777
const [cue, setCue] = useState(initialValue.cue);
7878
const [name, setName] = useState(initialValue.name);
79-
const [version] = useState(initialValue.version);
8079
const [formattingError, setFormattingError] = useState(false);
8180
const [unsavedChanges, setUnsavedChanges] = useState(false);
8281

@@ -127,7 +126,7 @@ function ChatbotEditionView({
127126
initialPrompt: prompt,
128127
chatbotCue: cue,
129128
chatbotName: name,
130-
gptVersion: version,
129+
gptVersion: initialValue.version,
131130
};
132131
onSave(data);
133132
}

0 commit comments

Comments
 (0)