1
0
Fork 0
freesewing/sites/org/hooks/useApp.mjs

217 lines
5.4 KiB
JavaScript
Raw Normal View History

import { useState } from 'react'
import get from 'lodash.get'
import set from 'lodash.set'
// Stores state in local storage
2023-02-05 16:39:18 +01:00
import { useLocalStorage } from 'shared/hooks/useLocalStorage.mjs'
import { useTheme } from 'shared/hooks/useTheme.mjs'
// Prebuild navigation
2023-02-05 16:39:18 +01:00
import { prebuildNavigation } from 'site/prebuild/navigation.mjs'
2022-05-25 18:35:20 +02:00
// Translation
import { useRouter } from 'next/router'
2022-05-25 18:35:20 +02:00
import { useTranslation } from 'next-i18next'
import toastMethod from 'react-hot-toast'
import { Toast } from 'site/components/toast/index.mjs'
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-05-25 18:35:20 +02:00
/*
* Generated the static navigation
* Static means not mdx, not strapi
*/
const staticNavigation = (t, lang) => ({
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
*/
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')
return nav
}
2022-05-25 18:35:20 +02:00
/* Custom toast methods */
const toastMethods = (t) => ({
info: (children) => toastMethod.custom(<Toast type="info">{children}</Toast>),
warning: (children) => toastMethod.custom(<Toast type="warning">{children}</Toast>),
error: (children) => toastMethod.custom(<Toast type="error">{children}</Toast>),
accent: (children) => toastMethod.custom(<Toast type="accent">{children}</Toast>),
success: (children) => toastMethod.custom(<Toast type="success">{children}</Toast>),
for: {
settingsSaved: () =>
toastMethod.custom(
<Toast type="success">
<span>{t('settingsSaved')}</span>
</Toast>
),
},
})
// Make it possible to always check for app.account.username
const noAccount = { username: false }
2022-05-25 18:35:20 +02:00
/*
* The actual hook
*/
export function useApp({ bugsnag }) {
2022-05-25 18:35:20 +02:00
// Load translation method
const locale = useRouter().locale
const { t } = useTranslation(['toast'])
2022-05-25 18:35:20 +02:00
// Persistent state
const [account, setAccount, accountReady] = useLocalStorage('account', noAccount)
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()
// React State
const [primaryMenu, setPrimaryMenu] = useState(false)
const [navigation, setNavigation] = useState(buildNavigation(locale, t))
const [slug, setSlug] = useState('/')
const [modal, setModal] = useState(false)
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('/')
}
setNavigation(set(navigation, path, content))
}
/*
* Helper method to get title from navigation structure
*/
2022-12-27 18:20:53 +01:00
const getTitle = (slug) => get(navigation, slug).__title
/*
* 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-12-28 21:04:30 +01:00
const error = (err) => {
const id = errId
bugsnag.notify(err, (evt) => {
2023-01-22 19:46:56 +01:00
evt.setUser(account.username ? account.username : '__visitor')
2022-12-28 21:04:30 +01:00
evt.context = id
})
return id
}
// Clear user data (when loggin in as a different user, this gets called)
const clear = () => {
setAccount({ username: false })
}
return {
// Static vars
site: 'org',
// i18n
locale,
// State
2023-01-09 21:02:08 +01:00
account,
2023-01-27 20:39:09 +01:00
accountReady,
2023-01-15 10:51:45 +01:00
token,
loading,
navigation,
primaryMenu,
slug,
theme,
modal,
// State setters
2023-01-09 21:02:08 +01:00
setAccount,
2023-01-15 10:51:45 +01:00
setToken,
setLoading,
setNavigation,
setPrimaryMenu,
setSlug,
setTheme,
2022-12-27 18:20:53 +01:00
startLoading: () => {
setLoading(true)
setPrimaryMenu(false)
}, // Always close menu when navigating
stopLoading: () => setLoading(false),
updateNavigation,
setModal,
clear,
// State handlers
togglePrimaryMenu,
toast: toastMethods(t),
// 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,
}
}