aboutsummaryrefslogtreecommitdiff
path: root/src/AppContext.tsx
diff options
context:
space:
mode:
authorAriel Costas Guerrero <94913521+arielcostas@users.noreply.github.com>2025-03-04 00:51:42 +0100
committerAriel Costas Guerrero <94913521+arielcostas@users.noreply.github.com>2025-03-04 00:51:42 +0100
commit79f3c42b0c04c7fd77481c14e6e9c345677b8c42 (patch)
tree6022fdb9d4cad1a77bc98eea88ad29803290f67d /src/AppContext.tsx
parent0a96e26ade5d3eafe64807fcbd877742d6bcf6da (diff)
Add table layout like iTranvias, remake settings page
Diffstat (limited to 'src/AppContext.tsx')
-rw-r--r--src/AppContext.tsx70
1 files changed, 70 insertions, 0 deletions
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