import { Link } from "@remix-run/react"; import AppInput from "../AppInput"; import { useState } from "react"; import { useGetUserThreads, useGetStoreThreads, } from "../../requestHandler/use-chat-hooks"; import { ThreadList, ThreadParticipant, UserProfile, } from "../../../src/api/types"; import { useServiceGetProfile } from "../../requestHandler/use-profile-hooks"; import UiProvider from "../UiProvider"; import SellerLogo from "../SellerLogo"; import { Search, User } from "lucide-react"; import ProfilePagesHeader from "../ProfilePagesHeader"; interface ThreadsPageProps { type: "user" | "store"; storeId?: string; } export default function ThreadsPage({ type, storeId }: ThreadsPageProps) { const [searchValue, setSearchValue] = useState(""); // Seller dashboard shows the STORE's inbox; the profile page shows the user's // personal (buyer) chats. Two different endpoints so the two never mix. const userThreads = useGetUserThreads(); const storeThreads = useGetStoreThreads(type === "store" ? storeId : undefined); const { data: threads, isLoading, isError, refetch, } = type === "store" ? storeThreads : userThreads; const sortedThreads = threads?.sort((a, b) => { const dateA = new Date(a.lastMessageTime || "2025-01-18T00:00:00.000Z"); const dateB = new Date(b.lastMessageTime || "2025-01-18T00:00:00.000Z"); return dateB.getTime() - dateA.getTime(); }); const { data: dataProfile, isFetching: isFetchingProfile } = useServiceGetProfile(); return (
{thread.isCreator ? otherParticipant?.storeName || otherParticipant?.username : otherParticipant?.name}
{new Date(thread.lastMessageTime).toLocaleTimeString( "fa-IR", { hour: "2-digit", minute: "2-digit", } )}
)} {hasUnread && ({thread.lastMessage?.content || (thread.lastMessage?.product ? `🛍 ${thread.lastMessage.product.title || "محصول"}` : "بدون پیام")}