stacker.news/components/top-header.js

102 lines
3.2 KiB
JavaScript
Raw Normal View History

2021-12-16 23:05:31 +00:00
import { useRouter } from 'next/router'
import { Form, Select, DatePicker } from './form'
import { ITEM_SORTS, USER_SORTS, WHENS } from '../lib/constants'
2023-11-14 16:23:44 +00:00
import { whenToFrom } from '../lib/time'
2023-02-03 19:10:18 +00:00
2023-06-12 22:15:20 +00:00
export default function TopHeader ({ sub, cat }) {
2021-12-16 23:05:31 +00:00
const router = useRouter()
2022-10-25 21:35:32 +00:00
const top = async values => {
2023-02-03 19:10:18 +00:00
const { what, when, ...query } = values
2023-02-09 18:41:28 +00:00
if (what === 'cowboys') {
await router.push({
pathname: `/top/${what}`
})
return
}
2023-06-12 22:15:20 +00:00
const prefix = sub ? `/~${sub}` : ''
if (typeof query.by !== 'undefined') {
if (query.by === '' ||
2023-07-26 00:45:35 +00:00
(what === 'stackers' && (query.by === 'stacked' || !USER_SORTS.includes(query.by))) ||
(what !== 'stackers' && (query.by === 'zaprank' || !ITEM_SORTS.includes(query.by)))) {
delete query.by
2023-02-03 19:10:18 +00:00
}
}
if (when !== 'custom') { delete query.from; delete query.to }
if (query.from && !query.to) return
2023-02-03 19:10:18 +00:00
2022-10-25 21:35:32 +00:00
await router.push({
2023-06-12 22:15:20 +00:00
pathname: `${prefix}/top/${what}/${when || 'day'}`,
2023-02-03 19:10:18 +00:00
query
2022-10-25 21:35:32 +00:00
})
}
2021-12-16 23:05:31 +00:00
const what = cat
const by = router.query.by || (what === 'stackers' ? 'stacked' : 'zaprank')
const when = router.query.when || ''
2021-12-16 23:05:31 +00:00
return (
2022-10-25 21:35:32 +00:00
<div className='d-flex'>
<Form
2023-07-24 18:35:05 +00:00
className='me-auto'
initial={{ what, by, when, from: '', to: '' }}
2022-10-25 21:35:32 +00:00
onSubmit={top}
>
<div className='text-muted fw-bold my-3 d-flex align-items-center flex-wrap'>
<div className='text-muted fw-bold my-2 d-flex align-items-center'>
top
<Select
groupClassName='mx-2 mb-0'
onChange={(formik, e) => top({ ...formik?.values, what: e.target.value })}
name='what'
size='sm'
overrideValue={what}
items={router?.query?.sub ? ['posts', 'comments'] : ['posts', 'comments', 'stackers', 'cowboys']}
/>
{cat !== 'cowboys' &&
<>
by
<Select
groupClassName='mx-2 mb-0'
onChange={(formik, e) => top({ ...formik?.values, by: e.target.value })}
name='by'
size='sm'
overrideValue={by}
items={cat === 'stackers' ? USER_SORTS : ITEM_SORTS}
/>
for
<Select
groupClassName='mb-0 mx-2'
onChange={(formik, e) => {
2023-11-14 16:23:44 +00:00
const range = e.target.value === 'custom' ? { from: whenToFrom(when), to: Date.now() } : {}
top({ ...formik?.values, when: e.target.value, ...range })
}}
name='when'
size='sm'
overrideValue={when}
items={WHENS}
/>
</>}
2023-02-09 18:41:28 +00:00
</div>
{when === 'custom' &&
<DatePicker
fromName='from'
toName='to'
className='p-0 px-2 my-2'
onChange={(formik, [from, to], e) => {
2023-11-14 16:23:44 +00:00
top({ ...formik?.values, from: from.getTime(), to: to.getTime() })
}}
from={router.query.from}
to={router.query.to}
when={when}
/>}
2022-10-25 21:35:32 +00:00
</div>
</Form>
</div>
2021-12-16 23:05:31 +00:00
)
}