diff --git a/src/features/chat/ui/ChatList.tsx b/src/features/chat/ui/ChatList.tsx index 9f1f7492..4eca9fe4 100644 --- a/src/features/chat/ui/ChatList.tsx +++ b/src/features/chat/ui/ChatList.tsx @@ -1,51 +1,38 @@ "use client"; -import { useEffect, useState } from "react"; import ChatItem from "@/entities/chat/ui/ChatItem"; import { fetchChatList } from "../model/chat.api"; import type { Chat } from "@/entities/chat/model/types"; +import { useQuery } from "@tanstack/react-query"; -const ChatList = ({ - onSelect, - tab, -}: { +interface ChatListProps { onSelect: (info: { postingId: number; otherId: number; chatId?: number; }) => void; tab?: "all" | "buyer" | "seller"; -}) => { - const [chats, setChats] = useState([]); - const [loading, setLoading] = useState(true); - const [error, setError] = useState(null); +} - useEffect(() => { - const load = async () => { - setLoading(true); - setError(null); - try { - const role = - tab === "all" ? undefined : tab === "buyer" ? "buyer" : "seller"; - const data = await fetchChatList(role); - console.log(data); - setChats(data); - } catch (err) { - console.error("채팅 목록 불러오기 실패:", err); - setError("채팅 목록을 불러오는 중 오류가 발생했습니다."); - } finally { - setLoading(false); - } - }; - load(); - }, [tab]); +const ChatList = ({ onSelect, tab = "all" }: ChatListProps) => { + const role = tab === "all" ? undefined : tab === "buyer" ? "buyer" : "seller"; - if (loading) { + const { + data: chats = [], + isLoading, + isError, + error, + } = useQuery({ + queryKey: ["chats", role], + queryFn: () => fetchChatList(role), + }); + + if (isLoading) { return

불러오는 중...

; } - if (error) { - return

{error}

; + if (isError) { + console.error("채팅 목록 불러오기 실패:", error); } if (!chats?.length) { @@ -53,6 +40,7 @@ const ChatList = ({

채팅 내역이 없습니다.

); } + return (
{chats.map((chat) => (