@@ -4,6 +4,7 @@ import { useMemo, useState } from "react";
44import Link from "next/link" ;
55
66import { CONTENT_TYPE_OPTIONS , type ContentTypeOptions } from "@/constants/content" ;
7+ import { useDebounce } from "@/hooks/use-debounce" ;
78import { useCreateContent } from "@/lib/tanstack/mutation/topic-content" ;
89import { useGetAllContents } from "@/lib/tanstack/query/topic" ;
910import { TopicNodeProps } from "@/types/topic" ;
@@ -33,6 +34,8 @@ export default function ContentList({
3334} : ContentListProps ) {
3435 const [ searchQuery , setSearchQuery ] = useState ( "" ) ;
3536
37+ const debouncedSearchQuery = useDebounce ( searchQuery , 300 ) ;
38+
3639 const {
3740 data : contents ,
3841 isLoading,
@@ -43,8 +46,13 @@ export default function ContentList({
4346 const { mutateAsync : createContent , isPending } = useCreateContent ( id ) ;
4447
4548 const filteredContents = useMemo ( ( ) => {
46- return contents ?. filter ( ( content ) => ! nodes . some ( ( node ) => node . data . item . id === content . id ) ) ;
47- } , [ contents , nodes ] ) ;
49+ const filteredBySearch = contents ?. filter ( ( content ) =>
50+ content . title . toLowerCase ( ) . includes ( debouncedSearchQuery . toLowerCase ( ) )
51+ ) ;
52+ return filteredBySearch ?. filter (
53+ ( content ) => ! nodes . some ( ( node ) => node . data . item . id === content . id )
54+ ) ;
55+ } , [ contents , nodes , debouncedSearchQuery ] ) ;
4856
4957 const onSearchChange = ( event : React . ChangeEvent < HTMLInputElement > ) => {
5058 setSearchQuery ( event . target . value ) ;
0 commit comments