-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathindex.tsx
More file actions
117 lines (108 loc) · 2.5 KB
/
index.tsx
File metadata and controls
117 lines (108 loc) · 2.5 KB
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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
import { Link } from "@tanstack/react-router";
import { GithubIcon, SearchIcon } from "lucide-react";
import { type ReactNode, useMemo } from "react";
import z from "zod";
import { Button } from "../ui/button";
/**
Naming convention:
- `project` highlights any ids starting with `project` (e.g., `project`, `project_sidebar`)
*/
export const highlightParamSchema = z.object({
highlight: z
.enum([
"project",
"project_sidebar",
"project_projectPage",
"board",
"editProject",
"apiLatencyConfigurator",
])
.optional(),
});
export type HighlightParam = z.infer<typeof highlightParamSchema>["highlight"];
function getFilePathFromGithubFileUrl(href: string) {
return href.split("src/")[1].replace(/#.*/, "");
}
/**
A "Source GitHub" link component that extracts
and displays the file path from a GitHub URL.
*/
export function GHLink({ href }: { href: string }) {
const label = useMemo(() => {
try {
const filePath = getFilePathFromGithubFileUrl(href);
return filePath;
} catch (e) {
return "View source";
}
}, [href]);
return (
<a
href={href}
target="_blank"
rel="noreferrer"
className="text-xs text-neutral-500 transition-colors underline hover:text-orange-500 mb-4 block w-fit"
>
<div className="flex items-center gap-1">
<GithubIcon className="h-4 w-4" /> {label}
</div>
</a>
);
}
export function HighLightComponent({
h_id: newHighLightGroupId,
children,
}: {
h_id: string;
children?: ReactNode;
}) {
const highlight = useMemo(() => {
try {
return highlightParamSchema.parse({
highlight: newHighLightGroupId,
}).highlight;
} catch (e) {
return undefined;
}
}, [newHighLightGroupId]);
return (
<div className="flex items-center gap-2">
<Link
to="."
replace={true}
search={(s) => ({
...s,
highlight,
})}
>
<Button>
<SearchIcon />
{children}
</Button>
</Link>
</div>
);
}
export function LinkToArticle({
children,
articleTitle,
}: {
children: ReactNode;
articleTitle: string;
}) {
const encodedTitle = useMemo(
() => encodeURIComponent(articleTitle.toLowerCase()),
[articleTitle],
);
return (
<Link
to="."
search={{
article: encodedTitle,
}}
className="text-orange-500 underline hover:text-orange-600 transition-colors cursor-pointer"
>
{children}
</Link>
);
}