import React from "react"; import { createBrowserHistory, createMemoryHistory } from "history"; import { Environment, loadQuery, PreloadedQuery } from "react-relay"; import { OperationType, VariablesOf } from "relay-runtime"; import { isElectron, isNotebook, NotFoundError, Resource, } from "@fiftyone/utilities"; import { Route } from ".."; import RouteDefinition, { RouteBase } from "./RouteDefinition"; import { getEnvironment } from "../use/useRouter"; import { MatchPathResult, matchPath } from "./matchPath"; export interface RouteData< T extends OperationType | undefined = OperationType > { isExact: boolean; path: string; url: string; variables: T extends OperationType ? VariablesOf : undefined; } export interface Entry { pathname: string; state: any; entries: { component: Resource>; prepared?: Resource>; routeData: RouteData; }[]; } export interface RoutingContext< T extends OperationType | undefined = OperationType > { history: ReturnType; get: () => Entry; pathname: string; state: any; subscribe: (cb: (entry: Entry) => void) => () => void; } interface Match { route: RouteDefinition; match: RouteData; } export interface Router { cleanup: () => void; context: RoutingContext; } export const createRouter = ( environment: Environment, routes: RouteDefinition[], { errors } = { errors: true, } ): Router => { const history = isElectron() || isNotebook() ? createMemoryHistory() : createBrowserHistory(); let currentEntry: Entry; let nextId = 0; const subscribers = new Map(); const cleanup = history.listen(({ location }) => { if (!currentEntry) { return; } const matches = matchRoute( routes, location.pathname, errors, location.state?.variables as Partial> ); const entries = prepareMatches(environment, matches); const nextEntry: Entry = { pathname: location.pathname, state: location.state, entries, }; currentEntry = nextEntry; subscribers.forEach((cb) => cb(nextEntry)); }); const context: RoutingContext = { history, get() { if (!currentEntry) { currentEntry = { pathname: history.location.pathname, state: history.location.state, entries: prepareMatches( environment, matchRoute( routes, history.location.pathname, errors, history.location.state?.variables as Partial> ) ), }; } return currentEntry; }, get pathname() { return history.location.pathname; }, get state() { return history.location.state; }, subscribe(cb) { const id = nextId++; const dispose = () => { subscribers.delete(id); }; subscribers.set(id, cb); return dispose; }, }; return { cleanup, context, }; }; export const matchRoutes = < T extends OperationType | undefined = OperationType >( routes: RouteBase[], pathname: string, variables: T extends OperationType ? Partial> : undefined, branch: { route: RouteBase; match: MatchPathResult }[] = [] ): { route: RouteBase; match: MatchPathResult }[] => { routes.some((route) => { const match = route.path ? matchPath(pathname, route, variables) : branch.length ? branch[branch.length - 1].match : ({ path: "/", url: "/", variables, isExact: pathname === "/", } as MatchPathResult); if (match) { branch.push({ route, match }); if (route.children) { matchRoutes(route.children, pathname, variables, branch); } } return match; }); return branch; }; const matchRoute = ( routes: RouteDefinition[], pathname: string, errors: boolean, variables: T extends OperationType ? Partial> : undefined ): Match[] => { const matchedRoutes = matchRoutes(routes, pathname, variables); if ( errors && matchedRoutes && matchedRoutes.every(({ match }) => !match.isExact) ) { throw new NotFoundError(pathname); } return matchedRoutes; }; const prepareMatches = ( environment: Environment, matches: Match[] ) => { return matches ? matches.map((match) => { const { route, match: matchData } = match; const query = route.query?.get(); const component = route.component.get(); if (component == null) { route.component.load(); } if (route.query && query == null) { route.query.load(); } const routeQuery = route.query; if (routeQuery !== undefined) { const prepared = new Resource(() => routeQuery.load().then((q) => { return loadQuery(environment, q, matchData.variables || {}, { fetchPolicy: "network-only", }); }) ); prepared.load(); return { component: route.component, prepared, routeData: matchData, }; } return { component: route.component, routeData: matchData, }; }) : []; }; export const RouterContext = React.createContext( createRouter(getEnvironment(), [], { errors: false }).context );