mirror of
https://github.com/d0zingcat/NotionNext.git
synced 2026-05-23 23:16:50 +00:00
初始化夜间模式
This commit is contained in:
@@ -1,8 +1,9 @@
|
||||
import lang from './lang'
|
||||
import { useContext, createContext, useState, useEffect } from 'react'
|
||||
import Router from 'next/router'
|
||||
import { initTheme, loadDarkModeFromCookies } from './theme'
|
||||
import { initDarkMode } from './theme'
|
||||
const GlobalContext = createContext()
|
||||
let hasInit = false
|
||||
|
||||
/**
|
||||
* 全局变量Provider,包括语言本地化、样式主题、搜索词
|
||||
@@ -12,7 +13,7 @@ const GlobalContext = createContext()
|
||||
*/
|
||||
export function GlobalContextProvider ({ children }) {
|
||||
const [locale, updateLocale] = useState(generateLocaleDict('en-US'))
|
||||
const [isDarkMode, updateDarkMode] = useState(loadDarkModeFromCookies())
|
||||
const [isDarkMode, updateDarkMode] = useState(false)
|
||||
const [onLoading, changeLoadingState] = useState(false)
|
||||
Router.events.on('routeChangeStart', (...args) => {
|
||||
changeLoadingState(true)
|
||||
@@ -23,10 +24,14 @@ export function GlobalContextProvider ({ children }) {
|
||||
})
|
||||
|
||||
// 服务端静态渲染,在渲染hooks后根据前端变量做初始化工作
|
||||
useEffect(() => {
|
||||
initTheme(isDarkMode, updateDarkMode)
|
||||
initLocale(locale, updateLocale)
|
||||
})
|
||||
setTimeout(() => {
|
||||
console.log('初始化', hasInit, isDarkMode)
|
||||
if (!hasInit) {
|
||||
hasInit = true
|
||||
initLocale(locale, updateLocale)
|
||||
initDarkMode(isDarkMode, updateDarkMode)
|
||||
}
|
||||
}, 1000)
|
||||
|
||||
return (
|
||||
<GlobalContext.Provider value={{ onLoading, locale, isDarkMode, updateDarkMode }}>
|
||||
|
||||
Reference in New Issue
Block a user