Skip to content

🀯 LobeHub - Super-Agents on Your Team. Add super-agents to the chats your team already uses.They take on work by themselves and get better every week.

πŸ› fix: prevent stale topic ID from persisting in URL on remount

YuTengjing2a23fb9a1062
diff --git a/src/features/GenerationTopicList/TopicUrlSync.tsx b/src/features/GenerationTopicList/TopicUrlSync.tsx
index 0f749b97110..63725ab5960 100644
--- a/src/features/GenerationTopicList/TopicUrlSync.tsx
+++ b/src/features/GenerationTopicList/TopicUrlSync.tsx
@@ -1,22 +1,29 @@
'use client';
import { useLayoutEffect } from 'react';
-import { createStoreUpdater } from 'zustand-utils';
import { useQueryState } from '@/hooks/useQueryParam';
import { useGenerationTopicContext } from './StoreContext';
/**
- * Bidirectional sync between URL 'topic' param and store's activeGenerationTopicId
+ * Bidirectional sync between URL 'topic' param and store's activeGenerationTopicId.
+ *
+ * Uses two useLayoutEffect hooks to ensure URL β†’ store sync runs before
+ * the store β†’ URL subscription is set up, preventing stale store values
+ * from overwriting the URL on remount.
*/
const TopicUrlSync = () => {
const { useStore } = useGenerationTopicContext();
- const useStoreUpdater = createStoreUpdater(useStore);
const [topic, setTopic] = useQueryState('topic', { history: 'replace', throttleMs: 500 });
- useStoreUpdater('activeGenerationTopicId', topic);
+ // URL β†’ store: runs first to ensure store matches URL before subscription
+ useLayoutEffect(() => {
+ useStore.setState({ activeGenerationTopicId: topic ?? null });
+ }, [topic, useStore]);
+
+ // Store β†’ URL: subscribes after URL β†’ store sync
useLayoutEffect(() => {
let prevTopicId = useStore.getState().activeGenerationTopicId;
const unsubscribeTopic = useStore.subscribe((state) => {
@@ -29,7 +36,7 @@ const TopicUrlSync = () => {
return () => {
unsubscribeTopic();
};
- }, [setTopic]);
+ }, [setTopic, useStore]);
return null;
};