Merge pull request #994 from MODSetter/dev_mod

feat: implement session storage for tabs state management and optimiz…
This commit is contained in:
Rohan Verma 2026-03-27 02:09:09 -07:00 committed by GitHub
commit 52487c4acd
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
2 changed files with 18 additions and 3 deletions

View file

@ -1,4 +1,5 @@
import { atom } from "jotai"; import { atom } from "jotai";
import { atomWithStorage, createJSONStorage } from "jotai/utils";
export type TabType = "chat" | "document"; export type TabType = "chat" | "document";
@ -32,7 +33,16 @@ const initialState: TabsState = {
activeTabId: "chat-new", activeTabId: "chat-new",
}; };
export const tabsStateAtom = atom<TabsState>(initialState); const sessionStorageAdapter = createJSONStorage<TabsState>(
() => (typeof window !== "undefined" ? sessionStorage : undefined) as Storage
);
export const tabsStateAtom = atomWithStorage<TabsState>(
"surfsense:tabs",
initialState,
sessionStorageAdapter,
{ getOnInit: true },
);
export const tabsAtom = atom((get) => get(tabsStateAtom).tabs); export const tabsAtom = atom((get) => get(tabsStateAtom).tabs);
export const activeTabIdAtom = atom((get) => get(tabsStateAtom).activeTabId); export const activeTabIdAtom = atom((get) => get(tabsStateAtom).activeTabId);

View file

@ -268,9 +268,14 @@ export function LayoutDataProvider({ searchSpaceId, children }: LayoutDataProvid
}, [pendingNewChat, params?.chat_id, router, searchSpaceId, resetCurrentThread]); }, [pendingNewChat, params?.chat_id, router, searchSpaceId, resetCurrentThread]);
// Reset transient slide-out panels and tabs when switching search spaces. // Reset transient slide-out panels and tabs when switching search spaces.
// Use a ref to skip the initial mount — only reset when the space actually changes.
const prevSearchSpaceIdRef = useRef(searchSpaceId);
useEffect(() => { useEffect(() => {
if (prevSearchSpaceIdRef.current !== searchSpaceId) {
prevSearchSpaceIdRef.current = searchSpaceId;
setActiveSlideoutPanel(null); setActiveSlideoutPanel(null);
resetTabs(); resetTabs();
}
}, [searchSpaceId, resetTabs]); }, [searchSpaceId, resetTabs]);
const searchSpaces: SearchSpace[] = useMemo(() => { const searchSpaces: SearchSpace[] = useMemo(() => {