Skip to content

Commit cbc21ee

Browse files
committed
fix(react-client): define chatSettingsValue type. add default value side effect
1 parent c5b5e1b commit cbc21ee

File tree

1 file changed

+14
-17
lines changed

1 file changed

+14
-17
lines changed

libs/react-client/src/store/chat.ts

Lines changed: 14 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,8 @@ interface ChatState {
1717
sideView?: { title: string; elements: IMessageElement[] };
1818
chatProfile?: string;
1919
chatSettingsInputs: any[];
20-
chatSettingsDefaultValue: any;
21-
chatSettingsValue: any;
20+
chatSettingsDefaultValue: Record<any, any>;
21+
chatSettingsValue: Record<any, any>;
2222

2323
setIsAiSpeaking: (isAiSpeaking: boolean) => void;
2424
setAudioConnection: (audioConnection: 'connecting' | 'on' | 'off') => void;
@@ -42,7 +42,7 @@ interface ChatState {
4242
) => void;
4343
setChatProfile: (chatProfile: string) => void;
4444
setChatSettingsInputs: (chatSettingsInputs: any[]) => void;
45-
setChatSettingsValue: (chatSettingsValue: any) => void;
45+
setChatSettingsValue: (chatSettingsValue: Record<any, any>) => void;
4646
resetChatSettingsInputs: () => void;
4747
resetChatSettingsValue: () => void;
4848
}
@@ -55,8 +55,8 @@ export const useChatStore = create<ChatState>((set, get) => ({
5555
wavRecorder: new WavRecorder(),
5656
commands: [],
5757
chatSettingsInputs: [],
58-
chatSettingsValue: [],
59-
chatSettingsDefaultValue: [],
58+
chatSettingsValue: {},
59+
chatSettingsDefaultValue: {},
6060

6161
setIsAiSpeaking: (isAiSpeaking) => {
6262
set({ isAiSpeaking });
@@ -87,7 +87,15 @@ export const useChatStore = create<ChatState>((set, get) => ({
8787
},
8888

8989
setChatSettingsInputs: (chatSettingsInputs) => {
90-
set({ chatSettingsInputs });
90+
// Collect default values of all inputs
91+
const chatSettingsDefaultValue = chatSettingsInputs.reduce(
92+
(form: { [key: string]: any }, input: any) => (
93+
(form[input.id] = input.initial), form
94+
),
95+
{}
96+
);
97+
98+
set({ chatSettingsInputs, chatSettingsDefaultValue });
9199
},
92100

93101
setChatSettingsValue: (chatSettingsValue) => {
@@ -109,14 +117,3 @@ export const useChatStore = create<ChatState>((set, get) => ({
109117
set({ chatSettingsValue });
110118
}
111119
}));
112-
113-
// useChatStore.subscribe((state, prevState) => {
114-
// const chatSettingsDefaultValue = state.chatSettingsInputs.reduce(
115-
// (form: { [key: string]: any }, input: any) => (
116-
// (form[input.id] = input.initial), form
117-
// ),
118-
// {}
119-
// );
120-
//
121-
// useChatStore.setState({chatSettingsDefaultValue });
122-
// })

0 commit comments

Comments
 (0)