From dae5187128f7b135ca0e076402dcb9b22e602a67 Mon Sep 17 00:00:00 2001 From: Alexander Date: Sun, 28 Apr 2024 16:03:33 -0400 Subject: [PATCH] use context for theme storage and providing --- ui/src/App.tsx | 15 +++++---------- ui/src/components/navbar.tsx | 24 ++++++------------------ ui/src/components/settings.tsx | 19 ++++++++----------- ui/src/context/ThemeContext.tsx | 26 ++++++++++++++++++++++++++ ui/src/index.tsx | 15 +++++++++------ 5 files changed, 54 insertions(+), 45 deletions(-) create mode 100644 ui/src/context/ThemeContext.tsx diff --git a/ui/src/App.tsx b/ui/src/App.tsx index d3d185a..ce11f18 100644 --- a/ui/src/App.tsx +++ b/ui/src/App.tsx @@ -1,19 +1,14 @@ -import { Component, createSignal } from "solid-js"; +import { Component, useContext } from "solid-js"; import { Navbar } from "~/components/navbar"; +import { IThemeContext, ThemeContext } from "~/context/ThemeContext.tsx"; const App: Component = (props: any) => { - let preferredTheme = localStorage.getItem("theme"); - const [theme, setTheme] = createSignal(preferredTheme ? preferredTheme : "blackout"); - - const changeThemeCallback = (theme: string) => { - setTheme(theme); - localStorage.setItem("theme", theme); - }; + const themeContext: IThemeContext = useContext(ThemeContext); return ( <> -
- +
+ {props.children}
diff --git a/ui/src/components/navbar.tsx b/ui/src/components/navbar.tsx index 6114538..86bd29d 100644 --- a/ui/src/components/navbar.tsx +++ b/ui/src/components/navbar.tsx @@ -1,28 +1,16 @@ import { Link } from "./link"; -import { Component, createSignal, For } from "solid-js"; +import { For } from "solid-js"; import { Settings } from "~/components/settings.tsx"; -import { useLocation } from '@solidjs/router'; - -interface Props { - theme: string; - callback: (theme: string) => void; -} +import { useLocation } from "@solidjs/router"; interface Route { path: string; display: string; } -export const Navbar: Component = (props: Props) => { - // let storedPreference = localStorage.getItem('theme'); - const [theme, setTheme] = createSignal(props.theme); +export function Navbar() { const location = useLocation(); - const changeTheme = (theme: string) => { - setTheme(theme); - props.callback(theme); - }; - const routes = [ { path: "/games", @@ -45,15 +33,15 @@ export const Navbar: Component = (props: Props) => { {route => ( {route.display} )} - + ); -}; +} diff --git a/ui/src/components/settings.tsx b/ui/src/components/settings.tsx index 0714c65..b08e3a0 100644 --- a/ui/src/components/settings.tsx +++ b/ui/src/components/settings.tsx @@ -1,4 +1,4 @@ -import { ComponentProps } from "solid-js"; +import { ComponentProps, useContext } from "solid-js"; import { AlertDialog, AlertDialogContent, @@ -14,14 +14,11 @@ import { SelectValue } from "~/components/ui/select.tsx"; import { OcGear3 } from "solid-icons/oc"; +import { IThemeContext, ThemeContext } from "~/context/ThemeContext.tsx"; -export interface SettingsProps extends ComponentProps<"div"> { - theme: string; - callback: (theme: string) => void; -} - -export function Settings(props: SettingsProps) { +export function Settings() { const availableThemes = ["blackout", "logan", "lavender", "light", "blue"]; + const themeContext: IThemeContext = useContext(ThemeContext); return ( @@ -29,7 +26,7 @@ export function Settings(props: SettingsProps) { Settings @@ -37,8 +34,8 @@ export function Settings(props: SettingsProps) { diff --git a/ui/src/context/ThemeContext.tsx b/ui/src/context/ThemeContext.tsx new file mode 100644 index 0000000..d886bbb --- /dev/null +++ b/ui/src/context/ThemeContext.tsx @@ -0,0 +1,26 @@ +import { createContext, JSXElement } from "solid-js"; +import { createStore } from "solid-js/store"; + +export interface ThemeContextProviderProps { + children?: JSXElement; +} + +export interface IThemeContext { + theme: string; + setTheme: (theme: string) => void; +} + +export const ThemeContext = createContext({} as IThemeContext); + +export function ThemeContextProvider(props: ThemeContextProviderProps) { + let preferredTheme = localStorage.getItem("accuribet-theme"); + const [themeStore, setThemeStore] = createStore({ + theme: preferredTheme ? preferredTheme : "blackout", + setTheme(theme: string) { + localStorage.setItem("accuribet-theme", theme); + setThemeStore("theme", theme); + } + }); + + return {props.children}; +} diff --git a/ui/src/index.tsx b/ui/src/index.tsx index 323e819..29c49c8 100644 --- a/ui/src/index.tsx +++ b/ui/src/index.tsx @@ -9,17 +9,20 @@ import { History } from "~/pages/History"; import { Notfound } from "~/pages/Notfound.tsx"; import "./index.css"; +import { ThemeContextProvider } from "~/context/ThemeContext.tsx"; const root = document.getElementById("root") as HTMLElement; render( () => ( - - - - - - + + + + + + + + ), root! );