import Button from 'react-bootstrap/Button' import Container from 'react-bootstrap/Container' import styles from './search.module.css' import SearchIcon from '../svgs/search-line.svg' import CloseIcon from '../svgs/close-line.svg' import { useEffect, useState } from 'react' import { Form, Input, Select, SubmitButton } from './form' import { useRouter } from 'next/router' export default function Search ({ sub }) { const router = useRouter() const [searching, setSearching] = useState(router.query.q) const [q, setQ] = useState(router.query.q || '') const [atBottom, setAtBottom] = useState() useEffect(() => { setAtBottom(Math.ceil(window.innerHeight + window.pageYOffset) >= document.body.offsetHeight) window.onscroll = function (ev) { if (Math.ceil(window.innerHeight + window.pageYOffset) >= document.body.offsetHeight) { setAtBottom(true) } else { setAtBottom(false) } } }, []) 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 === 'match') delete values.sort if (values.when === '' || values.when === 'forever') delete values.when await router.push({ pathname: prefix + '/search', query: values }) } } const showSearch = atBottom || searching || router.query.q const filter = sub !== 'jobs' const what = router.pathname.startsWith('/stackers') ? 'stackers' : router.query.what || 'all' const sort = router.query.sort || 'match' const when = router.query.when || 'forever' return ( <>
{showSearch ? (
{filter &&
search({ ...formik?.values, sort: e.target.value })} name='sort' size='sm' overrideValue={sort} items={['match', 'recent', 'comments', 'sats', 'votes']} /> for { setSearching(true) setQ(e.target.value?.trim()) }} /> {q || atBottom || router.query.q ? ( ) : ( )}
) : ( )}
) }