link.tsx
This file
Path
apps/web/src/features/shared/link.tsx
68 lines · 1.9 KiB · TypeScript
Last changed by
Merge build/comments-web-features into mainoscabrielbefore adoption74bfc72
History of this fileTake it
Put every page in the Control room shell: rail, bars, workspace, router links, keys, head, error and pending statesoscabrielbefore adoptionddbd51e
line 1why, one step back1/**Merge build/comments-web-features into mainoscabrielbefore adoption74bfc72
lines 2–3why, one step back2 * A server route (sign-in, a raw file, a `.md` twin, `llms.txt`) is a plain3 * `<a>`, or the router would look for a page that is not there.Put every page in the Control room shell: rail, bars, workspace, router links, keys, head, error and pending statesoscabrielbefore adoptionddbd51e
lines 4–19why, one step back4 */5import {6 defaultStringifySearch,7 Link,8 useRouter,9} from "@tanstack/react-router";10import type { ActiveOptions, AnyRouter } from "@tanstack/react-router";11import { Predicate } from "effect";12import { createElement } from "react";13import type { AnchorHTMLAttributes, ReactNode } from "react";1415export interface AppLinkProps extends Omit<16 AnchorHTMLAttributes<HTMLAnchorElement>,17 "href" | "target"18> {19 readonly to: string;Draw every list and subject from one row, one status line, one family fan and one set of fact blocksoscabrielbefore adoption886b726
line 20why, one step back20 readonly search?: Readonly<Record<string, string>> | undefined;Put every page in the Control room shell: rail, bars, workspace, router links, keys, head, error and pending statesoscabrielbefore adoptionddbd51e
lines 21–27why, one step back21 readonly hash?: string;22 readonly activeOptions?: ActiveOptions;23 readonly preload?: false;24 readonly replace?: boolean;25 readonly resetScroll?: boolean;26}27Review fixes for 1203oscabrielbefore adoption950d5e0
line 28why, one step back28/**Merge build/comments-web-features into mainoscabrielbefore adoption74bfc72
lines 29–31why, one step back29 * `$` and the braces of an optional segment are, in a path built whole, part of30 * a name (`routes/$project/$.tsx`), never a param for the router to fill.31 * Escaped, they reach the address as written.Review fixes for 1203oscabrielbefore adoption950d5e0
lines 32–35why, one step back32 */33export const literalPath = (path: string): string =>34 path.replaceAll("$", "%24").replaceAll("{", "%7B").replaceAll("}", "%7D");35Put every page in the Control room shell: rail, bars, workspace, router links, keys, head, error and pending statesoscabrielbefore adoptionddbd51e
lines 36–40why, one step back36export const hrefOf = ({37 hash,38 search,39 to,40}: Pick<AppLinkProps, "hash" | "search" | "to">): string =>Review fixes for 1203oscabrielbefore adoption950d5e0
line 41why, one step back41 `${literalPath(to)}${Put every page in the Control room shell: rail, bars, workspace, router links, keys, head, error and pending statesoscabrielbefore adoptionddbd51e
lines 42–52why, one step back42 search === undefined || Object.keys(search).length === 043 ? ""44 : defaultStringifySearch(search)45 }${hash === undefined ? "" : `#${hash}`}`;4647const PathLink = Link<AnyRouter, string, string>;4849export const AppLink = (props: AppLinkProps): ReactNode => {50 const router = useRouter({ warn: false });5152 if (Predicate.isNotNullish(router)) {Review fixes for 1203oscabrielbefore adoption950d5e0
line 53why, one step back53 return <PathLink {...props} to={literalPath(props.to)} />;Put every page in the Control room shell: rail, bars, workspace, router links, keys, head, error and pending statesoscabrielbefore adoptionddbd51e
lines 54–68why, one step back54 }5556 const {57 activeOptions: _active,58 hash: _hash,59 preload: _preload,60 replace: _replace,61 resetScroll: _reset,62 search: _search,63 to: _to,64 ...anchor65 } = props;6667 return createElement("a", { ...anchor, href: hrefOf(props) });68};