Skip to content
Source
Sign inSign up
fuko/trytanstackdb.com
Open History to see the last commit

index.tsx

135 lines2.9 KB

1import { Link } from "@tanstack/react-router";
2import { GithubIcon, SearchIcon, XCircleIcon } from "lucide-react";
3import { type ReactNode, useMemo } from "react";
4import z from "zod";
5import { Button } from "../ui/button";
6​
7/**
8 Naming convention:
9 - `project` highlights any ids starting with `project` (e.g., `project`, `project_sidebar`)
10*/
11export const highlightParamSchema = z.object({
12 highlight: z
13 .enum([
14 "project",
15 "project_sidebar",
16 "project_projectPage",
17 "board",
18 "editProject",
19 "apiLatencyConfigurator",
20 ])
21 .optional(),
22});
23​
24export type HighlightParam = z.infer<typeof highlightParamSchema>["highlight"];
25​
26function getFilePathFromGithubFileUrl(href: string) {
27 return href.split("src/")[1].replace(/#.*/, "");
29​
30/**
31 A "Source GitHub" link component that extracts
32 and displays the file path from a GitHub URL.
33*/
34export function GHLink({ href }: { href: string }) {
35 const label = useMemo(() => {
36 try {
37 const filePath = getFilePathFromGithubFileUrl(href);
38 return filePath;
39 } catch (e) {
40 return "View source";
41 }
42 }, [href]);
43​
44 return (
45 <a
46 href={href}
47 target="_blank"
48 rel="noreferrer"
49 className="text-xs text-neutral-500 transition-colors underline hover:text-orange-500 mb-4 block w-fit"
50 >
51 <div className="flex items-center gap-1">
52 <GithubIcon className="h-4 w-4" /> {label}
53 </div>
54 </a>
55 );
57​
58export function HighLightComponent({
59 h_id: newHighLightGroupId,
60 children,
61}: {
62 h_id: string;
63 children?: ReactNode;
64}) {
65 const highlight = useMemo(() => {
66 try {
67 return highlightParamSchema.parse({
68 highlight: newHighLightGroupId,
69 }).highlight;
70 } catch (e) {
71 return undefined;
72 }
73 }, [newHighLightGroupId]);
74​
75 return (
76 <div className="flex items-center gap-2">
77 <Link
78 to="."
79 replace={true}
80 search={(s) => ({
81 ...s,
82 highlight,
83 })}
84 >
85 <Button className="">
86 <SearchIcon />
87 {children}
88 </Button>
89 </Link>
90 </div>
91 );
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 );
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 );