From 65b610c010c06c4ea26526f33f78fe0ea0d61eb4 Mon Sep 17 00:00:00 2001 From: Pritam Kundu Date: Thu, 28 Nov 2024 22:01:00 +0530 Subject: [PATCH] fix: chats for new users --- client/src/app/(main)/search/page.tsx | 10 +++++----- client/src/components/chats/Chat.tsx | 17 +++++++++++++++-- client/src/components/chats/Sidebar.tsx | 20 +++++++++++++++++--- client/src/components/posts/Post.tsx | 1 + client/src/components/users/Button.tsx | 5 +++-- 5 files changed, 41 insertions(+), 12 deletions(-) diff --git a/client/src/app/(main)/search/page.tsx b/client/src/app/(main)/search/page.tsx index 9fd3553..173875f 100644 --- a/client/src/app/(main)/search/page.tsx +++ b/client/src/app/(main)/search/page.tsx @@ -3,18 +3,18 @@ import { ScrollArea } from '@/components/ui/scroll-area' import type { Metadata } from 'next' interface PageProps { - searchParams: { - q: string - } + searchParams: Promise<{ q: string }> } -export const generateMetadata = ({ searchParams: { q } }: PageProps): Metadata => { +export const generateMetadata = async ({ searchParams }: PageProps): Promise => { + const { q } = await searchParams return { title: `Search results for "${q}"` } } -const Page = ({ searchParams: { q } }: PageProps) => { +const Page = async ({ searchParams }: PageProps) => { + const { q } = await searchParams return (
diff --git a/client/src/components/chats/Chat.tsx b/client/src/components/chats/Chat.tsx index 06709ae..c49b889 100644 --- a/client/src/components/chats/Chat.tsx +++ b/client/src/components/chats/Chat.tsx @@ -1,16 +1,29 @@ 'use client' import { Chat as StreamChat } from 'stream-chat-react' -import { useInitializeChatClient } from '@/hooks' +import { useInitializeChatClient, useSession } from '@/hooks' import { useTheme } from 'next-themes' import { Loader2 } from 'lucide-react' import { Sidebar, Channel } from '.' -import { useState } from 'react' +import { useEffect, useState } from 'react' const Chat = () => { const chatClient = useInitializeChatClient() const [sidebarOpen, setSidebarOpen] = useState(false) const { resolvedTheme } = useTheme() + const { user } = useSession() + + useEffect(() => { + if (chatClient) { + const checkChannels = async () => { + const filter = { type: 'messaging', members: { $in: [user?.id] } } + const channels = await chatClient.queryChannels(filter, {}, { limit: 1 }) + setSidebarOpen(channels.length === 0) + } + checkChannels() + } + }, [chatClient]) + if (!chatClient) { return } diff --git a/client/src/components/chats/Sidebar.tsx b/client/src/components/chats/Sidebar.tsx index f0505b2..782665a 100644 --- a/client/src/components/chats/Sidebar.tsx +++ b/client/src/components/chats/Sidebar.tsx @@ -1,10 +1,10 @@ 'use client' import { ChannelList, ChannelPreviewMessenger, ChannelPreviewUIComponentProps } from 'stream-chat-react' -import { useCallback, useState } from 'react' +import { useCallback, useEffect, useState } from 'react' import { MailPlus, X } from 'lucide-react' import { Button } from '../ui/button' -import { useSession } from '@/hooks' +import { useInitializeChatClient, useSession } from '@/hooks' import { NewChatDialog } from '.' import { cn } from '@/lib/utils' @@ -54,12 +54,26 @@ interface MenuHeaderProps { const MenuHeader = ({ onClose }: MenuHeaderProps) => { const [showNewChatDialog, setShowNewChatDialog] = useState(false) + const chatClient = useInitializeChatClient() + const { user } = useSession() + const [channels, setChannels] = useState(0) + + useEffect(() => { + if (chatClient) { + const checkChannels = async () => { + const filter = { type: 'messaging', members: { $in: [user?.id] } } + const channels = await chatClient.queryChannels(filter, {}, { limit: 1 }) + setChannels(channels.length) + } + checkChannels() + } + }, [chatClient]) return ( <>
-
diff --git a/client/src/components/posts/Post.tsx b/client/src/components/posts/Post.tsx index 1115328..9f09cb5 100644 --- a/client/src/components/posts/Post.tsx +++ b/client/src/components/posts/Post.tsx @@ -159,6 +159,7 @@ const MediaPreview = ({ media }: MediaPreviewProps) => { src={media.url} alt="Post attachment" className="object-cover" + width="100%" sizes="(max-width: 768px) 100vw, (max-width: 1200px) 50vw, 33vw" onError={handleMediaError} /> diff --git a/client/src/components/users/Button.tsx b/client/src/components/users/Button.tsx index f905f25..501f786 100644 --- a/client/src/components/users/Button.tsx +++ b/client/src/components/users/Button.tsx @@ -61,7 +61,7 @@ const UserButton = ({ className }: UserButtonProps) => { Theme - + setTheme('light')} className="cursor-pointer"> Light @@ -84,7 +84,8 @@ const UserButton = ({ className }: UserButtonProps) => { - Logout + + Logout