Files
NotionNext/lib/global.js
tangly1024 4b53a3612c feature:
封装useGlobal组件,存放全局变量;
新增search页面,修复搜索页无法分页的bug
2021-11-04 13:28:08 +08:00

85 lines
2.2 KiB
JavaScript
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
import BLOG from '@/blog.config'
import lang from './lang'
import { useContext, createContext, useState, useEffect } from 'react'
import cookie from 'react-cookies'
const GlobalContext = createContext()
/**
* 全局变量Provider包括语言本地化、样式主题、搜索词
* @param children
* @returns {JSX.Element}
* @constructor
*/
export function GlobalContextProvider ({ children }) {
const locale = getCurrentLocale()
const [theme, changeTheme] = useState(loadUserThemeFromCookies())
useEffect(() => { initTheme(theme, changeTheme) })
return (
<GlobalContext.Provider value={{ locale, theme, changeTheme }}>{children}</GlobalContext.Provider>
)
}
/**
* 获取当前语言字典
* @returns 不同语言对应字典
*/
const getCurrentLocale = () => {
if (BLOG.lang.slice(0, 2).toLowerCase() === 'zh') {
switch (BLOG.lang.toLowerCase()) {
case 'zh-cn':
case 'zh-sg':
return lang['zh-CN']
case 'zh-hk':
return lang['zh-HK']
case 'zh-tw':
return lang['zh-TW']
default:
return lang['zh-TW']
}
} else {
return lang.en
}
}
/**
* 初始化主题
* @param theme 用户默认主题state
* @param changeTheme 更改主题ChangeState函数
* @description 主题样式相关 由于Server采用服务端静态渲染无法获取前端Cookie配置故在渲染hooks中做初始化主题
*/
const initTheme = (theme, changeTheme) => {
if (!theme) {
const date = new Date()
const prefersDarkMode = window.matchMedia('(prefers-color-scheme: dark)').matches
const useDark = prefersDarkMode || (date.getHours() >= 18 || date.getHours() < 6)
if (useDark) {
changeTheme('dark')
saveTheme('dark')
}
}
const baseLayoutClass = document.getElementById('wrapper').classList
if (!baseLayoutClass.contains(theme)) {
baseLayoutClass.add(theme)
}
}
/**
* 读取默认主题
* @returns {*}
*/
export const loadUserThemeFromCookies = () => {
return cookie.load('theme')
}
/**
* 保存默认主题
* @param newTheme
*/
export const saveTheme = (newTheme) => {
cookie.save('theme', newTheme, { path: '/' })
}
export const useGlobal = () => useContext(GlobalContext)