refactor: class components

This commit is contained in:
Artemy 2023-08-10 11:50:17 +03:00
parent b7a10179ad
commit bafd943814
3 changed files with 161 additions and 128 deletions

View file

@ -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;
}
}

View file

@ -1,5 +1,4 @@
import { Channel, Tab, Video } from "piped-api/dist/types";
import { useEffect, useState } from "react";
import {
SkeletonVideoComponent,
VideoComponent,
@ -8,34 +7,67 @@ 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<Channel>();
const [tab, setTab] = useState<Tab>();
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 (
<ChannelPageСomponent
id={id}
setSearchParams={setSearchParams}
tabId={tabId}
/>
);
}
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 });
}
}
if (!channel) {
getChannel();
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 });
}
if (!tab && tabId !== "videos") {
getTab(tabId);
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) {
return (
@ -76,7 +108,11 @@ export default function ChannelPage() {
<Button
onClick={() => {
setSearchParams({ tabId: String(index) });
setTab(undefined);
this.setState({
...this.state,
tab: undefined,
});
}}
key={tab.name}
className="text-xl font-bold mt-4"
@ -122,4 +158,5 @@ export default function ChannelPage() {
</div>
);
}
}
}

View file

@ -1,20 +1,22 @@
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 });
}
if (trending.length === 0) {
getTrending();
}
});
render() {
const { trending } = this.state;
if (trending.length === 0) {
return (
@ -33,4 +35,5 @@ export default function TrendingPage() {
))}
</div>
);
}
}