2022-01-02 17:16:15 +01:00
|
|
|
import { useState } from 'react'
|
2022-05-31 10:12:54 +02:00
|
|
|
import get from 'lodash.get'
|
2022-01-02 17:16:15 +01:00
|
|
|
import set from 'lodash.set'
|
|
|
|
// Stores state in local storage
|
|
|
|
import useLocalStorage from 'shared/hooks/useLocalStorage.js'
|
2022-06-06 13:12:34 -05:00
|
|
|
import useTheme from 'shared/hooks/useTheme'
|
2022-01-02 17:16:15 +01:00
|
|
|
// Prebuild navigation
|
|
|
|
import prebuildNavigation from 'site/prebuild/navigation.js'
|
2022-05-25 18:35:20 +02:00
|
|
|
// Translation
|
2022-05-29 11:58:48 +02:00
|
|
|
import { useRouter } from 'next/router'
|
2022-05-25 18:35:20 +02:00
|
|
|
import { useTranslation } from 'next-i18next'
|
|
|
|
|
2022-12-28 21:04:30 +01:00
|
|
|
/*
|
|
|
|
* Dumb method to generate a unique (enough) ID for submissions to bugsnag
|
|
|
|
*/
|
|
|
|
function errId() {
|
|
|
|
let result = ''
|
|
|
|
const characters = 'abcdefghijklmnopqrstuvwxyz0123456789'
|
|
|
|
const charactersLength = characters.length
|
|
|
|
for (let s = 0; s < 3; s++) {
|
|
|
|
for (let i = 0; i < 4; i++) {
|
|
|
|
result += characters.charAt(Math.floor(Math.random() * charactersLength))
|
|
|
|
}
|
|
|
|
if (s < 2) result += '-'
|
|
|
|
}
|
|
|
|
|
|
|
|
return result
|
|
|
|
}
|
|
|
|
|
2022-05-25 18:35:20 +02:00
|
|
|
/*
|
|
|
|
* Helper method for a simple navigation item
|
|
|
|
*/
|
2022-12-27 18:20:53 +01:00
|
|
|
const simpleNav = (term, t, lng, prefix = '', order = '') => ({
|
2022-05-25 18:35:20 +02:00
|
|
|
__title: t(term, { lng }),
|
|
|
|
__linktitle: t(term, { lng }),
|
2022-12-27 18:20:53 +01:00
|
|
|
__slug: prefix + term,
|
|
|
|
__order: order + t(term, { lng }),
|
2022-05-25 18:35:20 +02:00
|
|
|
})
|
2022-01-02 17:16:15 +01:00
|
|
|
|
2022-05-25 18:35:20 +02:00
|
|
|
/*
|
|
|
|
* Generated the static navigation
|
|
|
|
* Static means not mdx, not strapi
|
|
|
|
*/
|
|
|
|
const staticNavigation = (t, lang) => ({
|
2022-05-26 19:34:31 +02:00
|
|
|
designs: simpleNav('designs', t, lang, '', 'A'),
|
2022-05-25 18:35:20 +02:00
|
|
|
community: simpleNav('community', t, lang),
|
|
|
|
account: simpleNav('account', t, lang),
|
|
|
|
})
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Merges prebuild navigation with the static navigation
|
|
|
|
*/
|
2022-05-26 19:34:31 +02:00
|
|
|
const buildNavigation = (lang, t) => {
|
|
|
|
const nav = {
|
|
|
|
...prebuildNavigation[lang],
|
|
|
|
...staticNavigation(t, lang),
|
|
|
|
}
|
|
|
|
|
|
|
|
// Set top-level order
|
|
|
|
nav.designs.__order = 'a'
|
|
|
|
nav.showcase.__order = 'b'
|
|
|
|
nav.docs.__order = 'c'
|
|
|
|
nav.community.__order = 'd'
|
|
|
|
nav.blog.__order = 'e'
|
|
|
|
nav.account.__order = 'f'
|
|
|
|
|
2022-05-29 12:52:11 +02:00
|
|
|
// Translation top-level strapi pages
|
|
|
|
nav.showcase.__title = t('showcase')
|
|
|
|
nav.showcase.__linktitle = t('showcase')
|
|
|
|
nav.blog.__title = t('blog')
|
|
|
|
nav.blog.__linktitle = t('blog')
|
|
|
|
|
|
|
|
// Translation top-level strapi pages
|
|
|
|
nav.community.__title = t('community')
|
|
|
|
nav.community.__linktitle = t('community')
|
|
|
|
|
2022-05-26 19:34:31 +02:00
|
|
|
return nav
|
|
|
|
}
|
2022-05-25 18:35:20 +02:00
|
|
|
|
|
|
|
/*
|
|
|
|
* The actual hook
|
|
|
|
*/
|
2022-12-28 21:04:30 +01:00
|
|
|
function useApp({ bugsnag }) {
|
2022-05-25 18:35:20 +02:00
|
|
|
// Load translation method
|
2022-05-29 11:58:48 +02:00
|
|
|
const locale = useRouter().locale
|
2022-05-25 18:35:20 +02:00
|
|
|
const { t } = useTranslation()
|
|
|
|
|
2022-01-02 17:16:15 +01:00
|
|
|
// Persistent state
|
|
|
|
const [account, setAccount] = useLocalStorage('account', { username: false })
|
2023-01-15 10:51:45 +01:00
|
|
|
const [token, setToken] = useLocalStorage('token', null)
|
2022-06-06 13:12:34 -05:00
|
|
|
const [theme, setTheme] = useTheme()
|
2022-12-27 18:20:53 +01:00
|
|
|
const [username, setUsername] = useLocalStorage('username', false)
|
2022-01-02 17:16:15 +01:00
|
|
|
|
|
|
|
// React State
|
|
|
|
const [primaryMenu, setPrimaryMenu] = useState(false)
|
2022-05-29 11:58:48 +02:00
|
|
|
const [navigation, setNavigation] = useState(buildNavigation(locale, t))
|
2022-01-02 17:16:15 +01:00
|
|
|
const [slug, setSlug] = useState('/')
|
|
|
|
const [loading, setLoading] = useState(false)
|
|
|
|
|
|
|
|
// State methods
|
|
|
|
const togglePrimaryMenu = () => setPrimaryMenu(!primaryMenu)
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Hot-update navigation method
|
|
|
|
*/
|
|
|
|
const updateNavigation = (path, content) => {
|
|
|
|
if (typeof path === 'string') {
|
2022-12-27 18:20:53 +01:00
|
|
|
path = path.slice(0, 1) === '/' ? path.slice(1).split('/') : path.split('/')
|
2022-01-02 17:16:15 +01:00
|
|
|
}
|
|
|
|
setNavigation(set(navigation, path, content))
|
|
|
|
}
|
|
|
|
|
2022-05-31 10:12:54 +02:00
|
|
|
/*
|
|
|
|
* Helper method to get title from navigation structure
|
|
|
|
*/
|
2022-12-27 18:20:53 +01:00
|
|
|
const getTitle = (slug) => get(navigation, slug).__title
|
2022-05-31 10:12:54 +02:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Helper method to construct breadcrumb from navigation structure
|
|
|
|
*/
|
2022-12-27 18:20:53 +01:00
|
|
|
const getBreadcrumb = (slug) => [get(navigation, slug).__title, `/${slug}`]
|
|
|
|
|
|
|
|
const loadHelpers = {
|
|
|
|
startLoading: () => setLoading(true),
|
|
|
|
stopLoading: () => setLoading(false),
|
|
|
|
loading,
|
|
|
|
setLoading,
|
|
|
|
}
|
2022-05-31 10:12:54 +02:00
|
|
|
|
2022-12-28 21:04:30 +01:00
|
|
|
const error = (err) => {
|
|
|
|
const id = errId
|
|
|
|
bugsnag.notify(err, (evt) => {
|
|
|
|
evt.setUser(username ? username : '__visitor')
|
|
|
|
evt.context = id
|
|
|
|
})
|
|
|
|
|
|
|
|
return id
|
|
|
|
}
|
|
|
|
|
2022-01-02 17:16:15 +01:00
|
|
|
return {
|
|
|
|
// Static vars
|
2022-05-29 11:58:48 +02:00
|
|
|
site: 'org',
|
|
|
|
|
|
|
|
// i18n
|
|
|
|
locale,
|
2022-01-02 17:16:15 +01:00
|
|
|
|
|
|
|
// State
|
2023-01-09 21:02:08 +01:00
|
|
|
account,
|
2023-01-15 10:51:45 +01:00
|
|
|
token,
|
2022-01-02 17:16:15 +01:00
|
|
|
loading,
|
|
|
|
navigation,
|
|
|
|
primaryMenu,
|
|
|
|
slug,
|
|
|
|
theme,
|
2022-12-27 18:20:53 +01:00
|
|
|
username,
|
2022-01-02 17:16:15 +01:00
|
|
|
|
|
|
|
// State setters
|
2023-01-09 21:02:08 +01:00
|
|
|
setAccount,
|
2023-01-15 10:51:45 +01:00
|
|
|
setToken,
|
2022-01-02 17:16:15 +01:00
|
|
|
setLoading,
|
|
|
|
setNavigation,
|
|
|
|
setPrimaryMenu,
|
|
|
|
setSlug,
|
|
|
|
setTheme,
|
2022-12-27 18:20:53 +01:00
|
|
|
setUsername,
|
|
|
|
startLoading: () => {
|
|
|
|
setLoading(true)
|
|
|
|
setPrimaryMenu(false)
|
|
|
|
}, // Always close menu when navigating
|
2022-01-02 17:16:15 +01:00
|
|
|
stopLoading: () => setLoading(false),
|
|
|
|
updateNavigation,
|
|
|
|
|
|
|
|
// State handlers
|
|
|
|
togglePrimaryMenu,
|
2022-05-31 10:12:54 +02:00
|
|
|
|
|
|
|
// Navigation
|
|
|
|
getTitle,
|
|
|
|
getBreadcrumb,
|
2022-12-27 18:20:53 +01:00
|
|
|
|
|
|
|
// Loading state helpers
|
|
|
|
loadHelpers,
|
2022-12-28 21:04:30 +01:00
|
|
|
|
|
|
|
// Bugsnag wrapper
|
|
|
|
error,
|
|
|
|
errId,
|
2022-01-02 17:16:15 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
export default useApp
|