Skip to content

Commit 04c7413

Browse files
committed
Fix infinit loop with useConfig
1 parent 42b459f commit 04c7413

5 files changed

Lines changed: 25 additions & 13 deletions

File tree

lib/user-interface/react/src/components/chatbot/components/Sessions.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -158,7 +158,7 @@ export function Sessions ({ newSession }) {
158158
}
159159
});
160160
}
161-
}, [auth, getConfiguration]);
161+
}, [auth.isLoading, auth.isAuthenticated, getConfiguration]);
162162

163163
useEffect(() => {
164164
if (!isDeleteByIdLoading && isDeleteByIdSuccess) {

lib/user-interface/react/src/components/chatbot/hooks/chat.hooks.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -173,7 +173,7 @@ export const useChatGeneration = ({
173173
};
174174

175175
return new ChatOpenAI(modelConfig);
176-
}, [selectedModel, auth, chatConfiguration]);
176+
}, [selectedModel, auth.user?.id_token, chatConfiguration]);
177177

178178
const retryResponse = async () => {
179179
if (!lastRequest) return;

lib/user-interface/react/src/components/configuration/ConfigurationComponent.tsx

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -102,17 +102,18 @@ export function ConfigurationComponent (): ReactElement {
102102
// eslint-disable-next-line react-hooks/exhaustive-deps
103103
}, [initialForm, state.form]);
104104

105+
const configVersionId = config?.[0]?.versionId;
106+
105107
useEffect(() => {
106108
if (!isFetchingConfig && config != null) {
107109
setState({
108-
...state,
109110
form: {
110111
...config[0]?.configuration,
111112
},
112113
});
113114
}
114115
// eslint-disable-next-line react-hooks/exhaustive-deps
115-
}, [config, isFetchingConfig]);
116+
}, [configVersionId, isFetchingConfig]);
116117

117118
useEffect(() => {
118119
if (!isUpdating && isUpdateSuccess) {

lib/user-interface/react/src/components/model-management/hooks/useModelComparison.hook.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,7 @@ export const useModelComparison = (models: IModel[], chatConfig: IChatConfigurat
9595
};
9696

9797
return new ChatOpenAI(modelConfig);
98-
}, [models, auth, chatConfig]);
98+
}, [models, auth.user?.id_token, chatConfig]);
9999

100100
const generateModelResponse = async (
101101
modelId: string,

lib/user-interface/react/src/shared/hooks/useAnnouncementNotifier.ts

Lines changed: 19 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
limitations under the License.
1515
*/
1616

17-
import { useEffect, useCallback } from 'react';
17+
import { useEffect, useCallback, useRef } from 'react';
1818
import { useAppDispatch } from '@/config/store';
1919
import { useNotificationService } from '@/shared/util/hooks';
2020
import { clearNotification } from '@/shared/reducers/notification.reducer';
@@ -30,31 +30,42 @@ const ANNOUNCEMENT_NOTIFICATION_ID = 'announcement-notification';
3030
export function useAnnouncementNotifier (config: IConfiguration | undefined): void {
3131
const dispatch = useAppDispatch();
3232
const notificationService = useNotificationService(dispatch);
33+
const lastAnnouncementRef = useRef<string | null>(null);
3334

3435
const clearAnnouncement = useCallback(() => {
3536
dispatch(clearNotification(ANNOUNCEMENT_NOTIFICATION_ID));
3637
}, [dispatch]);
3738

39+
// Extract stable primitive values to avoid re-running the effect on object reference changes
40+
const isEnabled = config?.configuration?.announcement?.isEnabled ?? false;
41+
const message = config?.configuration?.announcement?.message ?? '';
42+
const createdAt = config?.createdAt;
43+
3844
useEffect(() => {
3945
if (!config) {
4046
return;
4147
}
4248

43-
const announcement = config.configuration.announcement ?? { isEnabled: false, message: '' };
44-
const { isEnabled, message } = announcement;
45-
4649
if (!isEnabled || !message) {
50+
lastAnnouncementRef.current = null;
4751
clearAnnouncement();
4852
return;
4953
}
5054

51-
if (!shouldShowAnnouncement(config.createdAt)) {
55+
if (!shouldShowAnnouncement(createdAt)) {
56+
return;
57+
}
58+
59+
// Avoid re-dispatching the same announcement notification
60+
const announcementKey = `${message}:${createdAt}`;
61+
if (lastAnnouncementRef.current === announcementKey) {
5262
return;
5363
}
64+
lastAnnouncementRef.current = announcementKey;
5465

5566
const onDismiss = () => {
56-
if (config.createdAt !== undefined) {
57-
setDismissedTimestamp(config.createdAt);
67+
if (createdAt !== undefined) {
68+
setDismissedTimestamp(createdAt);
5869
}
5970
clearAnnouncement();
6071
};
@@ -67,5 +78,5 @@ export function useAnnouncementNotifier (config: IConfiguration | undefined): vo
6778
true,
6879
onDismiss,
6980
);
70-
}, [config, clearAnnouncement, notificationService]);
81+
}, [config, isEnabled, message, createdAt, clearAnnouncement, notificationService]);
7182
}

0 commit comments

Comments
 (0)