2023-11-21 23:32:22 +00:00
|
|
|
import { useRouter } from 'next/router'
|
|
|
|
import { Select } from './form'
|
2024-03-20 00:37:31 +00:00
|
|
|
import { SSR } from '@/lib/constants'
|
|
|
|
import { SUBS } from '@/fragments/subs'
|
2023-11-21 23:32:22 +00:00
|
|
|
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),
|
2024-03-26 17:02:38 +00:00
|
|
|
...(sub ? [sub] : []),
|
2023-11-21 23:32:22 +00:00
|
|
|
...appendSubs.filter(s => s !== sub)])
|
|
|
|
useEffect(() => {
|
|
|
|
if (!data) return
|
2023-12-31 00:19:42 +00:00
|
|
|
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])
|
2023-11-21 23:32:22 +00:00
|
|
|
}, [data])
|
|
|
|
|
|
|
|
return subs
|
|
|
|
}
|
|
|
|
|
2024-02-23 15:32:20 +00:00
|
|
|
export default function SubSelect ({ prependSubs, sub, onChange, size, appendSubs, filterSubs, className, ...props }) {
|
2023-11-21 23:32:22 +00:00
|
|
|
const router = useRouter()
|
|
|
|
const subs = useSubs({ prependSubs, sub, filterSubs, appendSubs })
|
|
|
|
const valueProps = props.noForm
|
|
|
|
? {
|
2024-01-04 02:39:26 +00:00
|
|
|
value: sub
|
2023-11-21 23:32:22 +00:00
|
|
|
}
|
|
|
|
: {
|
2024-01-04 02:39:26 +00:00
|
|
|
overrideValue: sub
|
2023-11-21 23:32:22 +00:00
|
|
|
}
|
|
|
|
|
2024-02-10 02:35:32 +00:00
|
|
|
// If logged out user directly visits a nsfw sub, subs will not contain `sub`, so manually add it
|
|
|
|
// to display the correct sub name in the sub selector
|
|
|
|
const subItems = !sub || subs.find((s) => s === sub) ? subs : [sub].concat(subs)
|
|
|
|
|
2023-11-21 23:32:22 +00:00
|
|
|
return (
|
|
|
|
<Select
|
|
|
|
onChange={onChange || ((_, e) => {
|
2024-01-04 02:39:26 +00:00
|
|
|
const sub = ['home', 'pick territory'].includes(e.target.value) ? undefined : e.target.value
|
2023-11-21 23:32:22 +00:00
|
|
|
if (sub === 'create') {
|
|
|
|
router.push('/territory')
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
let asPath
|
|
|
|
// are we currently in a sub (ie not home)
|
|
|
|
if (router.query.sub) {
|
|
|
|
// are we going to a sub or home?
|
|
|
|
const subReplace = sub ? `/~${sub}` : ''
|
|
|
|
|
|
|
|
// if we are going to a sub, replace the current sub with the new one
|
|
|
|
asPath = router.asPath.replace(`/~${router.query.sub}`, subReplace)
|
|
|
|
// if we're going to home, just go there directly
|
|
|
|
if (asPath === '') {
|
|
|
|
router.push('/')
|
|
|
|
return
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
// we're currently on the home sub
|
|
|
|
// are we in a sub aware route?
|
|
|
|
if (router.pathname.startsWith('/~')) {
|
|
|
|
// if we are, go to the same path but in the sub
|
|
|
|
asPath = `/~${sub}` + router.asPath
|
|
|
|
} else {
|
|
|
|
// otherwise, just go to the sub
|
|
|
|
router.push(sub ? `/~${sub}` : '/')
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
const query = {
|
|
|
|
...router.query,
|
|
|
|
sub
|
|
|
|
}
|
|
|
|
delete query.nodata
|
|
|
|
router.push({
|
|
|
|
pathname: router.pathname,
|
|
|
|
query
|
|
|
|
}, asPath)
|
|
|
|
})}
|
|
|
|
name='sub'
|
|
|
|
size='sm'
|
|
|
|
{...valueProps}
|
|
|
|
{...props}
|
2024-02-23 15:32:20 +00:00
|
|
|
className={`${className} ${styles.subSelect} ${size === 'large' ? styles.subSelectLarge : size === 'medium' ? styles.subSelectMedium : ''}`}
|
2024-02-10 02:35:32 +00:00
|
|
|
items={subItems}
|
2023-11-21 23:32:22 +00:00
|
|
|
/>
|
|
|
|
)
|
|
|
|
}
|