import type { GetServerSideProps, NextPage } from "next"; import { getSession, useSession } from "next-auth/react"; import { useState } from "react"; import { dehydrate, QueryClient, useQuery } from "react-query"; import { CustomHead } from "../components/CustomHead"; import { Layout } from "../components/Layout"; import { getTAUrl } from "../lib/constants"; import { getChannels } from "../lib/getChannels"; import { formatNumbers } from "../lib/utils"; const TA_BASE_URL = getTAUrl(); type ViewStyle = "grid" | "list"; export const getServerSideProps: GetServerSideProps = async (context) => { const queryClient = new QueryClient(); const session = await getSession(context); if (!session) { return { redirect: { destination: "/auth/login", permanent: false, }, }; } await queryClient.prefetchQuery(["channels", session.ta_token.token], () => getChannels(session.ta_token.token) ); return { props: { dehydratedState: dehydrate(queryClient), session, }, }; }; const Channel: NextPage = () => { const { data: session } = useSession(); const { data: channels, error, isLoading, } = useQuery( ["channels", session.ta_token.token], () => getChannels(session.ta_token.token), { enabled: !!session?.ta_token?.token, } ); const [viewStyle, setViewStyle] = useState("grid"); const handleSetViewstyle = (selectedViewStyle: ViewStyle) => { setViewStyle(selectedViewStyle); }; if (isLoading) { return (

Loading...

); } if (error) { return (

Error

{JSON.stringify(error, null, 2)}
); } return ( <>

Channels

console.log("showForm()")} src="/img/icon-add.svg" alt="add-icon" title="Subscribe to Channels" />
{/* {% csrf_token %} {{ subscribe_form }} */}
Show subscribed only:
console.log("toggleCheckbox(this)")} type="checkbox" /> {/* {% if not show_subed_only %} */} {/* {% else %} */} {/* {% endif %} */}
handleSetViewstyle("grid")} alt="grid view" /> handleSetViewstyle("list")} alt="list view" />

Total matching channels: {channels?.data?.length}

{!channels.data ? (

No channels found...

) : ( channels?.data?.map((channel) => { return (

{" "} {channel?.channel_name}{" "}

{/* {% if channel.source.channel_subs >= 1000000 %} */}

Subscribers: {formatNumbers(channel?.channel_subs)}{" "}

{/* {% else %} */}

Last refreshed: {channel?.channel_last_refresh}{" "}

{/* {% if channel.source.channel_subscribed %} */} {/* {% else %} */} {/* */} {/* {% endif %} */}
); }) )} {/* {% endfor %} {% else %} */} {/*

No channels found...

*/} {/* {% endif %} */}
); }; export default Channel;