1
0
mirror of https://github.com/theoludwig/theoludwig.git synced 2026-05-06 13:48:12 +02:00

refactor: implement light/dark themes using cookies

This commit is contained in:
2023-08-01 14:11:46 +02:00
parent e82db952db
commit caa6a90418
21 changed files with 117 additions and 117 deletions
+9
View File
@@ -0,0 +1,9 @@
import UniversalCookie from 'universal-cookie'
import { DEFAULT_THEME } from '@/utils/constants'
import type { CookiesStore, Theme } from '@/utils/constants'
export const useTheme = (cookiesStore: CookiesStore): Theme => {
const universalCookie = new UniversalCookie(cookiesStore)
return universalCookie.get('theme') ?? DEFAULT_THEME
}
+21
View File
@@ -0,0 +1,21 @@
'use server'
import { cookies } from 'next/headers'
import type { Theme } from '@/utils/constants'
import { COOKIE_MAX_AGE, DEFAULT_THEME, THEMES } from '@/utils/constants'
export const setTheme = (theme: Theme): void => {
cookies().set('theme', theme, {
path: '/',
maxAge: COOKIE_MAX_AGE
})
}
export const getTheme = (): Theme => {
const theme = cookies().get('theme')?.value ?? DEFAULT_THEME
if (THEMES.includes(theme as Theme)) {
return theme as Theme
}
return DEFAULT_THEME
}