stacker.news/components/search.js

138 lines
4.9 KiB
JavaScript
Raw Normal View History

2023-07-24 18:35:05 +00:00
import Container from 'react-bootstrap/Container'
2022-01-26 21:43:18 +00:00
import styles from './search.module.css'
import SearchIcon from '@/svgs/search-line.svg'
Search bookmarks (#1075) * Support `is:bookmarked` search option to search my bookmarked items * Update the worker search module to include `bookmarkedBy: Array<Number>` which contains the list of user ids which have bookmarked a given item * Add a trigger on the `Bookmark` DB table to re-index the corresponding item when a bookmark is added/removed * Update the Search resolver to check for a `is:bookmarked` query option. If provided, include it as an option in the search request. This updates search to look for items which are bookmarked by the current user. By default, this preserves stacker privacy so you can only search your own bookmarks * Update the search page UI to show how to invoke searching your own bookmarks * undo `is:bookmarked` support, add `bookmarks` item in search select * short circuit return empty payload for anon requesting bookmarks * remove console.log for debugging * fix indexing a new item that has yet to be bookmarked * update db migration to re-index all existing bookmarked items one time * fix the case where deleting a bookmark doesn't trigger a new index of items explictly specify a `updatedAt` value when deleting a bookmark, to ensure that deleting a bookmark results in a new indexed version of the bookmarked item * update search indexer to use the latest of all three choices for the latest version * give bookmark index jobs longer expiration --------- Co-authored-by: Keyan <34140557+huumn@users.noreply.github.com> Co-authored-by: keyan <keyan.kousha+huumn@gmail.com>
2024-04-19 18:24:48 +00:00
import { useEffect, useMemo, useRef, useState } from 'react'
import { Form, Input, Select, DatePicker, SubmitButton } from './form'
2022-01-27 19:18:48 +00:00
import { useRouter } from 'next/router'
import { whenToFrom } from '@/lib/time'
Search bookmarks (#1075) * Support `is:bookmarked` search option to search my bookmarked items * Update the worker search module to include `bookmarkedBy: Array<Number>` which contains the list of user ids which have bookmarked a given item * Add a trigger on the `Bookmark` DB table to re-index the corresponding item when a bookmark is added/removed * Update the Search resolver to check for a `is:bookmarked` query option. If provided, include it as an option in the search request. This updates search to look for items which are bookmarked by the current user. By default, this preserves stacker privacy so you can only search your own bookmarks * Update the search page UI to show how to invoke searching your own bookmarks * undo `is:bookmarked` support, add `bookmarks` item in search select * short circuit return empty payload for anon requesting bookmarks * remove console.log for debugging * fix indexing a new item that has yet to be bookmarked * update db migration to re-index all existing bookmarked items one time * fix the case where deleting a bookmark doesn't trigger a new index of items explictly specify a `updatedAt` value when deleting a bookmark, to ensure that deleting a bookmark results in a new indexed version of the bookmarked item * update search indexer to use the latest of all three choices for the latest version * give bookmark index jobs longer expiration --------- Co-authored-by: Keyan <34140557+huumn@users.noreply.github.com> Co-authored-by: keyan <keyan.kousha+huumn@gmail.com>
2024-04-19 18:24:48 +00:00
import { useMe } from './me'
2022-01-26 21:43:18 +00:00
2022-02-17 17:23:43 +00:00
export default function Search ({ sub }) {
2022-01-27 19:18:48 +00:00
const router = useRouter()
2022-10-20 22:44:44 +00:00
const [q, setQ] = useState(router.query.q || '')
const inputRef = useRef(null)
Search bookmarks (#1075) * Support `is:bookmarked` search option to search my bookmarked items * Update the worker search module to include `bookmarkedBy: Array<Number>` which contains the list of user ids which have bookmarked a given item * Add a trigger on the `Bookmark` DB table to re-index the corresponding item when a bookmark is added/removed * Update the Search resolver to check for a `is:bookmarked` query option. If provided, include it as an option in the search request. This updates search to look for items which are bookmarked by the current user. By default, this preserves stacker privacy so you can only search your own bookmarks * Update the search page UI to show how to invoke searching your own bookmarks * undo `is:bookmarked` support, add `bookmarks` item in search select * short circuit return empty payload for anon requesting bookmarks * remove console.log for debugging * fix indexing a new item that has yet to be bookmarked * update db migration to re-index all existing bookmarked items one time * fix the case where deleting a bookmark doesn't trigger a new index of items explictly specify a `updatedAt` value when deleting a bookmark, to ensure that deleting a bookmark results in a new indexed version of the bookmarked item * update search indexer to use the latest of all three choices for the latest version * give bookmark index jobs longer expiration --------- Co-authored-by: Keyan <34140557+huumn@users.noreply.github.com> Co-authored-by: keyan <keyan.kousha+huumn@gmail.com>
2024-04-19 18:24:48 +00:00
const me = useMe()
2022-01-27 19:18:48 +00:00
useEffect(() => {
inputRef.current?.focus()
2022-01-27 19:18:48 +00:00
}, [])
2022-10-20 22:44:44 +00:00
const search = async values => {
let prefix = ''
if (sub) {
prefix = `/~${sub}`
}
2022-10-25 17:13:06 +00:00
2022-10-20 22:44:44 +00:00
if (values.q?.trim() !== '') {
2023-07-09 17:37:12 +00:00
if (values.what === 'stackers') {
2022-10-25 17:13:06 +00:00
await router.push({
2023-07-09 17:37:12 +00:00
pathname: '/stackers/search',
query: { q, what: 'stackers' }
}, {
pathname: '/stackers/search',
query: { q }
2022-10-25 17:13:06 +00:00
})
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
2022-10-20 22:44:44 +00:00
await router.push({
pathname: prefix + '/search',
query: values
})
}
}
2023-05-01 20:58:30 +00:00
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'
Search bookmarks (#1075) * Support `is:bookmarked` search option to search my bookmarked items * Update the worker search module to include `bookmarkedBy: Array<Number>` which contains the list of user ids which have bookmarked a given item * Add a trigger on the `Bookmark` DB table to re-index the corresponding item when a bookmark is added/removed * Update the Search resolver to check for a `is:bookmarked` query option. If provided, include it as an option in the search request. This updates search to look for items which are bookmarked by the current user. By default, this preserves stacker privacy so you can only search your own bookmarks * Update the search page UI to show how to invoke searching your own bookmarks * undo `is:bookmarked` support, add `bookmarks` item in search select * short circuit return empty payload for anon requesting bookmarks * remove console.log for debugging * fix indexing a new item that has yet to be bookmarked * update db migration to re-index all existing bookmarked items one time * fix the case where deleting a bookmark doesn't trigger a new index of items explictly specify a `updatedAt` value when deleting a bookmark, to ensure that deleting a bookmark results in a new indexed version of the bookmarked item * update search indexer to use the latest of all three choices for the latest version * give bookmark index jobs longer expiration --------- Co-authored-by: Keyan <34140557+huumn@users.noreply.github.com> Co-authored-by: keyan <keyan.kousha+huumn@gmail.com>
2024-04-19 18:24:48 +00:00
const whatItemOptions = useMemo(() => (['all', 'posts', 'comments', me ? 'bookmarks' : undefined, 'stackers'].filter(item => !!item)), [me])
2022-01-26 21:43:18 +00:00
return (
<>
<div className={styles.searchSection}>
2024-02-23 15:32:20 +00:00
<Container className={`px-0 ${styles.searchContainer}`}>
<Form
initial={{ q, what, sort, when, from: '', to: '' }}
onSubmit={values => search({ ...values })}
>
2024-02-23 15:32:20 +00:00
<div className={`${styles.active} mb-3`}>
<Input
name='q'
required
autoFocus
groupClassName='me-3 mb-0 flex-grow-1'
className='flex-grow-1'
clear
innerRef={inputRef}
overrideValue={q}
onChange={async (formik, e) => {
setQ(e.target.value?.trim())
}}
/>
<SubmitButton variant='primary' className={styles.search}>
<SearchIcon width={22} height={22} />
</SubmitButton>
</div>
{filter && router.query.q &&
<div className='text-muted fw-bold d-flex align-items-center flex-wrap'>
<div className='text-muted fw-bold d-flex align-items-center mb-2'>
<Select
groupClassName='me-2 mb-0'
onChange={(formik, e) => search({ ...formik?.values, what: e.target.value })}
name='what'
size='sm'
overrideValue={what}
Search bookmarks (#1075) * Support `is:bookmarked` search option to search my bookmarked items * Update the worker search module to include `bookmarkedBy: Array<Number>` which contains the list of user ids which have bookmarked a given item * Add a trigger on the `Bookmark` DB table to re-index the corresponding item when a bookmark is added/removed * Update the Search resolver to check for a `is:bookmarked` query option. If provided, include it as an option in the search request. This updates search to look for items which are bookmarked by the current user. By default, this preserves stacker privacy so you can only search your own bookmarks * Update the search page UI to show how to invoke searching your own bookmarks * undo `is:bookmarked` support, add `bookmarks` item in search select * short circuit return empty payload for anon requesting bookmarks * remove console.log for debugging * fix indexing a new item that has yet to be bookmarked * update db migration to re-index all existing bookmarked items one time * fix the case where deleting a bookmark doesn't trigger a new index of items explictly specify a `updatedAt` value when deleting a bookmark, to ensure that deleting a bookmark results in a new indexed version of the bookmarked item * update search indexer to use the latest of all three choices for the latest version * give bookmark index jobs longer expiration --------- Co-authored-by: Keyan <34140557+huumn@users.noreply.github.com> Co-authored-by: keyan <keyan.kousha+huumn@gmail.com>
2024-04-19 18:24:48 +00:00
items={whatItemOptions}
/>
{what !== 'stackers' &&
<>
by
<Select
groupClassName='mx-2 mb-0'
onChange={(formik, e) => search({ ...formik?.values, sort: e.target.value })}
name='sort'
size='sm'
overrideValue={sort}
2023-11-14 00:17:45 +00:00
items={['zaprank', 'recent', 'comments', 'sats']}
/>
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() } : {}
search({ ...formik?.values, when: e.target.value, ...range })
}}
name='when'
size='sm'
overrideValue={when}
items={['custom', 'forever', 'day', 'week', 'month', 'year']}
/>
</>}
</div>
{when === 'custom' &&
<DatePicker
fromName='from'
toName='to'
className='p-0 px-2'
onChange={(formik, [from, to], e) => {
2023-11-14 16:23:44 +00:00
search({ ...formik?.values, from: from.getTime(), to: to.getTime() })
}}
from={router.query.from}
to={router.query.to}
when={when}
/>}
</div>}
</Form>
2022-01-26 21:43:18 +00:00
</Container>
</div>
</>
)
}