-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Updated to address issue 62 -- connecting the theme switch UI to their user setting in the db via the sublinks client api * Updated to address issue 62 load the initial theme value from the DB if present * Fix eslint issues * Updated layout to check for initial theme * Addressed type issues
- Loading branch information
1 parent
9389522
commit 9e2d695
Showing
4 changed files
with
41 additions
and
14 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,20 @@ | ||
'use client'; | ||
|
||
import { useLocalStorage } from '@/hooks/use-local-storage'; | ||
import { handleSaveUserSettings } from '@/utils/settings'; | ||
|
||
export function useTheme() { | ||
return useLocalStorage('theme', typeof window !== 'undefined' && window.matchMedia('(prefers-color-scheme: dark)').matches ? 'dark' : 'light'); | ||
export type Theme = 'light' | 'dark'; | ||
|
||
export function useTheme(initialTheme: Theme): [Theme, (theme: Theme) => void] { | ||
const originalTheme = initialTheme ?? (typeof window !== 'undefined' && window.matchMedia('(prefers-color-scheme: dark)').matches ? 'dark' : 'light'); | ||
|
||
const [theme, setTheme] = useLocalStorage('theme', originalTheme); | ||
|
||
const saveTheme = async (newTheme: Theme) => { | ||
setTheme(newTheme); | ||
|
||
await handleSaveUserSettings({ theme: newTheme }); | ||
}; | ||
|
||
return [theme, saveTheme]; | ||
} |
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,12 @@ | ||
import { SaveUserSettings } from 'sublinks-js-client'; | ||
|
||
import SublinksApi from './api-client/client'; | ||
import logger from './logger'; | ||
|
||
export const handleSaveUserSettings = async (userSettings: SaveUserSettings) => { | ||
try { | ||
await SublinksApi.Instance().Client().saveUserSettings(userSettings); | ||
} catch (e) { | ||
logger.error(`Failed to save user settings ${userSettings}`, e); | ||
} | ||
}; |