2022-12-18 08:29:16 +09:00
|
|
|
import { pwaInfo } from 'virtual:pwa-info'
|
|
|
|
import type { Link } from '@unhead/schema'
|
2022-12-27 00:12:04 +09:00
|
|
|
import type { Directions } from 'vue-i18n-routing'
|
2022-12-28 10:28:17 +09:00
|
|
|
import { APP_NAME, COOKIE_MAX_AGE, STORAGE_KEY_LANG } from '~/constants'
|
2022-12-27 00:12:04 +09:00
|
|
|
import type { LocaleObject } from '#i18n'
|
2022-11-28 18:01:14 +09:00
|
|
|
|
2022-11-30 09:22:35 +09:00
|
|
|
export function setupPageHeader() {
|
2022-12-07 06:09:47 +09:00
|
|
|
const isDev = process.dev
|
|
|
|
const isPreview = useRuntimeConfig().public.env === 'staging'
|
|
|
|
|
2022-11-30 06:13:43 +09:00
|
|
|
const i18n = useI18n()
|
2022-11-30 09:22:35 +09:00
|
|
|
|
2022-12-18 08:29:16 +09:00
|
|
|
const link: Link[] = []
|
|
|
|
|
|
|
|
if (pwaInfo && pwaInfo.webManifest) {
|
|
|
|
const { webManifest } = pwaInfo
|
|
|
|
if (webManifest) {
|
|
|
|
const { href, useCredentials } = webManifest
|
|
|
|
if (useCredentials) {
|
|
|
|
link.push({
|
|
|
|
rel: 'manifest',
|
|
|
|
href,
|
|
|
|
crossorigin: 'use-credentials',
|
|
|
|
})
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
link.push({
|
|
|
|
rel: 'manifest',
|
|
|
|
href,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-12-27 00:12:04 +09:00
|
|
|
const localeMap = (i18n.locales.value as LocaleObject[]).reduce((acc, l) => {
|
2022-12-28 06:04:52 +09:00
|
|
|
acc[l.code!] = l.dir ?? 'auto'
|
2022-12-27 00:12:04 +09:00
|
|
|
return acc
|
|
|
|
}, {} as Record<string, Directions>)
|
|
|
|
|
2022-11-30 07:49:25 +09:00
|
|
|
useHeadFixed({
|
2022-11-30 06:13:43 +09:00
|
|
|
htmlAttrs: {
|
|
|
|
lang: () => i18n.locale.value,
|
2022-12-28 06:04:52 +09:00
|
|
|
dir: () => localeMap[i18n.locale.value] ?? 'auto',
|
2022-11-30 06:13:43 +09:00
|
|
|
},
|
2022-11-28 18:01:14 +09:00
|
|
|
titleTemplate: title => `${title ? `${title} | ` : ''}${APP_NAME}${isDev ? ' (dev)' : isPreview ? ' (preview)' : ''}`,
|
2022-12-18 08:29:16 +09:00
|
|
|
link,
|
2022-11-28 18:01:14 +09:00
|
|
|
})
|
|
|
|
}
|
2022-11-30 09:22:35 +09:00
|
|
|
|
|
|
|
export async function setupI18n() {
|
2022-12-02 11:17:41 +09:00
|
|
|
const { locale, setLocale, locales } = useI18n()
|
2022-12-28 10:28:17 +09:00
|
|
|
const cookieLocale = useCookie(STORAGE_KEY_LANG, { maxAge: COOKIE_MAX_AGE })
|
|
|
|
const isFirstVisit = cookieLocale.value == null
|
2022-12-18 01:55:29 +09:00
|
|
|
|
2022-12-28 10:28:17 +09:00
|
|
|
if (process.client && isFirstVisit) {
|
|
|
|
const userLang = (navigator.language || 'en-US').toLowerCase()
|
|
|
|
// cause vue-i18n not explicit export LocaleObject type
|
|
|
|
const supportLocales = unref(locales) as { code: string }[]
|
|
|
|
const lang = supportLocales.find(locale => userLang.startsWith(locale.code.toLowerCase()))?.code
|
2022-12-02 11:17:41 +09:00
|
|
|
|| supportLocales.find(locale => userLang.startsWith(locale.code.split('-')[0]))?.code
|
2022-12-28 10:28:17 +09:00
|
|
|
cookieLocale.value = lang || 'en-US'
|
|
|
|
}
|
2022-12-02 11:17:41 +09:00
|
|
|
|
2022-12-28 10:28:17 +09:00
|
|
|
if (cookieLocale.value && cookieLocale.value !== locale.value)
|
|
|
|
await setLocale(cookieLocale.value)
|
2022-11-30 09:22:35 +09:00
|
|
|
|
2022-12-28 10:28:17 +09:00
|
|
|
if (process.client) {
|
2022-12-18 01:55:29 +09:00
|
|
|
watchEffect(() => {
|
2022-12-28 10:28:17 +09:00
|
|
|
cookieLocale.value = locale.value
|
2022-12-18 01:55:29 +09:00
|
|
|
})
|
2022-12-28 10:28:17 +09:00
|
|
|
}
|
2022-11-30 09:22:35 +09:00
|
|
|
}
|
2022-12-28 03:37:22 +09:00
|
|
|
|
2022-12-28 04:19:47 +09:00
|
|
|
export async function setupEmojis() {
|
2022-12-28 05:56:43 +09:00
|
|
|
if (process.client) {
|
|
|
|
const promise = import('@emoji-mart/data').then(r => r.default)
|
|
|
|
const { init } = await import('emoji-mart')
|
|
|
|
init({
|
|
|
|
data: () => promise,
|
|
|
|
})
|
|
|
|
}
|
2022-12-28 03:37:22 +09:00
|
|
|
}
|