From 47c84bb74d44d8285c68fa1491d01ca3b08f827f Mon Sep 17 00:00:00 2001 From: Pinapelz Date: Fri, 9 May 2025 23:14:52 -0700 Subject: migrate to static API --- src/pages/twitch/index.tsx | 88 ++++++++++++++++++++-------------------------- 1 file changed, 38 insertions(+), 50 deletions(-) (limited to 'src/pages/twitch') diff --git a/src/pages/twitch/index.tsx b/src/pages/twitch/index.tsx index 43d88ed..fc389c2 100644 --- a/src/pages/twitch/index.tsx +++ b/src/pages/twitch/index.tsx @@ -1,4 +1,3 @@ -import { useEffect, useState } from "react"; import TwitchDataTable, { type TwitchDataTableProp, } from "../../components/SubscriberTable/TwitchDataTable"; @@ -6,60 +5,49 @@ import TitleBar from "../../components/TitleBar/TitleBar"; import Announcement from "../../components/Announcement"; import "../../app/globals.css"; -function TwitchPage() { - const [twitchData, setTwitchData] = useState(null); - const [error, setError] = useState(null); - - const announcementText = process.env.NEXT_PUBLIC_ANNOUNCEMENT; - - useEffect(() => { - async function fetchTwitchData() { - try { - const apiUrl = process.env.NEXT_PUBLIC_API_URL_TESTING; - const endpoint = "/api/twitch"; - const headers = { - "Cache-Control": "no-cache", - }; - const cacheOption = "no-cache"; - - const response = await fetch(`${apiUrl}${endpoint}`, { - headers: headers, - cache: cacheOption, - }); - - if (!response.ok) { - throw new Error(response.statusText); - } - - const data = await response.json(); - setTwitchData(data); - } catch (err) { - setError(err instanceof Error ? err.message : "An error occurred"); - } - } - - fetchTwitchData(); - }, []); +type Props = { + data: TwitchDataTableProp; + graphURL: string | undefined; + announcementText: string | undefined; +}; +function TwitchPage({ data, graphURL, announcementText }: Props) { return ( <> - {announcementText && ( - - )} - {error ? ( -
Error: {error}
- ) : twitchData ? ( - - ) : ( -
Loading...
- )} + ); } -export default TwitchPage; +export async function getServerSideProps() { + const graphURL = process.env.NEXT_PUBLIC_TWITCH_GRAPH_URL; + const announcementText = process.env.NEXT_PUBLIC_ANNOUNCEMENT; + const apiUrl = process.env.NEXT_PUBLIC_API_URL_TESTING; + const endpoint = "/twitch.json"; + const headers = { + "Cache-Control": "no-cache", + }; + const cacheOption = "no-cache"; + + const response = await fetch(`${apiUrl}${endpoint}`, { + headers: headers, + cache: cacheOption, + }); + let data = {}; + if (response.ok) { + data = await response.json(); + } else { + console.log(response.statusText); + } + + return { + props: { + data, + graphURL: graphURL ?? null, + announcementText: announcementText ?? null, + }, + }; +} + +export default TwitchPage; \ No newline at end of file -- cgit v1.2.3