2024-04-02 10:29:34 -04:00
|
|
|
import { useRouteLoaderData } from 'react-router-dom'
|
|
|
|
import { useSettingsAuthContext } from './useSettingsAuthContext'
|
2024-08-09 02:47:25 -04:00
|
|
|
import { PATHS } from 'lib/paths'
|
2024-04-02 10:29:34 -04:00
|
|
|
import { settings } from 'lib/settings/initialSettings'
|
|
|
|
import { useEffect } from 'react'
|
|
|
|
|
|
|
|
/**
|
|
|
|
* I was dismayed to learn that index route in Router.tsx where we initially load up the settings
|
|
|
|
* doesn't re-run on subsequent navigations. This hook is a workaround,
|
|
|
|
* in conjunction with additional uses of settingsLoader further down the router tree.
|
|
|
|
* @param routeId - The id defined in Router.tsx to load the settings from.
|
|
|
|
*/
|
2024-08-09 02:47:25 -04:00
|
|
|
export function useRefreshSettings(routeId: string = PATHS.INDEX) {
|
2024-04-02 10:29:34 -04:00
|
|
|
const ctx = useSettingsAuthContext()
|
|
|
|
const routeData = useRouteLoaderData(routeId) as typeof settings
|
|
|
|
|
|
|
|
if (!ctx) {
|
2024-06-24 11:45:40 -04:00
|
|
|
// Intended to stop the world
|
|
|
|
// eslint-disable-next-line
|
2024-04-02 10:29:34 -04:00
|
|
|
throw new Error(
|
|
|
|
'useRefreshSettings must be used within a SettingsAuthProvider'
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
useEffect(() => {
|
2024-09-09 19:59:36 +03:00
|
|
|
ctx.settings.send({
|
|
|
|
type: 'Set all settings',
|
2024-04-02 10:29:34 -04:00
|
|
|
settings: routeData,
|
2024-11-16 16:49:44 -05:00
|
|
|
doNotPersist: true,
|
2024-04-02 10:29:34 -04:00
|
|
|
})
|
|
|
|
}, [])
|
|
|
|
}
|