1
0
Fork 0
freesewing/sites/shared/hooks/use-app.mjs

56 lines
1.2 KiB
JavaScript
Raw Normal View History

2023-03-24 16:33:14 +01:00
// Hooks
import { useState } from 'react'
import { useBugsnag } from 'site/hooks/use-bugsnag.mjs'
import { useRouter } from 'next/router'
// Dependencies
import set from 'lodash.set'
import unset from 'lodash.unset'
const defaultState = {
loading: false,
modal: null,
}
/*
* The useApp hook
*/
export function useApp({ bugsnag }) {
const router = useRouter()
const reportError = useBugsnag ? useBugsnag(bugsnag) : () => false
2023-03-24 16:33:14 +01:00
// React state
const [state, setState] = useState({
...defaultState,
slug: useRouter().asPath.slice(1),
})
/*
* Helper methods for partial state updates
*/
const updateState = (path, value) => setState(set({ ...state }, path, value))
const unsetState = (path) => setState(unset({ ...state }, path))
/*
* Helper methods for specific state updates
*/
const clearModal = () => updateState('modal', null)
const startLoading = () => updateState('loading', true)
const stopLoading = () => updateState('loading', false)
return {
// All-purpose React state object
state,
setState,
updateState,
unsetState,
// Helper methods
clearModal,
startLoading,
stopLoading,
// Bugsnag helper
reportError,
}
}