dify/web/app/components/next-route-state/atoms.ts
Stephen Zhou a84c2d36a3
style: format with vp fmt (#38803)
Co-authored-by: autofix-ci[bot] <114827586+autofix-ci[bot]@users.noreply.github.com>
2026-07-12 15:57:46 +00:00

51 lines
1.5 KiB
TypeScript

import { atom } from 'jotai'
export type NextRouteParams = Record<string, string | string[]>
type NextRouteState = {
pathname: string
params: NextRouteParams
}
// Mirrors Next router state. NextRouteStateBridge force-hydrates this atom on
// render so feature atoms can read route state without calling router hooks.
const nextRouteStateAtom = atom<NextRouteState>({
pathname: '',
params: {},
})
function normalizedParamEntries(params: NextRouteParams) {
return Object.keys(params)
.sort()
.map((key) => {
const value = params[key]
return [key, Array.isArray(value) ? [...value] : value] as const
})
}
function normalizeNextRouteParams(params: NextRouteParams): NextRouteParams {
return Object.fromEntries(normalizedParamEntries(params)) as NextRouteParams
}
function routeParamsKey(params: NextRouteParams) {
return JSON.stringify(normalizedParamEntries(params))
}
export const nextParamsAtom = atom((get) => get(nextRouteStateAtom).params)
export const nextPathnameAtom = atom((get) => get(nextRouteStateAtom).pathname)
export const setNextRouteStateAtom = atom(null, (get, set, routeState: NextRouteState) => {
const nextParams = normalizeNextRouteParams(routeState.params)
const currentRouteState = get(nextRouteStateAtom)
if (
currentRouteState.pathname !== routeState.pathname ||
routeParamsKey(currentRouteState.params) !== routeParamsKey(nextParams)
) {
set(nextRouteStateAtom, {
pathname: routeState.pathname,
params: nextParams,
})
}
})