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

index.tsx

117 lines2.5 KB

1import { Link } from "@tanstack/react-router";
2import { GithubIcon, SearchIcon } 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>
86 <SearchIcon />
87 {children}
88 </Button>
89 </Link>
90 </div>
91 );
93​
94export function LinkToArticle({
95 children,
96 articleTitle,
97}: {
98 children: ReactNode;
99 articleTitle: string;
100}) {
101 const encodedTitle = useMemo(
102 () => encodeURIComponent(articleTitle),
103 [articleTitle],
104 );
105​
106 return (
107 <Link
108 to="."
109 search={{
110 step: encodedTitle,
111 }}
112 className="text-orange-500 underline hover:text-orange-600 transition-colors cursor-pointer"
113 >
114 {children}
115 </Link>
116 );