import { useEffect, useCallback } from 'react' import { useStoreState, useStoreActions } from '@/store' import { useLocation } from 'react-router-dom' import type { LayoutType } from '@/@types/theme' import type { ComponentType } from 'react' export type AppRouteProps = { component: ComponentType routeKey: string layout?: LayoutType } const AppRoute = >({ component: Component, routeKey, ...props }: AppRouteProps) => { const location = useLocation() const { setCurrentRouteKey } = useStoreActions((actions) => actions.base.common) const { setLayout, setPreviousLayout } = useStoreActions((actions) => actions.theme) const layoutType = useStoreState((state) => state.theme.layout.type) const previousLayout = useStoreState((state) => state.theme.layout.previousType) const handleLayoutChange = useCallback(() => { setCurrentRouteKey(routeKey) if (props.layout && props.layout !== layoutType) { setPreviousLayout(layoutType) setLayout(props.layout) } if (!props.layout && previousLayout && layoutType !== previousLayout) { setLayout(previousLayout) setPreviousLayout('') } }, [ layoutType, previousLayout, props.layout, routeKey, setCurrentRouteKey, setLayout, setPreviousLayout, ]) useEffect(() => { handleLayoutChange() }, [location, handleLayoutChange]) return } export default AppRoute