import { useRouter } from 'next/router' import { Select } from './form' import Info from './info' import { SSR } from '../lib/constants' import { SUBS } from '../fragments/subs' import { useQuery } from '@apollo/client' import { useEffect, useState } from 'react' import styles from './sub-select.module.css' export function SubSelectInitial ({ sub }) { const router = useRouter() sub = sub || router.query.sub || 'pick territory' return { sub } } export function useSubs ({ prependSubs = [], sub, filterSubs = () => true, appendSubs = [] }) { const { data } = useQuery(SUBS, SSR ? {} : { pollInterval: 300000, nextFetchPolicy: 'cache-and-network' }) const [subs, setSubs] = useState([ ...prependSubs.filter(s => s !== sub), sub, ...appendSubs.filter(s => s !== sub)]) useEffect(() => { if (!data) return const joined = data.subs.filter(filterSubs).filter(s => !s.meMuteSub).map(s => s.name) const muted = data.subs.filter(filterSubs).filter(s => s.meMuteSub).map(s => s.name) const mutedSection = muted.length ? [{ label: 'muted', items: muted }] : [] setSubs([ ...prependSubs, ...joined, ...mutedSection, ...appendSubs]) }, [data]) return subs } export const SubInfo = () => (
The territory your post will go in ...
) export default function SubSelect ({ prependSubs, sub, onChange, appendSubs, filterSubs, className, ...props }) { const router = useRouter() const subs = useSubs({ prependSubs, sub, filterSubs, appendSubs }) const valueProps = props.noForm ? { value: sub.toLowerCase() } : { overrideValue: sub.toLowerCase() } return (