chore(org): Moved useApp hook to shared hook
This commit is contained in:
parent
89617bef92
commit
264e7a0b9d
36 changed files with 37 additions and 35 deletions
|
@ -1,55 +0,0 @@
|
|||
// 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(bugsnag)
|
||||
|
||||
// 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,
|
||||
}
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue