-
Notifications
You must be signed in to change notification settings - Fork 1
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
34aeb3e
commit 2704ecc
Showing
6 changed files
with
221 additions
and
7 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,18 @@ | ||
import { NextResponse } from "next/server"; | ||
|
||
interface IParams { | ||
conversationId?: string; | ||
} | ||
|
||
|
||
export async function POST( | ||
request: Request, | ||
{ params }: { params: IParams } | ||
) { | ||
try { | ||
|
||
} catch (error) { | ||
console.log(error, 'ERROR_MESSAGES_SEEN'); | ||
return new NextResponse("Internal Error", { status: 500 }) | ||
} | ||
} |
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,69 @@ | ||
import { currentUser } from "@/lib/current-user"; | ||
import { db } from "@/lib/db"; | ||
import { NextResponse } from "next/server"; | ||
|
||
export async function POST( | ||
request: Request | ||
) { | ||
const user = await currentUser(); | ||
if (!user) return new NextResponse('Unauthorized', { status: 401 }) | ||
|
||
try { | ||
const body = await request.json(); | ||
const { message, image, conversationId } = body; | ||
|
||
const newMessage = await db.message.create({ | ||
data: { | ||
body: message, | ||
image, | ||
conversation: { | ||
connect: { | ||
id: conversationId | ||
} | ||
}, | ||
sender: { | ||
connect: { | ||
id: user.id | ||
} | ||
}, | ||
seen: { | ||
connect: { | ||
id: user.id | ||
} | ||
} | ||
}, | ||
include: { | ||
seen: true, | ||
sender: true | ||
} | ||
}) | ||
|
||
const updatedConversation = await db.conversation.update({ | ||
where: { | ||
id: conversationId | ||
}, | ||
data: { | ||
lastMessageAt: new Date(), | ||
messages: { | ||
connect: { | ||
id: newMessage.id | ||
} | ||
} | ||
}, | ||
include: { | ||
users: true, | ||
messages: { | ||
include: { | ||
seen: true | ||
} | ||
} | ||
} | ||
}); | ||
|
||
|
||
return NextResponse.json(newMessage) | ||
} catch (error) { | ||
console.log(error, 'ERROR_MESSAGES'); | ||
return new NextResponse('Internal Error', { status: 500 }) | ||
} | ||
} |
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
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,83 @@ | ||
"use client" | ||
|
||
import { useCurrentUser } from "@/hooks/use-current-user"; | ||
import { cn } from "@/lib/utils"; | ||
import { FullMessageType } from "@/types" | ||
import { Avatar } from "@nextui-org/react"; | ||
import { format } from "date-fns"; | ||
import Image from "next/image"; | ||
|
||
interface MessageBoxProps { | ||
data: FullMessageType; | ||
isLast?: boolean; | ||
} | ||
|
||
const MessageBox = ({ | ||
data, | ||
isLast | ||
}: MessageBoxProps) => { | ||
const session = useCurrentUser(); | ||
|
||
const isOwn = session?.email === data.sender.email; | ||
const seenList = (data.seen || []) | ||
.filter((user) => user.email !== data.sender.email) | ||
.map((user) => user.name) | ||
.join(', '); | ||
|
||
const container = cn( | ||
"flex gap-3 p-4", | ||
isOwn && "justify-end" | ||
); | ||
|
||
const avatar = cn(isOwn && "order-2") | ||
|
||
const body = cn( | ||
"flex flex-col gap-2", | ||
isOwn && "items-end" | ||
) | ||
|
||
const message = cn( | ||
"text-sm w-fit overflow-hidden", | ||
isOwn ? "bg-primary text-white" : "bg-zinc-800 text-white", | ||
data.image ? "rounded-md p-0" : "rounded-full py-2 px-3" | ||
) | ||
|
||
return ( | ||
<div className={container}> | ||
<div className={avatar}> | ||
<div className="relative"> | ||
<Avatar src={data?.sender.image as string} alt="avatar" showFallback /> | ||
<span className="absolute block rounded-full bg-green-500 ring-2 ring-white top-[2px] right-[2px] h-2 w-2 md:h-2 md:w-2" /> | ||
</div> | ||
</div> | ||
<div className={body}> | ||
<div className="flex items-center gap-2"> | ||
<div className="text-sm truncate w-[150px] text-white/80"> | ||
{data.sender.name} | ||
</div> | ||
<div className="text-sm text-white/50"> | ||
{format(new Date(data.createdAt), 'p')} | ||
</div> | ||
</div> | ||
<div className={message}> | ||
{data.image ? ( | ||
<Image | ||
alt="image" | ||
height={288} | ||
width={288} | ||
src={data.image} | ||
className="object-cover cursor-pointer hover:scale-110 transition translate" | ||
/> | ||
): ( | ||
<div> | ||
{data.body} | ||
</div> | ||
)} | ||
</div> | ||
|
||
</div> | ||
</div> | ||
) | ||
} | ||
|
||
export default MessageBox |