Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 | 3450x 118x 3407x 3407x 118x 21x 21x 1586x 1586x 1586x 22x | /**
* `react-router-dom`, as fe-core imports it — aliased here in `vite.config.ts`
* and `vitest.config.ts`, so every `import … from "react-router-dom"` in the
* app (and in fe-shared) reaches this module.
*
* Everything is react-router's own (react-router-dom v7 is only a re-export
* of it), except that `Link`, `NavLink`, `Navigate` and `useNavigate` keep
* their target inside the current workspace (`routes/workspace.ts`
* `pathIn`): a page written against `APP_ROUTES.USER` links to `/user/…`,
* and opened in an organisation the same link goes to `/organisation/<id>/…`.
* Account pages, other apps and relative links are left alone, and Personal
* changes nothing. One place, rather than wrapping each of the app's hundred
* or so links by hand — and a page added later is covered without knowing
* this exists.
*/
import React, { useCallback } from "react";
import * as RR from "react-router";
import { useWorkspace } from "@/context/WorkspaceContext";
export * from "react-router";
type PathFn = (to: string) => string;
function inWorkspace(path: PathFn, to: RR.To): RR.To {
Eif (typeof to === "string") return to.startsWith("/") ? path(to) : to;
if (to && typeof to.pathname === "string" && to.pathname.startsWith("/")) {
return { ...to, pathname: path(to.pathname) };
}
return to;
}
export const Link = React.forwardRef<HTMLAnchorElement, RR.LinkProps>(
function Link(props, ref) {
const { path } = useWorkspace();
return <RR.Link ref={ref} {...props} to={inWorkspace(path, props.to)} />;
},
);
export const NavLink = React.forwardRef<HTMLAnchorElement, RR.NavLinkProps>(
function NavLink(props, ref) {
const { path } = useWorkspace();
return <RR.NavLink ref={ref} {...props} to={inWorkspace(path, props.to)} />;
},
);
export function Navigate(props: RR.NavigateProps): React.ReactElement | null {
const { path } = useWorkspace();
return <RR.Navigate {...props} to={inWorkspace(path, props.to)} />;
}
export function useNavigate(): RR.NavigateFunction {
const navigate = RR.useNavigate();
const { path } = useWorkspace();
return useCallback(
((to: RR.To | number, options?: RR.NavigateOptions) =>
typeof to === "number"
? navigate(to)
: navigate(inWorkspace(path, to), options)) as RR.NavigateFunction,
[navigate, path],
);
}
|