forked from craigary/nobelium
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* New hook: useConfig * Avoid sending private fields from config file to client
- Loading branch information
1 parent
2f15a92
commit ea3bd33
Showing
33 changed files
with
278 additions
and
190 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
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,18 +1,29 @@ | ||
'use client' | ||
|
||
import { useEffect, useState } from 'react' | ||
import dayjs from 'dayjs' | ||
import localizedFormat from 'dayjs/plugin/localizedFormat' | ||
import BLOG from '@/blog.config' | ||
import { useConfig } from '@/lib/config' | ||
|
||
dayjs.extend(localizedFormat) | ||
|
||
const lang = BLOG.lang.slice(0, 2) | ||
import(`dayjs/locale/${lang}`) | ||
.then(() => { | ||
dayjs.locale(BLOG.lang.slice(0, 2)) | ||
}) | ||
.catch(() => console.warn(`dayjs locale \`${lang}\` not found`)) | ||
const loaded = {} | ||
|
||
export default function FormattedDate ({ date }) { | ||
const lang = useConfig().lang.slice(0, 2) | ||
const [isLocaleLoaded, setIsLocaleLoaded] = useState(loaded[lang] === true) | ||
|
||
useEffect(() => { | ||
if (!isLocaleLoaded) { | ||
loaded[lang] ??= import(`dayjs/locale/${lang}`).then( | ||
() => { | ||
loaded[lang] = true | ||
dayjs.locale(lang) | ||
}, | ||
() => console.warn(`dayjs locale \`${lang}\` not found`) | ||
) | ||
loaded[lang].then(() => setIsLocaleLoaded(true)) | ||
} | ||
|
||
}, [isLocaleLoaded, lang]) | ||
|
||
return <span>{dayjs(date).format('ll')}</span> | ||
} |
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,18 +1,20 @@ | ||
import { useEffect } from 'react' | ||
import { useRouter } from 'next/router' | ||
import { useConfig } from '@/lib/config' | ||
import * as gtag from '@/lib/gtag' | ||
|
||
const Gtag = () => { | ||
const config = useConfig() | ||
const router = useRouter() | ||
useEffect(() => { | ||
const handleRouteChange = url => { | ||
gtag.pageview(url) | ||
gtag.pageview(config.analytics.gaConfig.measurementId, url) | ||
} | ||
router.events.on('routeChangeComplete', handleRouteChange) | ||
return () => { | ||
router.events.off('routeChangeComplete', handleRouteChange) | ||
} | ||
}, [router.events]) | ||
}, [config, router.events]) | ||
return null | ||
} | ||
export default Gtag |
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
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,31 +1,35 @@ | ||
import Script from 'next/script' | ||
import BLOG from '@/blog.config' | ||
import { useConfig } from '@/lib/config' | ||
|
||
const Scripts = () => ( | ||
<> | ||
{BLOG.analytics && BLOG.analytics.provider === 'ackee' && ( | ||
<Script | ||
src={BLOG.analytics.ackeeConfig.tracker} | ||
data-ackee-server={BLOG.analytics.ackeeConfig.dataAckeeServer} | ||
data-ackee-domain-id={BLOG.analytics.ackeeConfig.domainId} | ||
/> | ||
)} | ||
{BLOG.analytics && BLOG.analytics.provider === 'ga' && ( | ||
<> | ||
const Scripts = () => { | ||
const BLOG = useConfig() | ||
|
||
return ( | ||
<> | ||
{BLOG.analytics && BLOG.analytics.provider === 'ackee' && ( | ||
<Script | ||
src={`https://www.googletagmanager.com/gtag/js?id=${BLOG.analytics.gaConfig.measurementId}`} | ||
src={BLOG.analytics.ackeeConfig.tracker} | ||
data-ackee-server={BLOG.analytics.ackeeConfig.dataAckeeServer} | ||
data-ackee-domain-id={BLOG.analytics.ackeeConfig.domainId} | ||
/> | ||
<Script strategy="lazyOnload" id="ga"> | ||
{`window.dataLayer = window.dataLayer || []; | ||
function gtag(){dataLayer.push(arguments);} | ||
gtag('js', new Date()); | ||
gtag('config', '${BLOG.analytics.gaConfig.measurementId}', { | ||
page_path: window.location.pathname, | ||
});`} | ||
</Script> | ||
</> | ||
)} | ||
</> | ||
) | ||
)} | ||
{BLOG.analytics && BLOG.analytics.provider === 'ga' && ( | ||
<> | ||
<Script | ||
src={`https://www.googletagmanager.com/gtag/js?id=${BLOG.analytics.gaConfig.measurementId}`} | ||
/> | ||
<Script strategy="lazyOnload" id="ga"> | ||
{`window.dataLayer = window.dataLayer || []; | ||
function gtag(){dataLayer.push(arguments);} | ||
gtag('js', new Date()); | ||
gtag('config', '${BLOG.analytics.gaConfig.measurementId}', { | ||
page_path: window.location.pathname, | ||
});`} | ||
</Script> | ||
</> | ||
)} | ||
</> | ||
) | ||
} | ||
|
||
export default Scripts |
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,51 @@ | ||
exports.FONTS_SANS = [ | ||
'"IBM Plex Sans"', | ||
'ui-sans-serif', | ||
'system-ui', | ||
'-apple-system', | ||
'BlinkMacSystemFont', | ||
'Segoe UI', | ||
'Roboto', | ||
'"Noto Sans"', | ||
'"Helvetica Neue"', | ||
'Helvetica', | ||
'"Nimbus Sans L"', | ||
'Arial', | ||
'"Liberation Sans"', | ||
'"PingFang SC"', | ||
'"Hiragino Sans GB"', | ||
'"Noto Sans CJK SC"', | ||
'"Source Han Sans SC"', | ||
'"Source Han Sans CN"', | ||
'"Microsoft YaHei"', | ||
'"Wenquanyi Micro Hei"', | ||
'"WenQuanYi Zen Hei"', | ||
'"ST Heiti"', | ||
'SimHei', | ||
'"WenQuanYi Zen Hei Sharp"', | ||
'sans-serif' | ||
] | ||
exports.FONTS_SERIF = [ | ||
'"Source Serif"', | ||
'ui-serif', | ||
'Georgia', | ||
'"Nimbus Roman No9 L"', | ||
'"Songti SC"', | ||
'"Noto Serif CJK SC"', | ||
'"Source Han Serif SC"', | ||
'"Source Han Serif CN"', | ||
'STSong', | ||
'"AR PL New Sung"', | ||
'"AR PL SungtiL GB"', | ||
'NSimSun', | ||
'SimSun', | ||
'"TW-Sung"', | ||
'"WenQuanYi Bitmap Song"', | ||
'"AR PL UMing CN"', | ||
'"AR PL UMing HK"', | ||
'"AR PL UMing TW"', | ||
'"AR PL UMing TW MBE"', | ||
'PMingLiU', | ||
'MingLiU', | ||
'serif' | ||
] |
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,15 @@ | ||
import { createContext, useContext } from 'react' | ||
|
||
const ConfigContext = createContext(undefined) | ||
|
||
export function ConfigProvider ({ value, children }) { | ||
return ( | ||
<ConfigContext.Provider value={value}> | ||
{children} | ||
</ConfigContext.Provider> | ||
) | ||
} | ||
|
||
export function useConfig () { | ||
return useContext(ConfigContext) | ||
} |
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,10 +1,12 @@ | ||
import dayjs from 'dayjs' | ||
import utc from 'dayjs/plugin/utc' | ||
import timezone from 'dayjs/plugin/timezone' | ||
import BLOG from '@/blog.config' | ||
|
||
dayjs.extend(utc) | ||
dayjs.extend(timezone) | ||
dayjs.tz.setDefault(BLOG.timezone) | ||
|
||
export function prepareDayjs (timezone) { | ||
dayjs.tz.setDefault(timezone) | ||
} | ||
|
||
export default dayjs |
Oops, something went wrong.