Skip to content

Commit

Permalink
feat: add search to blog posts
Browse files Browse the repository at this point in the history
  • Loading branch information
ammar-ahmed22 committed May 9, 2024
1 parent aa02131 commit 33b6724
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 3 deletions.
7 changes: 5 additions & 2 deletions src/hooks/debounce.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,12 @@ export type UseDebouncedOpts = {
delay?: number
}

export type UseDebouncedResponse<T = any> = [T, T, SetState<T>]

export function useDebounced<T = any>(
defaultValue: T,
opts?: UseDebouncedOpts,
) {
): UseDebouncedResponse<T> {
let delay = 200
if (opts?.delay) delay = opts.delay

Expand All @@ -16,9 +18,10 @@ export function useDebounced<T = any>(
useState<T>(defaultValue)

useEffect(() => {
setTimeout(() => {
const id = setTimeout(() => {
setDebouncedValue(value)
}, delay)
return () => clearTimeout(id)
}, [value, delay])

return [value, debouncedValue, setValue]
Expand Down
9 changes: 8 additions & 1 deletion src/pages/Blog/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import {
import { useQuery } from '@apollo/client'
import BlogCard, { BlogCardSkeleton } from './BlogCard'
import { useBreakpointValue } from '../../hooks/mediaQuery'
import { useDebounced } from '../../hooks/debounce'

const Blog: React.FC = () => {
const textGradient = useTextGradient({
Expand All @@ -20,6 +21,9 @@ const Blog: React.FC = () => {
})
const [tags, setTags] = useState<string[]>([])
const [category, setCategory] = useState<string | undefined>()
const [query, debouncedQuery, setQuery] = useDebounced('', {
delay: 400,
})

const { data, loading } = useQuery<
BlogMetadataQuery.Response,
Expand All @@ -29,6 +33,7 @@ const Blog: React.FC = () => {
onlyPublished: true,
category,
tags,
query: debouncedQuery,
},
})
const isMobile = useBreakpointValue({ default: true, md: false })
Expand All @@ -41,7 +46,7 @@ const Blog: React.FC = () => {
>
Blog
</h1>
<p className='text-default-500 text-xl w-3/5 text-center'>
<p className='text-default-500 text-xl md:w-3/5 w-full text-center'>
Sometimes I like to write about things I've worked on, my
experiences or anything else of interest to me.
</p>
Expand All @@ -51,6 +56,8 @@ const Blog: React.FC = () => {
label='Search'
startContent={<MagnifyingGlassIcon className='size-4' />}
isClearable
value={query}
onValueChange={setQuery}
/>
<div className='grid grid-cols-1 md:grid-cols-2 w-full gap-4'>
<BlogTags
Expand Down

0 comments on commit 33b6724

Please sign in to comment.