Skip to content

Commit

Permalink
Remove ThemeProvider and move Theme logic to Settings
Browse files Browse the repository at this point in the history
  • Loading branch information
KaffinPX committed Sep 1, 2024
1 parent d7b785f commit fa758b9
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 96 deletions.
27 changes: 12 additions & 15 deletions src/App.tsx
Original file line number Diff line number Diff line change
@@ -1,28 +1,25 @@
import { MemoryRouter, Route, Routes } from 'react-router-dom'
import { SettingsProvider } from './contexts/Settings'
import { KaspaProvider } from './contexts/Kaspa'
import { ThemeProvider } from '@/components/ThemeProvider'
import Landing from './pages/Landing'
import CreateWallet from '@/pages/CreateWallet'
import Wallet from '@/pages/Wallet'
import UnlockWallet from '@/pages/Unlock'

function App () {
return (
<ThemeProvider>
<SettingsProvider>
<KaspaProvider>
<MemoryRouter>
<Routes>
<Route path="/" element={<Landing />} />
<Route path="/create" element={<CreateWallet />} />
<Route path="/unlock" element={<UnlockWallet />} />
<Route path="/wallet" element={<Wallet />} />
</Routes>
</MemoryRouter>
</KaspaProvider>
</SettingsProvider>
</ThemeProvider>
<SettingsProvider>
<KaspaProvider>
<MemoryRouter>
<Routes>
<Route path="/" element={<Landing />} />
<Route path="/create" element={<CreateWallet />} />
<Route path="/unlock" element={<UnlockWallet />} />
<Route path="/wallet" element={<Wallet />} />
</Routes>
</MemoryRouter>
</KaspaProvider>
</SettingsProvider>
)
}

Expand Down
73 changes: 0 additions & 73 deletions src/components/ThemeProvider.tsx

This file was deleted.

28 changes: 24 additions & 4 deletions src/contexts/Settings.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,13 @@ import { createContext, useState, type ReactNode, useEffect, useCallback } from

export interface ISettings {
version: number
theme: "dark" | "light" | "system"
currency: keyof typeof currencies
nodes: {
name: string
address: string
locked: boolean
}[]
currency: keyof typeof currencies
selectedNode: number
}

Expand All @@ -18,7 +19,9 @@ export const currencies = {
}

export const defaultSettings: ISettings = {
version: 4,
version: 1,
theme: 'system',
currency: 'USD',
nodes: [{
name: "Public node",
address: "mainnet",
Expand All @@ -32,8 +35,7 @@ export const defaultSettings: ISettings = {
address: "testnet-11",
locked: true
}],
currency: 'USD',
selectedNode: 0
selectedNode: 0,
}

export const SettingsContext = createContext<{
Expand All @@ -51,6 +53,24 @@ export function SettingsProvider({ children }: {
LocalStorage.set("settings", settings)
}, [ settings ])

useEffect(() => {
const root = window.document.documentElement

root.classList.remove("light", "dark")

if (settings['theme'] === "system") {
const systemTheme = window.matchMedia("(prefers-color-scheme: dark)")
.matches
? "dark"
: "light"

root.classList.add(systemTheme)
return
}

root.classList.add(settings['theme'])
}, [ settings['theme'] ])

const load = useCallback(async () => {
const storedSettings = await LocalStorage.get('settings', defaultSettings)

Expand Down
6 changes: 2 additions & 4 deletions src/pages/Wallet/Settings/General.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,12 @@ import { i18n } from "webextension-polyfill"
import { Sun, Moon, Laptop } from "lucide-react"
import { Select, SelectContent, SelectItem, SelectTrigger, SelectValue } from "@/components/ui/select"
import { AccordionContent, AccordionItem, AccordionTrigger } from "@/components/ui/accordion"
import { useTheme } from "@/components/ThemeProvider"
import { ToggleGroup, ToggleGroupItem } from "@/components/ui/toggle-group"
import { currencies } from "@/contexts/Settings"
import useSettings from "@/hooks/useSettings"

export default function General () {
const { settings, updateSetting } = useSettings()
const theme = useTheme()

return (
<AccordionItem value="general">
Expand All @@ -23,11 +21,11 @@ export default function General () {
<div className={"flex gap-1"}>
<ToggleGroup
type="single"
defaultValue={theme.theme}
defaultValue={settings.theme}
onValueChange={(value) => {
if (value === "") return

theme.setTheme(value as never)
updateSetting('theme', value as never)
}}
>
<ToggleGroupItem value="light">
Expand Down

0 comments on commit fa758b9

Please sign in to comment.