74 lines
2.1 KiB
TypeScript
74 lines
2.1 KiB
TypeScript
import {
|
|
appBasePath,
|
|
appPathFromLocalRoute,
|
|
} from "@agent-native/core/client/api-path";
|
|
import { useAgentRouteState } from "@agent-native/core/client/navigation";
|
|
|
|
import { TAB_ID } from "@/lib/tab-id";
|
|
|
|
export interface NavigationState {
|
|
view?: string;
|
|
path?: string;
|
|
publicPath?: string;
|
|
}
|
|
|
|
export function useNavigationState() {
|
|
useAgentRouteState<NavigationState>({
|
|
browserTabId: TAB_ID,
|
|
requestSource: TAB_ID,
|
|
getNavigationState: ({ pathname }) => ({
|
|
view: viewForPath(pathname),
|
|
path: pathname,
|
|
publicPath: appPathFromLocalRoute(pathname),
|
|
}),
|
|
getCommandPath: commandPathForNavigation,
|
|
});
|
|
}
|
|
|
|
export function commandPathForNavigation(command: NavigationState): string {
|
|
const fallbackPath = pathForView(command?.view);
|
|
const path = typeof command?.path === "string" ? command.path : "";
|
|
if (!path) return fallbackPath;
|
|
|
|
const basePath = appBasePath();
|
|
if (!basePath) return path;
|
|
|
|
const suffixIndex = path.search(/[?#]/);
|
|
const pathname = suffixIndex === -1 ? path : path.slice(0, suffixIndex);
|
|
const suffix = suffixIndex === -1 ? "" : path.slice(suffixIndex);
|
|
const publicPath =
|
|
typeof command?.publicPath === "string" ? command.publicPath : "";
|
|
const publicPathSuffixIndex = publicPath.search(/[?#]/);
|
|
const declaredPublicPath =
|
|
publicPathSuffixIndex === -1
|
|
? publicPath
|
|
: publicPath.slice(0, publicPathSuffixIndex);
|
|
const publicViewPath = appPathFromLocalRoute(pathForView(command?.view));
|
|
if (
|
|
matchesPathPrefix(pathname, basePath) &&
|
|
(matchesPathPrefix(pathname, publicViewPath) ||
|
|
(declaredPublicPath && declaredPublicPath === pathname))
|
|
) {
|
|
return `${pathname.slice(basePath.length) || "/"}${suffix}`;
|
|
}
|
|
return path;
|
|
}
|
|
|
|
function matchesPathPrefix(pathname: string, prefix: string): boolean {
|
|
return pathname === prefix || pathname.startsWith(`${prefix}/`);
|
|
}
|
|
|
|
function viewForPath(pathname: string): string {
|
|
if (pathname === "/") return "home";
|
|
return "home";
|
|
}
|
|
|
|
function pathForView(view?: string): string {
|
|
switch (view) {
|
|
case "home":
|
|
return "/";
|
|
default:
|
|
return "/";
|
|
}
|
|
}
|