From d7d3cdc15b355182f22bf4e48d0cc397b8bc7c5d Mon Sep 17 00:00:00 2001 From: 1ilit <1ilit@proton.me> Date: Fri, 8 Nov 2024 20:56:44 +0400 Subject: [PATCH] Cache settings --- src/context/SettingsContext.jsx | 31 +++++++++++++++++-------------- 1 file changed, 17 insertions(+), 14 deletions(-) diff --git a/src/context/SettingsContext.jsx b/src/context/SettingsContext.jsx index 7e5a8b97..8107068b 100644 --- a/src/context/SettingsContext.jsx +++ b/src/context/SettingsContext.jsx @@ -1,7 +1,7 @@ -import { createContext, useState } from "react"; +import { createContext, useEffect, useState } from "react"; import { tableWidth } from "../data/constants"; -export const SettingsContext = createContext({ +const defaultSettings = { strictMode: false, showFieldSummary: true, showGrid: true, @@ -11,20 +11,23 @@ export const SettingsContext = createContext({ showCardinality: true, tableWidth: tableWidth, showDebugCoordinates: false, -}); +}; + +export const SettingsContext = createContext(defaultSettings); export default function SettingsContextProvider({ children }) { - const [settings, setSettings] = useState({ - strictMode: false, - showFieldSummary: true, - showGrid: true, - mode: "light", - autosave: true, - panning: true, - showCardinality: true, - tableWidth: tableWidth, - showDebugCoordinates: false, - }); + const [settings, setSettings] = useState(defaultSettings); + + useEffect(() => { + const settings = localStorage.getItem("settings"); + if (settings) { + setSettings(JSON.parse(settings)); + } + }, []); + + useEffect(() => { + localStorage.setItem("settings", JSON.stringify(settings)); + }, [settings]); return (