2022-03-24 18:35:59 -04:00
|
|
|
import { Code, Dot, Input, Note, Text } from "@geist-ui/core"
|
2022-03-10 02:46:59 -05:00
|
|
|
import NextLink from "next/link"
|
2022-03-06 20:20:01 -05:00
|
|
|
import Link from '../Link'
|
2022-03-06 19:46:59 -05:00
|
|
|
|
|
|
|
import styles from './post-list.module.css'
|
2022-03-09 20:11:37 -05:00
|
|
|
import ListItemSkeleton from "./list-item-skeleton"
|
|
|
|
import ListItem from "./list-item"
|
2022-03-24 18:35:59 -04:00
|
|
|
import { Post } from "@lib/types"
|
|
|
|
import { ChangeEvent, useEffect, useMemo, useState } from "react"
|
|
|
|
import debounce from "lodash.debounce"
|
2022-03-06 19:46:59 -05:00
|
|
|
|
|
|
|
type Props = {
|
2022-03-24 18:35:59 -04:00
|
|
|
posts: Post[]
|
2022-03-06 19:46:59 -05:00
|
|
|
error: any
|
|
|
|
}
|
|
|
|
|
|
|
|
const PostList = ({ posts, error }: Props) => {
|
2022-03-24 18:35:59 -04:00
|
|
|
const [search, setSearchValue] = useState('')
|
|
|
|
// const [searching, setSearching] = useState(false)
|
|
|
|
const [searchResults, setSearchResults] = useState<Post[]>(posts)
|
|
|
|
|
|
|
|
// update posts on search
|
|
|
|
useEffect(() => {
|
|
|
|
if (search) {
|
|
|
|
// support filters like "title is:private has:content" in the text
|
|
|
|
// extract the filters
|
|
|
|
const filters = search.split(' ').filter(s => s.includes(':'))
|
|
|
|
const filtersMap = new Map<string, string>()
|
|
|
|
filters.forEach(f => {
|
|
|
|
const [key, value] = f.split(':')
|
|
|
|
filtersMap.set(key, value)
|
|
|
|
})
|
|
|
|
|
|
|
|
const results = posts.filter(post => {
|
|
|
|
if (filtersMap.has('is') && filtersMap.get('is') !== post.visibility) {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
for (const file of post.files) {
|
|
|
|
if (file.content.toLowerCase().includes(search.toLowerCase())) {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return post.title.toLowerCase().includes(search.toLowerCase())
|
|
|
|
})
|
|
|
|
setSearchResults(results)
|
|
|
|
|
|
|
|
} else {
|
|
|
|
setSearchResults(posts)
|
|
|
|
}
|
|
|
|
}, [search, posts])
|
|
|
|
|
|
|
|
const handleSearchChange = (e: ChangeEvent<HTMLInputElement>) => {
|
|
|
|
setSearchValue(e.target.value)
|
|
|
|
}
|
|
|
|
|
|
|
|
const debouncedSearchHandler = useMemo(
|
|
|
|
() => debounce(handleSearchChange, 300)
|
|
|
|
, []);
|
|
|
|
|
|
|
|
useEffect(() => {
|
|
|
|
return () => {
|
|
|
|
debouncedSearchHandler.cancel();
|
|
|
|
}
|
|
|
|
}, [debouncedSearchHandler]);
|
|
|
|
|
2022-03-06 19:46:59 -05:00
|
|
|
return (
|
|
|
|
<div className={styles.container}>
|
2022-03-24 18:35:59 -04:00
|
|
|
<div className={styles.searchContainer}>
|
|
|
|
<Input scale={3 / 2}
|
|
|
|
clearable
|
|
|
|
placeholder="is:private"
|
|
|
|
onChange={debouncedSearchHandler} />
|
|
|
|
<Text type="secondary">Available filters: <Code>is:visibility</Code></Text>
|
|
|
|
</div>
|
2022-03-06 19:46:59 -05:00
|
|
|
{error && <Text type='error'>Failed to load.</Text>}
|
|
|
|
{
|
2022-03-24 18:35:59 -04:00
|
|
|
!posts && <ul>
|
|
|
|
<li>
|
|
|
|
<ListItemSkeleton />
|
|
|
|
</li>
|
|
|
|
<li>
|
|
|
|
<ListItemSkeleton />
|
|
|
|
</li>
|
|
|
|
</ul>
|
|
|
|
}
|
|
|
|
{posts.length === 0 && !error && <Text type='secondary'>No posts found.Create one <NextLink passHref={true} href="/new"><Link color>here</Link></NextLink>.</Text>}
|
|
|
|
{searchResults.length === 0 && <Text>No posts returned. Create one <NextLink passHref={true} href="/new"><Link color>here</Link></NextLink>.</Text>}
|
|
|
|
{
|
|
|
|
searchResults?.length > 0 && <div>
|
2022-03-06 19:46:59 -05:00
|
|
|
<ul>
|
2022-03-24 18:35:59 -04:00
|
|
|
{searchResults.map((post) => {
|
2022-03-09 20:11:37 -05:00
|
|
|
return <ListItem post={post} key={post.id} />
|
2022-03-06 19:46:59 -05:00
|
|
|
})}
|
|
|
|
</ul>
|
|
|
|
</div>
|
|
|
|
}
|
2022-03-09 04:33:22 -05:00
|
|
|
</div >
|
2022-03-06 19:46:59 -05:00
|
|
|
)
|
|
|
|
}
|
|
|
|
|
2022-03-09 20:11:37 -05:00
|
|
|
export default PostList
|