-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* feat: toggle theme provider * chore: clean up * chore: database connection item hover dark mode * chore: error box dark mode * remove use client directive * refactor toggle theme * slightly refactor --------- Co-authored-by: Visal In <[email protected]>
- Loading branch information
Showing
10 changed files
with
243 additions
and
85 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
import fs from "fs"; | ||
import { getUserDataFile } from "./file-helper"; | ||
|
||
interface Settings { | ||
[key: string]: string; | ||
} | ||
|
||
export class Setting { | ||
private filePath: string; | ||
private settings: Settings; | ||
|
||
constructor() { | ||
this.filePath = getUserDataFile("settings.json"); | ||
this.settings = {}; | ||
} | ||
|
||
/** | ||
* load window file system | ||
*/ | ||
load() { | ||
try { | ||
if (fs.existsSync(this.filePath)) { | ||
const data = fs.readFileSync(this.filePath, "utf-8"); | ||
this.settings = JSON.parse(data) as Settings; | ||
} else { | ||
this.save(); // Create the file if it doesn't exist | ||
} | ||
} catch (error) { | ||
console.error("Error loading settings:", error); | ||
} | ||
} | ||
|
||
save() { | ||
try { | ||
fs.writeFileSync( | ||
this.filePath, | ||
JSON.stringify(this.settings, null, 2), | ||
"utf-8", | ||
); | ||
} catch (error) { | ||
console.error("Error saving settings:", error); | ||
} | ||
} | ||
|
||
/** | ||
* | ||
* @param key string | ||
* @returns any value | ||
*/ | ||
get<T>(key: string): T | undefined { | ||
return this.settings[key] as T; | ||
} | ||
|
||
/** | ||
* | ||
* @param key string | ||
* @param value any | ||
*/ | ||
set<T extends string>(key: string, value: T) { | ||
this.settings[key] = value; | ||
this.save(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
import { | ||
PropsWithChildren, | ||
createContext, | ||
useCallback, | ||
useContext, | ||
useEffect, | ||
useMemo, | ||
useState, | ||
} from "react"; | ||
|
||
const KEY = "theme"; | ||
|
||
export type ThemeType = "dark" | "light"; | ||
|
||
type ContextType = { | ||
theme: ThemeType; | ||
toggleTheme: (theme?: ThemeType) => void; | ||
}; | ||
|
||
const ThemeContext = createContext<ContextType>({ | ||
theme: "light", | ||
toggleTheme: () => {}, | ||
}); | ||
|
||
export const useTheme = () => useContext(ThemeContext); | ||
|
||
export default function ThemeProvider({ | ||
children, | ||
}: PropsWithChildren<unknown>) { | ||
const [theme, setTheme] = useState<ThemeType>("light"); | ||
|
||
const toggleTheme = useCallback(async () => { | ||
const newTheme = theme === "light" ? "dark" : "light"; | ||
setTheme(newTheme); | ||
document.body.className = newTheme; | ||
await window.outerbaseIpc.setting.set(KEY, newTheme); | ||
}, [setTheme, theme]); | ||
|
||
useEffect(() => { | ||
(async () => { | ||
const savedTheme = await window.outerbaseIpc.setting.get<ThemeType>(KEY); | ||
if (savedTheme) { | ||
setTheme(savedTheme); | ||
document.body.className = savedTheme; | ||
} | ||
})(); | ||
}, []); | ||
|
||
const value = useMemo(() => { | ||
return { | ||
theme, | ||
toggleTheme, | ||
}; | ||
}, [toggleTheme, theme]); | ||
|
||
return ( | ||
<ThemeContext.Provider value={value}>{children}</ThemeContext.Provider> | ||
); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.