82 lines
2.2 KiB
TypeScript
82 lines
2.2 KiB
TypeScript
import { useQuery } from "@tanstack/react-query";
|
|
import { useState } from "react";
|
|
import { useNavigate, useParams } from "react-router-dom";
|
|
|
|
import { api } from "../api/client";
|
|
import { useAuth } from "../auth/AuthProvider";
|
|
import { ChatList } from "../components/ChatList";
|
|
import { ChatView } from "../components/ChatView";
|
|
import { CreateChatDialog } from "../components/CreateChatDialog";
|
|
import { EmptyChat } from "../components/EmptyChat";
|
|
import { NavigationRail } from "../components/NavigationRail";
|
|
import { ProfileDrawer } from "../components/ProfileDrawer";
|
|
|
|
type MessengerPageProps = {
|
|
profileOpen?: boolean;
|
|
};
|
|
|
|
export function MessengerPage({ profileOpen = false }: MessengerPageProps) {
|
|
const { user } = useAuth();
|
|
const { chatId } = useParams();
|
|
const navigate = useNavigate();
|
|
const [createChatOpen, setCreateChatOpen] = useState(false);
|
|
|
|
const chatsQuery = useQuery({
|
|
queryKey: ["chats"],
|
|
queryFn: api.chats,
|
|
refetchInterval: 10_000,
|
|
staleTime: 5_000,
|
|
});
|
|
|
|
if (!user) {
|
|
return null;
|
|
}
|
|
|
|
const chats = chatsQuery.data ?? [];
|
|
const selectedChat = chatId
|
|
? chats.find((chat) => chat.id === chatId)
|
|
: undefined;
|
|
|
|
return (
|
|
<main
|
|
className={[
|
|
"messenger-shell",
|
|
chatId ? "messenger-shell--chat-open" : "",
|
|
profileOpen ? "messenger-shell--drawer-open" : "",
|
|
].join(" ")}
|
|
>
|
|
<NavigationRail profileOpen={profileOpen} user={user} />
|
|
|
|
<ChatList
|
|
chats={chats}
|
|
currentUser={user}
|
|
error={chatsQuery.isError}
|
|
isLoading={chatsQuery.isLoading}
|
|
onCreateChat={() => setCreateChatOpen(true)}
|
|
selectedChatID={chatId}
|
|
/>
|
|
|
|
{selectedChat ? (
|
|
<ChatView
|
|
chat={selectedChat}
|
|
currentUser={user}
|
|
onBack={() => navigate("/app")}
|
|
/>
|
|
) : (
|
|
<EmptyChat
|
|
hasChats={chats.length > 0}
|
|
onCreateChat={() => setCreateChatOpen(true)}
|
|
/>
|
|
)}
|
|
|
|
{profileOpen && <ProfileDrawer user={user} />}
|
|
{createChatOpen && (
|
|
<CreateChatDialog
|
|
currentUser={user}
|
|
onClose={() => setCreateChatOpen(false)}
|
|
/>
|
|
)}
|
|
</main>
|
|
);
|
|
}
|