-
-
{children}
+const Badge = React.forwardRef
(
+ ({ type, children }: BadgeProps, ref) => {
+ return (
+
-
- )
-}
+ )
+ }
+)
+
+Badge.displayName = "Badge"
export default Badge
diff --git a/src/app/components/post-list/index.tsx b/src/app/components/post-list/index.tsx
index 2fabd677..4470e2d9 100644
--- a/src/app/components/post-list/index.tsx
+++ b/src/app/components/post-list/index.tsx
@@ -2,11 +2,7 @@
import styles from "./post-list.module.css"
import ListItem from "./list-item"
-import {
- ChangeEvent,
- useCallback,
- useState
-} from "react"
+import { ChangeEvent, useCallback, useState } from "react"
import Link from "@components/link"
import type { PostWithFiles } from "@lib/server/prisma"
import Input from "@components/input"
@@ -60,31 +56,42 @@ const PostList = ({
[posts, hasMorePosts]
)
- const onSearch = (query: string) => {
- setSearching(true)
- async function fetchPosts() {
- const res = await fetch(
- `/api/post/search?q=${encodeURIComponent(query)}&userId=${userId}`,
- {
- method: "GET",
- headers: {
- "Content-Type": "application/json"
+ // eslint-disable-next-line react-hooks/exhaustive-deps -- TODO: address this
+ const onSearch = useCallback(
+ debounce((query: string) => {
+ if (!query) {
+ setPosts(initialPosts)
+ setSearching(false)
+ return
+ }
+
+ setSearching(true)
+ async function fetchPosts() {
+ const res = await fetch(
+ `/api/post/search?q=${encodeURIComponent(query)}&userId=${userId}`,
+ {
+ method: "GET",
+ headers: {
+ "Content-Type": "application/json"
+ }
}
- }
- )
- const json = await res.json()
- setPosts(json)
- setSearching(false)
- }
- fetchPosts()
- }
+ )
+ const json = await res.json()
+ setPosts(json)
+ setSearching(false)
+ }
+ fetchPosts()
+ }, 300),
+ [userId]
+ )
- const debouncedSearch = debounce(onSearch, 500)
-
- const handleSearchChange = (e: ChangeEvent
) => {
- setSearchValue(e.target.value)
- debouncedSearch(e.target.value)
- }
+ const onSearchChange = useCallback(
+ (e: ChangeEvent) => {
+ setSearchValue(e.target.value)
+ onSearch(e.target.value)
+ },
+ [onSearch]
+ )
const deletePost = useCallback(
(postId: string) => async () => {
@@ -108,16 +115,18 @@ const PostList = ({
return (
- {!hideSearch &&
-
-
}
+ {!hideSearch && (
+
+
+
+ )}
{!posts &&
Failed to load.
}
{searching && (
@@ -125,7 +134,7 @@ const PostList = ({
)}
- {posts?.length === 0 && posts && (
+ {!searching && posts?.length === 0 && posts && (
No posts found. Create one{" "}
@@ -134,7 +143,7 @@ const PostList = ({
.
)}
- {posts?.length > 0 && (
+ {!searching && posts?.length > 0 && (
{posts.map((post) => {
@@ -149,7 +158,7 @@ const PostList = ({
)}
- {hasMorePosts && !setSearchValue && (
+ {!searching && hasMorePosts && !setSearchValue && (