Skip to content

Commit

Permalink
Merge pull request #461 from rendely/main
Browse files Browse the repository at this point in the history
Add multi select to see multiple subreddits at once
  • Loading branch information
gregrickaby authored Apr 27, 2023
2 parents 0573a69 + 38d2c2d commit 4f630d6
Show file tree
Hide file tree
Showing 3 changed files with 70 additions and 15 deletions.
64 changes: 50 additions & 14 deletions components/Search.tsx
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
import {
Autocomplete,
Badge,
createStyles,
Group,
SelectItemProps
MultiSelect,
SelectItem,
SelectItemProps,
createStyles
} from '@mantine/core'
import {useDebouncedValue} from '@mantine/hooks'
import {IconSearch} from '@tabler/icons-react'
import {forwardRef} from 'react'
import useSWR from 'swr'
import {useRedditContext} from '~/components/RedditProvider'
Expand Down Expand Up @@ -38,10 +38,21 @@ const AutoCompleteItem = forwardRef<HTMLDivElement, ItemProps>(
)
AutoCompleteItem.displayName = 'AutoCompleteItem'

/**
* Stores items selected by multi select
*/
let storedData: (string | SelectItem)[] = [{label: 'gif', value: 'gif'}]

function storeValue(values: any) {
storedData = values.map((value: any) => {
return {value: value, label: value}
})
}

/**
* Search component.
*
* @see https://mantine.dev/core/autocomplete/
* @see https://mantine.dev/core/multi-select/
*/
export default function Search() {
const {setSubreddit, searchInput, setSearchInput, subReddit} =
Expand All @@ -62,20 +73,45 @@ export default function Search() {
setSearchInput(string)
}

/**
* get item data to populate typeahead and combine with already selected items
*/
function formatItems(i: {value: string; label: string}) {
return {value: i.value, label: i.value}
}

function getData(): (string | SelectItem)[] {
let itemOptions
if (results) {
itemOptions = [...storedData, ...results.map(formatItems)]
} else if (beforeSearch) {
itemOptions = [...storedData, ...beforeSearch.map(formatItems)]
} else {
itemOptions = ['Empty']
}
return itemOptions
}

return (
<>
<Autocomplete
<MultiSelect
aria-label="Search sub-reddits"
className={classes.searchBar}
data={results ? results : beforeSearch ? beforeSearch : []}
icon={<IconSearch />}
itemComponent={AutoCompleteItem}
clearable
clearSearchOnBlur
clearSearchOnChange
data={getData()}
hoverOnSearchChange
nothingFound="No subs found. Try searching for something else."
onChange={handleSearch}
onItemSubmit={(value) => setSubreddit(value.value)}
placeholder={subReddit}
size="lg"
value={searchInput}
onChange={(values) => {
storeValue(values)
setSubreddit(encodeURI(values.join('%2B')))
setSearchInput('')
}}
onSearchChange={handleSearch}
placeholder="Pick all that you like"
searchable
searchValue={searchInput}
/>
<Settings />
</>
Expand Down
19 changes: 19 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion public/sitemap-0.xml
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
<?xml version="1.0" encoding="UTF-8"?>
<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9" xmlns:news="http://www.google.com/schemas/sitemap-news/0.9" xmlns:xhtml="http://www.w3.org/1999/xhtml" xmlns:mobile="http://www.google.com/schemas/sitemap-mobile/1.0" xmlns:image="http://www.google.com/schemas/sitemap-image/1.1" xmlns:video="http://www.google.com/schemas/sitemap-video/1.1">
<url><loc>https://redditviewer.vercel.app</loc><lastmod>2023-03-17T12:19:39.508Z</lastmod><changefreq>daily</changefreq><priority>0.7</priority></url>
<url><loc>https://redditviewer.vercel.app</loc><lastmod>2023-04-08T22:29:59.520Z</lastmod><changefreq>daily</changefreq><priority>0.7</priority></url>
</urlset>

1 comment on commit 4f630d6

@vercel
Copy link

@vercel vercel bot commented on 4f630d6 Apr 27, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.