From 46e121a037790c279011ca59a42a33c14ba5c52c Mon Sep 17 00:00:00 2001 From: Artemy Date: Thu, 10 Aug 2023 13:06:52 +0300 Subject: [PATCH] feat: channels in channel --- package-lock.json | 8 +++---- package.json | 2 +- src/App.tsx | 8 ++++++- src/components/channel.tsx | 44 ++++++++++++++++++++++++++++++++++++++ src/components/utils.ts | 4 ++++ src/pages/channel.tsx | 27 ++++++++++++++--------- 6 files changed, 77 insertions(+), 16 deletions(-) create mode 100644 src/components/channel.tsx diff --git a/package-lock.json b/package-lock.json index d98d077..934b403 100644 --- a/package-lock.json +++ b/package-lock.json @@ -11,7 +11,7 @@ "@heroicons/react": "^2.0.18", "@nextui-org/react": "^2.0.7", "framer-motion": "^10.15.1", - "piped-api": "^1.2.1", + "piped-api": "^1.2.2", "react": "^18.2.0", "react-dom": "^18.2.0", "react-router-dom": "^6.14.2" @@ -4439,9 +4439,9 @@ } }, "node_modules/piped-api": { - "version": "1.2.1", - "resolved": "https://registry.npmjs.org/piped-api/-/piped-api-1.2.1.tgz", - "integrity": "sha512-HoJPQVEiDkwPFTwl29pg1zRcCm7GGZjW5pWt35A8uRRPb6475NgmZT0K+Z+GPtC4j78rbcGK2NEdkZceibW9gQ==", + "version": "1.2.2", + "resolved": "https://registry.npmjs.org/piped-api/-/piped-api-1.2.2.tgz", + "integrity": "sha512-eSzyanTMRGq3c9xCsSSlDXQ7jmd9auLvRIP5RNwv7yIkYBQjBxbQI5CR1vLcmKnD1tw4XdBF15aLIh0Jru3iIw==", "dependencies": { "axios": "^1.4.0" } diff --git a/package.json b/package.json index 601f00b..dabddf4 100644 --- a/package.json +++ b/package.json @@ -13,7 +13,7 @@ "@heroicons/react": "^2.0.18", "@nextui-org/react": "^2.0.7", "framer-motion": "^10.15.1", - "piped-api": "^1.2.1", + "piped-api": "^1.2.2", "react": "^18.2.0", "react-dom": "^18.2.0", "react-router-dom": "^6.14.2" diff --git a/src/App.tsx b/src/App.tsx index 74f0cc1..b24318a 100644 --- a/src/App.tsx +++ b/src/App.tsx @@ -4,6 +4,7 @@ import { HashRouter, Navigate, Route, Routes } from "react-router-dom"; import TrendingPage from "./pages/trending"; import { NavbarComponent } from "./components/navbar"; import ChannelPage from "./pages/channel"; +import { useState } from "react"; declare global { interface Window { @@ -12,9 +13,14 @@ declare global { } function App() { window.piped_api = new PipedAPI(); + const [key, setKey] = useState(Math.random()); + + window.addEventListener("reRenderPage", () => { + setKey(Math.random()); + }); return ( - + } /> diff --git a/src/components/channel.tsx b/src/components/channel.tsx new file mode 100644 index 0000000..52ebd28 --- /dev/null +++ b/src/components/channel.tsx @@ -0,0 +1,44 @@ +import { Avatar, Card, CardBody, CardHeader, Link } from "@nextui-org/react"; +import { Channel } from "piped-api/dist/types"; +import { useNavigate } from "react-router-dom"; +import { CheckCircleIcon } from "@heroicons/react/24/solid"; +import { shortenNumber } from "./utils"; +export function ChannelComponent({ + channel, + uploaderAvatar, +}: ChannelComponentProps) { + const navigate = useNavigate(); + + return ( + { + navigate(channel.url || ""); + }} + shadow="none" + > + + + +
+

{channel.name}

+ {channel.verified && } +
+ +
+ +

+ {`${shortenNumber(channel.videos || -1)} videos`} +

+
+
+ ); +} + +type ChannelComponentProps = { + channel: Channel; + uploaderAvatar?: string; +}; diff --git a/src/components/utils.ts b/src/components/utils.ts index cc0ac73..b640b56 100644 --- a/src/components/utils.ts +++ b/src/components/utils.ts @@ -20,3 +20,7 @@ export function getTheme() { return "light"; } } + +export function reRenderPage() { + window.dispatchEvent(new Event("reRenderPage")); +} diff --git a/src/pages/channel.tsx b/src/pages/channel.tsx index 9c1be1e..4748c34 100644 --- a/src/pages/channel.tsx +++ b/src/pages/channel.tsx @@ -10,6 +10,7 @@ import { capitalize } from "../components/utils"; import React from "react"; import { PlaylistComponent } from "../components/playlist"; import { CheckCircleIcon } from "@heroicons/react/24/solid"; +import { ChannelComponent } from "../components/channel"; export default function ChannelPage() { const { id } = useParams(); @@ -58,11 +59,20 @@ class ChannelPageŠ”omponent extends React.Component< this.setState({ channel, tab }); } - componentDidUpdate() { - if (!this.state.tab) { - this.updateTab(); + async updateChannel() { + const channel = (await window.piped_api.channel( + this.props.id || "" + )) as Channel; + + this.setState({ ...this.state, channel }); + } + + async componentDidUpdate() { + if (this.props.id !== this.state?.channel?.id) { + await this.updateChannel(); } } + render() { if (!this.state) { return null; @@ -111,13 +121,10 @@ class ChannelPageŠ”omponent extends React.Component< {channel.tabs.map((tab, index) => { return (