added member chatheader and conversation page
This commit is contained in:
parent
ad78831d7e
commit
7d48962a96
@ -1,7 +1,58 @@
|
|||||||
const MemberIdPage = () => {
|
import { ChatHeader } from "@/components/chat/chat-header";
|
||||||
|
import { getOrCreateConversation } from "@/lib/conversation";
|
||||||
|
import { currentProfile } from "@/lib/current-profile";
|
||||||
|
import { db } from "@/lib/db";
|
||||||
|
import { redirectToSignIn } from "@clerk/nextjs";
|
||||||
|
import { redirect } from "next/navigation";
|
||||||
|
|
||||||
|
interface MemberIdPageProps{
|
||||||
|
params: {
|
||||||
|
serverId: string;
|
||||||
|
memberId: string;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
const MemberIdPage = async ({
|
||||||
|
params
|
||||||
|
}: MemberIdPageProps) => {
|
||||||
|
const profile = await currentProfile();
|
||||||
|
|
||||||
|
if (!profile) {
|
||||||
|
return redirectToSignIn();
|
||||||
|
}
|
||||||
|
|
||||||
|
const currentMember = await db.member.findFirst({
|
||||||
|
where: {
|
||||||
|
serverId: params.serverId,
|
||||||
|
profileId: profile.id,
|
||||||
|
},
|
||||||
|
include: {
|
||||||
|
profile: true,
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
if (!currentMember) {
|
||||||
|
return redirect(`/`);
|
||||||
|
}
|
||||||
|
|
||||||
|
const conversation = await getOrCreateConversation(currentMember.id, params.memberId);
|
||||||
|
|
||||||
|
if (!conversation) {
|
||||||
|
return redirect(`/servers/${params.serverId}`);
|
||||||
|
}
|
||||||
|
|
||||||
|
const { MemberOne, MemberTwo } = conversation;
|
||||||
|
|
||||||
|
const otherMember = MemberOne.profileId === profile.id ? MemberTwo : MemberOne;
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<div>
|
<div className="bg-white dark:bg-[#313338] flex flex-col h-full">
|
||||||
Member ID Page
|
<ChatHeader
|
||||||
|
imageUrl={otherMember.profile.imageUrl}
|
||||||
|
name={otherMember.profile.name}
|
||||||
|
serverId={params.serverId}
|
||||||
|
type={"conversation"}
|
||||||
|
/>
|
||||||
</div>
|
</div>
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
@ -1,5 +1,6 @@
|
|||||||
import { Hash, Menu } from "lucide-react"
|
import { Hash, Menu } from "lucide-react"
|
||||||
import { MobileToggle } from "@/components/mobile-toggle";
|
import { MobileToggle } from "@/components/mobile-toggle";
|
||||||
|
import { UserAvatar } from "../user-avatar";
|
||||||
|
|
||||||
interface ChatHeaderProps {
|
interface ChatHeaderProps {
|
||||||
serverId: string;
|
serverId: string;
|
||||||
@ -21,6 +22,12 @@ export const ChatHeader = ({
|
|||||||
{type === "channel" && (
|
{type === "channel" && (
|
||||||
<Hash className="w-5 h-5 text-zinc-500 dark:text-zinc-400 mr-2"/>
|
<Hash className="w-5 h-5 text-zinc-500 dark:text-zinc-400 mr-2"/>
|
||||||
)}
|
)}
|
||||||
|
{type === "conversation" && (
|
||||||
|
<UserAvatar
|
||||||
|
src={imageUrl}
|
||||||
|
className="w-8 h-8 md:h-8 md:w-8 mr-2"
|
||||||
|
/>
|
||||||
|
)}
|
||||||
<p className="font-semibold text-md text-black dark:text-white">
|
<p className="font-semibold text-md text-black dark:text-white">
|
||||||
{name}
|
{name}
|
||||||
</p>
|
</p>
|
||||||
|
72
lib/conversation.ts
Normal file
72
lib/conversation.ts
Normal file
@ -0,0 +1,72 @@
|
|||||||
|
import { db } from "@/lib/db";
|
||||||
|
|
||||||
|
export const getOrCreateConversation = async (memberOneId: string, memberTwoId: string) => {
|
||||||
|
let conversation = await findConversation(memberOneId, memberTwoId) || await findConversation(memberTwoId, memberOneId);
|
||||||
|
|
||||||
|
if (!conversation)
|
||||||
|
{
|
||||||
|
conversation = await createNewConversation(memberOneId, memberTwoId);
|
||||||
|
}
|
||||||
|
|
||||||
|
return conversation;
|
||||||
|
}
|
||||||
|
|
||||||
|
const findConversation = async (memberOneId: string, memberTwoId: string) => {
|
||||||
|
try
|
||||||
|
{
|
||||||
|
return await db.conversation.findFirst({
|
||||||
|
where: {
|
||||||
|
AND: [
|
||||||
|
{ memberOneId: memberOneId},
|
||||||
|
{ memberTwoId: memberTwoId},
|
||||||
|
],
|
||||||
|
},
|
||||||
|
include: {
|
||||||
|
MemberOne: {
|
||||||
|
include: {
|
||||||
|
profile: true,
|
||||||
|
}
|
||||||
|
},
|
||||||
|
MemberTwo: {
|
||||||
|
include: {
|
||||||
|
profile: true,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
});
|
||||||
|
}
|
||||||
|
catch (error)
|
||||||
|
{
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
const createNewConversation = async (memberOneId: string, memberTwoId: string) => {
|
||||||
|
try
|
||||||
|
{
|
||||||
|
return await db.conversation.create({
|
||||||
|
data:{
|
||||||
|
memberOneId,
|
||||||
|
memberTwoId,
|
||||||
|
},
|
||||||
|
include: {
|
||||||
|
MemberOne: {
|
||||||
|
include: {
|
||||||
|
profile: true,
|
||||||
|
}
|
||||||
|
},
|
||||||
|
MemberTwo: {
|
||||||
|
include: {
|
||||||
|
profile: true,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
},
|
||||||
|
|
||||||
|
});
|
||||||
|
}
|
||||||
|
catch (error)
|
||||||
|
{
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
}
|
Loading…
x
Reference in New Issue
Block a user