Skip to content

Commit 19971f4

Browse files
committed
Build generic github markdown component
1 parent 4c59862 commit 19971f4

File tree

1 file changed

+37
-0
lines changed

1 file changed

+37
-0
lines changed
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
import { AppLoading } from "./AppLoading";
2+
import { Markdown } from "@/lib/markdown";
3+
import { useQuery } from "@tanstack/react-query";
4+
5+
async function fetchMarkdown(url: string): Promise<string> {
6+
const res = await fetch(url);
7+
8+
if (!res.ok) {
9+
throw new Error("Failed to fetch markdown from GitHub.");
10+
}
11+
12+
return res.text();
13+
}
14+
15+
type AppGitHubGenericMarkdownProps = {
16+
url: string;
17+
};
18+
19+
export function AppGitHubGenericMarkdown({
20+
url,
21+
}: AppGitHubGenericMarkdownProps) {
22+
const { data, isLoading, error } = useQuery({
23+
queryKey: ["markdown-text"],
24+
queryFn: () => fetchMarkdown(url),
25+
enabled: !!url,
26+
});
27+
28+
if (isLoading) return <AppLoading />;
29+
if (error) return <div>Failed to fetch markdown from GitHub.</div>;
30+
if (!data) return <div>Failed to fetch markdown from GitHub.</div>;
31+
32+
return (
33+
<div>
34+
<Markdown content={data} />
35+
</div>
36+
);
37+
}

0 commit comments

Comments
 (0)