-
Notifications
You must be signed in to change notification settings - Fork 151
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
55ce978
commit 2f1e8f6
Showing
13 changed files
with
587 additions
and
16 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
"use client"; | ||
|
||
import { Loader2 } from "lucide-react"; | ||
import { useTheme } from "next-themes"; | ||
import { useState } from "react"; | ||
import { Chat as StreamChat } from "stream-chat-react"; | ||
import ChatChannel from "./ChatChannel"; | ||
import ChatSidebar from "./ChatSidebar"; | ||
import useInitializeChatClient from "./useInitializeChatClient"; | ||
|
||
export default function Chat() { | ||
const chatClient = useInitializeChatClient(); | ||
|
||
const { resolvedTheme } = useTheme(); | ||
|
||
const [sidebarOpen, setSidebarOpen] = useState(false); | ||
|
||
if (!chatClient) { | ||
return <Loader2 className="mx-auto my-3 animate-spin" />; | ||
} | ||
|
||
return ( | ||
<main className="relative w-full overflow-hidden rounded-2xl bg-card shadow-sm"> | ||
<div className="absolute bottom-0 top-0 flex w-full"> | ||
<StreamChat | ||
client={chatClient} | ||
theme={ | ||
resolvedTheme === "dark" | ||
? "str-chat__theme-dark" | ||
: "str-chat__theme-light" | ||
} | ||
> | ||
<ChatSidebar | ||
open={sidebarOpen} | ||
onClose={() => setSidebarOpen(false)} | ||
/> | ||
<ChatChannel | ||
open={!sidebarOpen} | ||
openSidebar={() => setSidebarOpen(true)} | ||
/> | ||
</StreamChat> | ||
</div> | ||
</main> | ||
); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
import { Button } from "@/components/ui/button"; | ||
import { cn } from "@/lib/utils"; | ||
import { Menu } from "lucide-react"; | ||
import { | ||
Channel, | ||
ChannelHeader, | ||
ChannelHeaderProps, | ||
MessageInput, | ||
MessageList, | ||
Window, | ||
} from "stream-chat-react"; | ||
|
||
interface ChatChannelProps { | ||
open: boolean; | ||
openSidebar: () => void; | ||
} | ||
|
||
export default function ChatChannel({ open, openSidebar }: ChatChannelProps) { | ||
return ( | ||
<div className={cn("w-full md:block", !open && "hidden")}> | ||
<Channel> | ||
<Window> | ||
<CustomChannelHeader openSidebar={openSidebar} /> | ||
<MessageList /> | ||
<MessageInput /> | ||
</Window> | ||
</Channel> | ||
</div> | ||
); | ||
} | ||
|
||
interface CustomChannelHeaderProps extends ChannelHeaderProps { | ||
openSidebar: () => void; | ||
} | ||
|
||
function CustomChannelHeader({ | ||
openSidebar, | ||
...props | ||
}: CustomChannelHeaderProps) { | ||
return ( | ||
<div className="flex items-center gap-3"> | ||
<div className="h-full p-2 md:hidden"> | ||
<Button size="icon" variant="ghost" onClick={openSidebar}> | ||
<Menu className="size-5" /> | ||
</Button> | ||
</div> | ||
<ChannelHeader {...props} /> | ||
</div> | ||
); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,100 @@ | ||
import { Button } from "@/components/ui/button"; | ||
import { cn } from "@/lib/utils"; | ||
import { MailPlus, X } from "lucide-react"; | ||
import { useCallback, useState } from "react"; | ||
import { | ||
ChannelList, | ||
ChannelPreviewMessenger, | ||
ChannelPreviewUIComponentProps, | ||
} from "stream-chat-react"; | ||
import { useSession } from "../SessionProvider"; | ||
import NewChatDialog from "./NewChatDialog"; | ||
|
||
interface ChatSidebarProps { | ||
open: boolean; | ||
onClose: () => void; | ||
} | ||
|
||
export default function ChatSidebar({ open, onClose }: ChatSidebarProps) { | ||
const { user } = useSession(); | ||
|
||
const ChannelPreviewCustom = useCallback( | ||
(props: ChannelPreviewUIComponentProps) => ( | ||
<ChannelPreviewMessenger | ||
{...props} | ||
onSelect={() => { | ||
props.setActiveChannel?.(props.channel, props.watchers); | ||
onClose(); | ||
}} | ||
/> | ||
), | ||
[onClose], | ||
); | ||
|
||
return ( | ||
<div | ||
className={cn( | ||
"size-full flex-col border-e md:flex md:w-72", | ||
open ? "flex" : "hidden", | ||
)} | ||
> | ||
<MenuHeader onClose={onClose} /> | ||
<ChannelList | ||
filters={{ | ||
type: "messaging", | ||
members: { $in: [user.id] }, | ||
}} | ||
showChannelSearch | ||
options={{ state: true, presence: true, limit: 8 }} | ||
sort={{ last_message_at: -1 }} | ||
additionalChannelSearchProps={{ | ||
searchForChannels: true, | ||
searchQueryParams: { | ||
channelFilters: { | ||
filters: { members: { $in: [user.id] } }, | ||
}, | ||
}, | ||
}} | ||
Preview={ChannelPreviewCustom} | ||
/> | ||
</div> | ||
); | ||
} | ||
|
||
interface MenuHeaderProps { | ||
onClose: () => void; | ||
} | ||
|
||
function MenuHeader({ onClose }: MenuHeaderProps) { | ||
const [showNewChatDialog, setShowNewChatDialog] = useState(false); | ||
|
||
return ( | ||
<> | ||
<div className="flex items-center gap-3 p-2"> | ||
<div className="h-full md:hidden"> | ||
<Button size="icon" variant="ghost" onClick={onClose}> | ||
<X className="size-5" /> | ||
</Button> | ||
</div> | ||
<h1 className="me-auto text-xl font-bold md:ms-2">Messages</h1> | ||
<Button | ||
size="icon" | ||
variant="ghost" | ||
title="Start new chat" | ||
onClick={() => setShowNewChatDialog(true)} | ||
> | ||
<MailPlus className="size-5" /> | ||
</Button> | ||
</div> | ||
{showNewChatDialog && ( | ||
<NewChatDialog | ||
onOpenChange={setShowNewChatDialog} | ||
onChatCreated={() => { | ||
setShowNewChatDialog(false); | ||
onClose(); | ||
}} | ||
/> | ||
)} | ||
</> | ||
); | ||
} |
Oops, something went wrong.