Persist sidebar collapsed state and selected text ID in local storage

This commit is contained in:
2026-01-31 12:51:04 +01:00
parent 8ffb7af26f
commit c866590896

View File

@@ -88,7 +88,9 @@ export default function App() {
} }
return 16; return 16;
}); });
const [sidebarCollapsed, setSidebarCollapsed] = useState(false); const [sidebarCollapsed, setSidebarCollapsed] = useState(() => {
return localStorage.getItem("textdb.sidebarCollapsed") === "true";
});
const bodyRef = useRef(body); const bodyRef = useRef(body);
const historySnapshotRef = useRef<HistorySnapshot | null>(null); const historySnapshotRef = useRef<HistorySnapshot | null>(null);
@@ -110,6 +112,16 @@ export default function App() {
localStorage.setItem("textdb.textSize", String(textSize)); localStorage.setItem("textdb.textSize", String(textSize));
}, [textSize]); }, [textSize]);
useEffect(() => {
localStorage.setItem("textdb.sidebarCollapsed", String(sidebarCollapsed));
}, [sidebarCollapsed]);
useEffect(() => {
if (selectedTextId) {
localStorage.setItem("textdb.selectedTextId", selectedTextId);
}
}, [selectedTextId]);
const isViewingHistory = viewingVersion !== null; const isViewingHistory = viewingVersion !== null;
const isDirty = !isViewingHistory && body !== lastPersistedBody; const isDirty = !isViewingHistory && body !== lastPersistedBody;
const hasText = body.trim().length > 0; const hasText = body.trim().length > 0;
@@ -186,9 +198,13 @@ export default function App() {
}, [refreshTexts]); }, [refreshTexts]);
useEffect(() => { useEffect(() => {
if (!selectedTextId && texts.length > 0) { if (selectedTextId || texts.length === 0) return;
setSelectedTextId(texts[0].id); const storedId = localStorage.getItem("textdb.selectedTextId");
} const fallback = texts[0].id;
const resolved = storedId && texts.some((text) => text.id === storedId)
? storedId
: fallback;
setSelectedTextId(resolved);
}, [selectedTextId, texts]); }, [selectedTextId, texts]);
useEffect(() => { useEffect(() => {
@@ -229,6 +245,7 @@ export default function App() {
if (!text) { if (!text) {
setSelectedTextId(null); setSelectedTextId(null);
localStorage.removeItem("textdb.selectedTextId");
return; return;
} }
@@ -393,6 +410,7 @@ export default function App() {
await refreshTexts(); await refreshTexts();
if (selectedTextId === promptId) { if (selectedTextId === promptId) {
setSelectedTextId(null); setSelectedTextId(null);
localStorage.removeItem("textdb.selectedTextId");
} }
}, },
[refreshTexts, selectedTextId] [refreshTexts, selectedTextId]