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

index.tsx

180 lines3.8 KB

1import { Link } from "@tanstack/react-router";
2import { GithubIcon, SearchIcon } from "lucide-react";
3import { type ReactNode, useCallback, useMemo } from "react";
4import z from "zod";
5import { userPreferencesCollection } from "@/collections/UserPreferences";
6import { USER_PLACEHOLDER } from "@/utils/USER_PLACEHOLDER_CONSTANT";
7import { Button } from "../ui/button";
8​
9/**
10 Naming convention:
11 - `project` highlights any ids starting with `project` (e.g., `project`, `project_sidebar`)
12*/
13export const highlightParamSchema = z.object({
14 highlight: z
15 .enum([
16 "project",
17 "project_sidebar",
18 "project_projectPage",
19 "board",
20 "editProject",
21 "apiLatencyConfigurator",
22 "networkPanel_toggle",
23 "networkPanel_panel",
24 ])
25 .optional(),
26});
27​
28export type HighlightParam = z.infer<typeof highlightParamSchema>["highlight"];
29​
30function getFilePathFromGithubFileUrl(href: string) {
31 return href.split("src/")[1].replace(/#.*/, "");
33​
34/**
35 A "Source GitHub" link component that extracts
36 and displays the file path from a GitHub URL.
37*/
38export function GHLink({ href }: { href: string }) {
39 const label = useMemo(() => {
40 try {
41 const filePath = getFilePathFromGithubFileUrl(href);
42 return filePath;
43 } catch (e) {
44 return "View source";
45 }
46 }, [href]);
47​
48 return (
49 <a
50 href={href}
51 target="_blank"
52 rel="noreferrer"
53 className="text-xs text-neutral-500 transition-colors underline hover:text-orange-500 mb-4 block w-fit"
54 >
55 <div className="flex items-center gap-1">
56 <GithubIcon className="h-4 w-4" /> {label}
57 </div>
58 </a>
59 );
61​
62export function HighLightComponent({
63 h_id: newHighLightGroupId,
64 children,
65}: {
66 h_id: string;
67 children?: ReactNode;
68}) {
69 const highlight = useMemo(() => {
70 try {
71 return highlightParamSchema.parse({
72 highlight: newHighLightGroupId,
73 }).highlight;
74 } catch (e) {
75 return undefined;
76 }
77 }, [newHighLightGroupId]);
78​
79 return (
80 <div className="flex items-center gap-2">
81 <Link
82 to="."
83 replace={true}
84 search={(s) => ({
85 ...s,
86 highlight,
87 })}
88 >
89 <Button>
90 <SearchIcon />
91 {children}
92 </Button>
93 </Link>
94 </div>
95 );
97​
98export function HighlightLink({
99 h_id: newHighLightGroupId,
100 children,
101}: {
102 h_id: string;
103 children: ReactNode;
104}) {
105 const highlight = useMemo(() => {
106 try {
107 return highlightParamSchema.parse({
108 highlight: newHighLightGroupId,
109 }).highlight;
110 } catch (e) {
111 return undefined;
112 }
113 }, [newHighLightGroupId]);
114​
115 return (
116 <Link
117 className="inline"
118 to="."
119 replace={true}
120 search={(s) => ({
121 ...s,
122 highlight,
123 })}
124 >
125 {children}
126 </Link>
127 );
129​
130export function LinkToArticle({
131 children,
132 articleTitle,
133}: {
134 children: ReactNode;
135 articleTitle: string;
136}) {
137 const encodedTitle = useMemo(
138 () => encodeURIComponent(articleTitle.toLowerCase()),
139 [articleTitle],
140 );
141​
142 return (
143 <Link
144 to="."
145 search={{
146 article: encodedTitle,
147 }}
148 className="text-orange-500 underline hover:text-orange-600 transition-colors cursor-pointer"
149 >
150 {children}
151 </Link>
152 );
154​
155export function OpenAPIRequestsPanelLink({
156 children,
157}: {
158 children: ReactNode;
159}) {
160 const openThePanel = useCallback(() => {
161 if (typeof window !== "undefined") {
162 userPreferencesCollection.update(USER_PLACEHOLDER.id, (draft) => {
163 draft.networkPanel = "open";
164 });
165 }
166 }, []);
167​
168 return (
169 <Link
170 to="."
171 onClick={openThePanel}
172 search={{
173 // highlight the toggle button of the network panel
174 highlight: "networkPanel_panel",
175 }}
176 >
177 {children}
178 </Link>
179 );