From 79f3c42b0c04c7fd77481c14e6e9c345677b8c42 Mon Sep 17 00:00:00 2001
From: Ariel Costas Guerrero <94913521+arielcostas@users.noreply.github.com>
Date: Tue, 4 Mar 2025 00:51:42 +0100
Subject: Add table layout like iTranvias, remake settings page

---
 src/AppContext.tsx | 70 ++++++++++++++++++++++++++++++++++++++++++++++++++++++
 1 file changed, 70 insertions(+)
 create mode 100644 src/AppContext.tsx

(limited to 'src/AppContext.tsx')

diff --git a/src/AppContext.tsx b/src/AppContext.tsx
new file mode 100644
index 0000000..373e624
--- /dev/null
+++ b/src/AppContext.tsx
@@ -0,0 +1,70 @@
+import { createContext, useContext, useEffect, useState, ReactNode } from 'react';
+
+type Theme = 'light' | 'dark';
+type TableStyle = 'regular'|'grouped';
+
+interface AppContextProps {
+  theme: Theme;
+  setTheme: React.Dispatch<React.SetStateAction<Theme>>;
+  toggleTheme: () => void;
+  
+  tableStyle: TableStyle;
+  setTableStyle: React.Dispatch<React.SetStateAction<TableStyle>>;
+  toggleTableStyle: () => void;
+}
+
+const AppContext = createContext<AppContextProps | undefined>(undefined);
+
+export const AppProvider = ({ children }: { children: ReactNode }) => {
+  //#region Theme
+  const [theme, setTheme] = useState<Theme>(() => {
+    const savedTheme = localStorage.getItem('theme');
+    if (savedTheme) {
+      return savedTheme as Theme;
+    }
+    const prefersDark = window.matchMedia && window.matchMedia('(prefers-color-scheme: dark)').matches;
+    return prefersDark ? 'dark' : 'light';
+  });
+
+  const toggleTheme = () => {
+    setTheme((prevTheme) => (prevTheme === 'light' ? 'dark' : 'light'));
+  };
+
+  useEffect(() => {
+    document.documentElement.setAttribute('data-theme', theme);
+    localStorage.setItem('theme', theme);
+  }, [theme]);
+  //#endregion
+
+  //#region Table Style
+  const [tableStyle, setTableStyle] = useState<TableStyle>(() => {
+    const savedTableStyle = localStorage.getItem('tableStyle');
+    if (savedTableStyle) {
+      return savedTableStyle as TableStyle;
+    }
+    return 'regular';
+  });
+
+  const toggleTableStyle = () => {
+    setTableStyle((prevTableStyle) => (prevTableStyle === 'regular' ? 'grouped' : 'regular'));
+  }
+
+  useEffect(() => {
+    localStorage.setItem('tableStyle', tableStyle);
+  }, [tableStyle]);
+  //#endregion
+
+  return (
+    <AppContext.Provider value={{ theme, setTheme, toggleTheme, tableStyle, setTableStyle, toggleTableStyle }}>
+      {children}
+    </AppContext.Provider>
+  );
+};
+
+export const useApp = () => {
+  const context = useContext(AppContext);
+  if (!context) {
+    throw new Error('useApp must be used within a AppProvider');
+  }
+  return context;
+};
\ No newline at end of file
-- 
cgit v1.3

