From 0664205b0eba5ad88f4a88727b5d60485f41f3c3 Mon Sep 17 00:00:00 2001 From: samet Date: Tue, 5 Mar 2024 01:39:27 +0300 Subject: [PATCH] fetch readme files using ssr --- components/Readme.tsx | 63 +++++++++++++++++-------------------------- 1 file changed, 24 insertions(+), 39 deletions(-) diff --git a/components/Readme.tsx b/components/Readme.tsx index 5931099..a0198cf 100644 --- a/components/Readme.tsx +++ b/components/Readme.tsx @@ -1,5 +1,3 @@ -"use client"; -import { useEffect, useState } from "react"; import MarkdownPreview from "@uiw/react-markdown-preview"; import { Box, Button, Dialog, Text } from "@radix-ui/themes"; import { FaReadme } from "react-icons/fa6"; @@ -10,45 +8,32 @@ interface ReadmeProps { children: React.ReactNode; } -// The Readme component fetches and displays the content of a README.md file from a provided URL. -export default function Readme({ url, children }: ReadmeProps) { - // State to hold the content of the README file - const [content, setContent] = useState(null); - // State to track if there was an error fetching the README file - const [error, setError] = useState(false); - - useEffect(() => { - // Function to fetch the README content - const fetchReadme = async () => { - try { - const response = await fetch(url); - // Check if the response is not OK and throw an error - if (!response.ok) { - console.log(`Failed to fetch README.md: ${url}`); - } - const text = await response.text(); - // Check if the content is not the GitHub 404 message - text === "404: Not Found" ? setError(true) : setContent(text); - } catch (err) { - // Catch any network or other errors and set the error state - console.error(err); - setError(true); - } - }; - - // Call the fetch function - fetchReadme(); - }, [url]); - - // If there was an error, return null to render nothing - if (error) { - return null; +const getData = async (url: string) => { + try { + const response = await fetch(url); + // Check if the response is not OK and throw an error + if (!response.ok) { + console.log(`Failed to fetch README.md: ${url}`); + } + const text = await response.text(); + // Check if the content is not the GitHub 404 message + if (text === "404: Not Found") { + return null; + } + return text; + } catch (err) { + // Catch any network or other errors and set the error state + console.error(err); } +}; +// The Readme component fetches and displays the content of a README.md file from a provided URL. +export default async function Readme({ url, children }: ReadmeProps) { + const data = await getData(url); // If content is available, render the Dialog with the README content return ( <> - {!error && content && ( + {data && ( @@ -57,9 +42,9 @@ export default function Readme({ url, children }: ReadmeProps) { - + - + README.md