Commit 01f97f3f authored by ThinhNC's avatar ThinhNC

chore: remove custom ZMPRouter file

parent 8068b030
Pipeline #53712 passed with stage
in 32 seconds
import React, { useCallback, useMemo, useRef, useState } from "react";
import { BrowserRouter, MemoryRouter } from "react-router-dom";
export interface AnimationRouterContextType {
animate: boolean;
direction: string;
setAnimate: (config: { animate?: boolean; direction?: string }) => void;
pageScrollPosition: Record<string, number>;
updatePosition: (key: string, position: number) => void;
}
export const AnimationRouterContext = React.createContext<AnimationRouterContextType | undefined>(undefined);
export interface ZMPRouterProps {
children?: React.ReactNode;
memoryRouter?: boolean;
}
const PAGE_TRANSITION_FORWARD = "forward";
const PAGE_TRANSITION_BACKWARD = "backward";
export const ZMPRouter: React.FC<ZMPRouterProps> = (props) => {
const [animate, setAnimate] = useState(true);
const [direction, setDirection] = useState(PAGE_TRANSITION_FORWARD);
const pageScrollPosition = useRef<Record<string, number>>({});
const { children, memoryRouter } = props;
// Determine basepath safely:
// Inside Zalo webview, the path begins with `/zapps/<APP_ID>`.
// On Vercel / standalone web, use "" so all standard routes (/, /login, etc.) match.
const win = typeof window !== "undefined" ? (window as unknown as Record<string, unknown>) : null;
let basepath = (win?.BASE_PATH as string) || "";
const pathname = typeof window !== "undefined" ? window.location.pathname : "";
const appId = (win?.APP_ID as string) || "";
if (pathname.startsWith("/zapps/") && appId) {
basepath = `/zapps/${appId}`;
} else if (!basepath) {
basepath = "";
}
const handleSetAnimate = useCallback(
({ animate: ani = true, direction: dir = PAGE_TRANSITION_FORWARD }: { animate?: boolean; direction?: string }) => {
setAnimate(Boolean(ani));
const correctDirection = [PAGE_TRANSITION_FORWARD, PAGE_TRANSITION_BACKWARD].includes(dir)
? dir
: PAGE_TRANSITION_FORWARD;
setDirection(correctDirection);
},
[]
);
const updateScrollPosition = (key: string, position: number) => {
pageScrollPosition.current[key] = position;
};
const contextValue = useMemo(
() => ({
setAnimate: handleSetAnimate,
animate,
direction,
pageScrollPosition: pageScrollPosition.current,
updatePosition: updateScrollPosition,
}),
[animate, direction, handleSetAnimate]
);
const RouterComponent = memoryRouter ? MemoryRouter : BrowserRouter;
const routerProps = memoryRouter ? { initialEntries: [basepath || "/"] } : {};
return (
<AnimationRouterContext.Provider value={contextValue}>
<RouterComponent basename={basepath} {...routerProps}>
{children}
</RouterComponent>
</AnimationRouterContext.Provider>
);
};
export default ZMPRouter;
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment