import { useContext, useEffect, useState } from 'react'; import {getQueries, split} from '@ywfe/utils'; import { RouterContextProps } from './router.interface'; import { RouterContext } from './context'; import { matchParams } from './utils'; export const useLocation = (): Record => { const { pathname, search, host, hash } = location; return { pathname: '/' + split('/', pathname).join('/'), search, hash, host, url: pathname + search }; }; export const useParams = (): Record => { const { pathname } = useLocation(); return matchParams(pathname, '/:moduleName/:moduleId/:menu?'); }; export const useQuery = () => { const {search} = useLocation(); return getQueries(search); } export const useRoute = (path: string) => { const { store } = useContext(RouterContext); const [state] = store(); const { pathname } = useLocation(); const { moduleId } = useParams(); const [isCurrent, setIsCurrent] = useState(false); useEffect(() => { if(moduleId){ setIsCurrent(path === state.current.path || path === state.current.default); } else { setIsCurrent(pathname === path); } }, [pathname, moduleId, state.current]) return isCurrent }