Files
temporal-ai-agent/frontend/src/hooks/useLocalChatHistory.js
Steve Androulakis 93ec96a406 basic react API
2025-01-02 18:05:28 -08:00

20 lines
527 B
JavaScript

import { useState, useEffect } from "react";
export default function useLocalChatHistory(key, initialValue) {
const [state, setState] = useState(() => {
try {
const stored = window.localStorage.getItem(key);
return stored ? JSON.parse(stored) : initialValue;
} catch (err) {
console.error("Error parsing localStorage:", err);
return initialValue;
}
});
useEffect(() => {
window.localStorage.setItem(key, JSON.stringify(state));
}, [key, state]);
return [state, setState];
}