import Container from 'react-bootstrap/Container' import styles from './search.module.css' import SearchIcon from '@/svgs/search-line.svg' import { useEffect, useMemo, useRef, useState } from 'react' import { Form, Input, Select, DatePicker, SubmitButton } from './form' import { useRouter } from 'next/router' import { whenToFrom } from '@/lib/time' import { useMe } from './me' export default function Search ({ sub }) { const router = useRouter() const [q, setQ] = useState(router.query.q || '') const inputRef = useRef(null) const me = useMe() useEffect(() => { inputRef.current?.focus() }, []) const search = async values => { let prefix = '' if (sub) { prefix = `/~${sub}` } if (values.q?.trim() !== '') { if (values.what === 'stackers') { await router.push({ pathname: '/stackers/search', query: { q, what: 'stackers' } }, { pathname: '/stackers/search', query: { q } }) return } if (values.what === '' || values.what === 'all') delete values.what if (values.sort === '' || values.sort === 'zaprank') delete values.sort if (values.when === '' || values.when === 'forever') delete values.when if (values.when !== 'custom') { delete values.from; delete values.to } if (values.from && !values.to) return await router.push({ pathname: prefix + '/search', query: values }) } } const filter = sub !== 'jobs' const what = router.pathname.startsWith('/stackers') ? 'stackers' : router.query.what || 'all' const sort = router.query.sort || 'zaprank' const when = router.query.when || 'forever' const whatItemOptions = useMemo(() => (['all', 'posts', 'comments', me ? 'bookmarks' : undefined, 'stackers'].filter(item => !!item)), [me]) return ( <>
search({ ...values })} >
{ setQ(e.target.value?.trim()) }} />
{filter && router.query.q &&
search({ ...formik?.values, sort: e.target.value })} name='sort' size='sm' overrideValue={sort} items={['zaprank', 'recent', 'comments', 'sats']} /> for