Skip to content

🪢 Open source agent evals & observability: Trace, evaluate, and improve LLM applications with one open platform.

chore: revert changes to session storage hook from #5314 (#5513)

marliessophiebecae4c720bb
diff --git a/web/src/components/useSessionStorage.tsx b/web/src/components/useSessionStorage.tsx
index b7bae88e1e..dcc8f9503d 100644
--- a/web/src/components/useSessionStorage.tsx
+++ b/web/src/components/useSessionStorage.tsx
@@ -1,8 +1,4 @@
-import { useState, useEffect, useCallback } from "react";
-
-function isClient() {
- return typeof window !== "undefined";
-}
+import { useState, useEffect } from "react";
/**
* useSessionStorage is a hook for managing data with the sessionStorage API.
@@ -26,18 +22,18 @@ function useSessionStorage<T>(
sessionStorageKey: string,
initialValue: T,
): [T, React.Dispatch<React.SetStateAction<T>>, () => void] {
- const readValue = useCallback((): T => {
- if (!isClient()) return initialValue; // Fallback for SSR
+ const [value, setValue] = useState<T>(() => {
+ if (typeof window === "undefined") {
+ return initialValue;
+ }
try {
const storedValue = sessionStorage.getItem(sessionStorageKey);
- return storedValue ? JSON.parse(storedValue) : initialValue;
+ return storedValue ? (JSON.parse(storedValue) as T) : initialValue;
} catch (error) {
console.error("Error reading from session storage", error);
return initialValue;
}
- }, [sessionStorageKey, initialValue]);
-
- const [storedValue, setStoredValue] = useState<T>(readValue);
+ });
const clearValue = () => {
try {
@@ -48,38 +44,16 @@ function useSessionStorage<T>(
}
};
- const setValue = useCallback(
- (value: T | ((prev: T) => T)) => {
- try {
- const newValue = value instanceof Function ? value(storedValue) : value;
- setStoredValue(newValue);
- if (isClient()) {
- sessionStorage.setItem(sessionStorageKey, JSON.stringify(newValue));
- window.dispatchEvent(new Event("session-storage"));
- }
- } catch (error) {
- console.error("Error reading from session storage", error);
- }
- },
- [sessionStorageKey, storedValue],
- );
-
// Sync state with sessionStorage changes across tabs
useEffect(() => {
- const handleStorageChange = () => {
- setStoredValue(readValue());
- };
-
- window.addEventListener("session-storage", handleStorageChange);
- window.addEventListener("storage", handleStorageChange);
-
- return () => {
- window.removeEventListener("session-storage", handleStorageChange);
- window.removeEventListener("storage", handleStorageChange);
- };
- }, [readValue]);
+ try {
+ sessionStorage.setItem(sessionStorageKey, JSON.stringify(value));
+ } catch (error) {
+ console.error("Error writing to session storage", error);
+ }
+ }, [sessionStorageKey, value]);
- return [storedValue, setValue, clearValue] as const;
+ return [value, setValue, clearValue] as const;
}
export default useSessionStorage;