import Link from 'next/link' import { abbrNum, numWithUnits } from '@/lib/format' import styles from './item.module.css' import React, { useEffect, useMemo, useState } from 'react' import { useQuery } from '@apollo/client' import MoreFooter from './more-footer' import { useData } from './use-data' import Info from './info' import { TerritoryInfo } from './territory-header' // all of this nonsense is to show the stat we are sorting by first const Revenue = ({ sub }) => (sub.optional.revenue !== null && {abbrNum(sub.optional.revenue)} revenue) const Stacked = ({ sub }) => (sub.optional.stacked !== null && {abbrNum(sub.optional.stacked)} stacked) const Spent = ({ sub }) => (sub.optional.spent !== null && {abbrNum(sub.optional.spent)} spent) const Posts = ({ sub }) => ( {numWithUnits(sub.nposts, { unitSingular: 'post', unitPlural: 'posts' })} ) const Comments = ({ sub }) => ( {numWithUnits(sub.ncomments, { unitSingular: 'comment', unitPlural: 'comments' })} ) const Separator = () => ( \ ) const STAT_POS = { stacked: 0, revenue: 1, spent: 2, posts: 3, comments: 4 } const STAT_COMPONENTS = [Stacked, Revenue, Spent, Posts, Comments] function separate (arr, separator) { return arr.flatMap((x, i) => i < arr.length - 1 ? [x, separator] : [x]) } export default function TerritoryList ({ ssrData, query, variables, destructureData, rank }) { const { data, fetchMore } = useQuery(query, { variables }) const dat = useData(data, ssrData) const [statComps, setStatComps] = useState(separate(STAT_COMPONENTS, Separator)) useEffect(() => { // shift the stat we are sorting by to the front const comps = [...STAT_COMPONENTS] setStatComps(separate([...comps.splice(STAT_POS[variables?.by || 0], 1), ...comps], Separator)) }, [variables?.by]) const { subs, cursor } = useMemo(() => { if (!dat) return {} if (destructureData) { return destructureData(dat) } else { return dat } }, [dat]) if (!dat) { return } return ( <>
{subs?.map((sub, i) => ( {rank ? (
{i + 1}
) :
}
{sub.name}
{statComps.map((Comp, i) => )}
))}
) } export function SubsSkeleton () { const subs = new Array(21).fill(null) return (
{subs.map((_, i) => (
))}
) }