Open History to see the last commit
1import { Link } from "@tanstack/react-router";
2import { GithubIcon, HighlighterIcon, XCircleIcon } from "lucide-react";
3import { type ReactNode, useMemo } from "react";
4import z from "zod";
5
6/**
7 Naming convention:
8 - `project` highlights any ids starting with `project` (e.g., `project`, `project_sidebar`)
9*/
10export const highlightParamSchema = z.object({
11 highlight: z
12 .enum([
13 "project",
14 "project_sidebar",
15 "project_projectPage",
16 "board",
17 "editProject",
18 "networkLatencyCofigurator",
19 ])
20 .optional(),
21});
22
23export type HighlightParam = z.infer<typeof highlightParamSchema>["highlight"];
24
25function getFilePathFromGithubFileUrl(href: string) {
26 return href.split("src/")[1].replace(/#.*/, "");
27}
28
29/**
30 A "Source GitHub" link component that extracts
31 and displays the file path from a GitHub URL.
32*/
33export function GHLink({ href }: { href: string }) {
34 const label = useMemo(() => {
35 try {
36 const filePath = getFilePathFromGithubFileUrl(href);
37 return filePath;
38 } catch (e) {
39 return "View source";
40 }
41 }, [href]);
42
43 return (
44 <a
45 href={href}
46 target="_blank"
47 rel="noreferrer"
48 className="text-xs text-neutral-500 transition-colors underline hover:text-orange-500 mb-4 block w-fit"
49 >
50 <div className="flex items-center gap-1">
51 <GithubIcon className="h-4 w-4" /> {label}
52 </div>
53 </a>
54 );
55}
56
57export function HighLightComponent({
58 h_id: newHighLightGroupId,
59 children,
60}: {
61 h_id: string;
62 children?: ReactNode;
63}) {
64 const highlight = useMemo(() => {
65 try {
66 return highlightParamSchema.parse({
67 highlight: newHighLightGroupId,
68 }).highlight;
69 } catch (e) {
70 return undefined;
71 }
72 }, [newHighLightGroupId]);
73
74 return (
75 <div className="flex items-center gap-2">
76 <div className="px-2 py-1 bg-black w-fit rounded-md cursor-pointer hover:bg-neutral-800 transition-colors">
77 <Link
78 to="."
79 search={(s) => ({
80 ...s,
81 highlight,
82 })}
83 className="flex items-center no-underline text-white"
84 >
85 <HighlighterIcon className="h-4 2-4" />
86 {children}
87 </Link>
88 </div>
89 <ClearHighlightsButton />
90 </div>
91 );
92}
93
94export function ClearHighlightsButton() {
95 return (
96 <div className="py-1 w-fit rounded-md cursor-pointer ">
97 <Link
98 to="."
99 search={(s) => ({
100 ...s,
101 highlight: undefined,
102 })}
103 className="flex items-center no-underline text-neutral-700 hover:text-black transition-colors"
104 >
105 <XCircleIcon className="h-4 2-4" />
106 Clear Highlights
107 </Link>
108 </div>
109 );
110}
111
112export function LinkToArticle({
113 children,
114 articleTitle,
115}: {
116 children: ReactNode;
117 articleTitle: string;
118}) {
119 const encodedTitle = useMemo(
120 () => encodeURIComponent(articleTitle),
121 [articleTitle],
122 );
123
124 return (
125 <Link
126 to="."
127 search={{
128 step: encodedTitle,
129 }}
130 className="text-orange-500 underline hover:text-orange-600 transition-colors cursor-pointer"
131 >
132 {children}
133 </Link>
134 );
135}