From bafd943814bf0f63dd07bb81138877397c078f86 Mon Sep 17 00:00:00 2001 From: Artemy Date: Thu, 10 Aug 2023 11:50:17 +0300 Subject: [PATCH] refactor: class components --- src/index.css | 7 -- src/pages/channel.tsx | 237 ++++++++++++++++++++++++----------------- src/pages/trending.tsx | 45 ++++---- 3 files changed, 161 insertions(+), 128 deletions(-) diff --git a/src/index.css b/src/index.css index 27ed007..b5c61c9 100644 --- a/src/index.css +++ b/src/index.css @@ -1,10 +1,3 @@ @tailwind base; @tailwind components; @tailwind utilities; - -@layer base { - body { - background-image: url("data:image/svg+xml,%3csvg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 32 32' width='32' height='32' fill='none' stroke='%23f1f5f9'%3e%3cpath d='M0 .5H31.5V32'/%3e%3c/svg%3e"); - @apply bg-gray-200; - } -} diff --git a/src/pages/channel.tsx b/src/pages/channel.tsx index 25d419a..34151c1 100644 --- a/src/pages/channel.tsx +++ b/src/pages/channel.tsx @@ -1,5 +1,4 @@ import { Channel, Tab, Video } from "piped-api/dist/types"; -import { useEffect, useState } from "react"; import { SkeletonVideoComponent, VideoComponent, @@ -8,118 +7,156 @@ import { import { useParams, useSearchParams } from "react-router-dom"; import { Button, Card, CardFooter } from "@nextui-org/react"; import { capitalize } from "../components/utils"; +import React from "react"; export default function ChannelPage() { - const [channel, setChannel] = useState(); - const [tab, setTab] = useState(); - const { id } = useParams(); const [searchParams, setSearchParams] = useSearchParams(); const tabId = searchParams.get("tabId") || "videos"; - useEffect(() => { - async function getChannel() { - const channel = (await window.piped_api.channel(id || "")) as Channel; - setChannel(channel); + return ( + + ); +} + +class ChannelPageŠ”omponent extends React.Component< + { id; setSearchParams; tabId }, + { channel: Channel; tab: Tab | undefined } +> { + async componentDidMount() { + const channel = (await window.piped_api.channel( + this.props.id || "" + )) as Channel; + + if (this.props.tabId === "videos") { + return this.setState({ channel }); } - async function getTab(name: string) { - const tab = await window.piped_api.channelTabs(channel?.tabs[name].data); - setTab(tab); + if (!this.state?.tab) { + const tab = await window.piped_api.channelTabs( + channel.tabs[this.props.tabId].data + ); + + this.setState({ channel, tab }); } + } + + async updateTab() { + if (this.props.tabId === "videos") { + return; + } + const { channel } = this.state; + const tab = await window.piped_api.channelTabs( + channel.tabs[this.props.tabId].data + ); + this.setState({ channel, tab }); + } + + componentDidUpdate() { + if (!this.state.tab) { + this.updateTab(); + } + } + render() { + if (!this.state) { + return null; + } + + const { channel, tab } = this.state; + const { setSearchParams, tabId } = this.props; if (!channel) { - getChannel(); - } - - if (!tab && tabId !== "videos") { - getTab(tabId); - } - }); - - if (!channel) { - return ( -
- {[...Array(20).keys()].map((num) => ( - - ))} -
- ); - } else { - return ( -
- -
- -
- -
-
- {channel.name} -
-
-
-
- -
- - {channel.tabs.map((tab, index) => { - return ( - - ); - })} + return ( +
+ {[...Array(20).keys()].map((num) => ( + + ))}
+ ); + } else { + return ( +
+ +
+ +
+ +
+
+ {channel.name} +
+
+
+
- {tabId !== "videos" ? ( - - {tab?.content.map((video) => { - if (video.type === "stream") { - video = video as Video; - return ( - - ); - } else { - return ( -
- Soon... -
- ); - } +
+ + {channel.tabs.map((tab, index) => { + return ( + + ); })} - - ) : ( - - {channel.relatedStreams.map((video) => ( - - ))} - - )} -
- ); +
+ + {tabId !== "videos" ? ( + + {tab?.content.map((video) => { + if (video.type === "stream") { + video = video as Video; + return ( + + ); + } else { + return ( +
+ Soon... +
+ ); + } + })} +
+ ) : ( + + {channel.relatedStreams.map((video) => ( + + ))} + + )} +
+ ); + } } } diff --git a/src/pages/trending.tsx b/src/pages/trending.tsx index c1aba1b..be8ac8d 100644 --- a/src/pages/trending.tsx +++ b/src/pages/trending.tsx @@ -1,36 +1,39 @@ import { Video } from "piped-api/dist/types"; -import { useEffect, useState } from "react"; import { SkeletonVideoComponent, VideoComponent } from "../components/video"; +import React from "react"; -export default function TrendingPage() { - const [trending, setTrending] = useState([] as Video[]); +export default class TrendingPage extends React.Component { + state: { trending: Video[] } = { + trending: [], + }; - useEffect(() => { - async function getTrending() { - const trending = await window.piped_api.trending("US"); - setTrending(trending); - } + async componentDidMount() { + console.log("trending"); + const trending = await window.piped_api.trending( + localStorage.getItem("region") || "US" + ); + this.setState({ trending }); + } + + render() { + const { trending } = this.state; if (trending.length === 0) { - getTrending(); + return ( +
+ {[...Array(20).keys()].map((num) => ( + + ))} +
+ ); } - }); - if (trending.length === 0) { return (
- {[...Array(20).keys()].map((num) => ( - + {trending.map((video) => ( + ))}
); } - - return ( -
- {trending.map((video) => ( - - ))} -
- ); }