-
Notifications
You must be signed in to change notification settings - Fork 4
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
Showing
5 changed files
with
104 additions
and
68 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,70 @@ | ||
'use client' | ||
|
||
import { useRef, useEffect } from 'react' | ||
import { ScrollArea } from "@/components/ui/scroll-area" | ||
import { Avatar, AvatarFallback, AvatarImage } from "@/components/ui/avatar" | ||
import { format } from "date-fns" | ||
import { IChat, IMessage } from "@/types" | ||
import { getReceiverData, getSenderData } from './getUserData' | ||
|
||
interface ChatSectionProps { | ||
sender: "doctor" | "patient" | ||
messages: IMessage[] | ||
chat: IChat | ||
} | ||
|
||
export default function Messages({ messages, sender, chat }: ChatSectionProps) { | ||
const scrollAreaRef = useRef<HTMLDivElement>(null) | ||
|
||
useEffect(() => { | ||
if (scrollAreaRef.current) { | ||
scrollAreaRef.current.scrollTop = scrollAreaRef.current.scrollHeight | ||
} | ||
}, [messages]) | ||
|
||
return ( | ||
<ScrollArea className="flex-grow p-4" ref={scrollAreaRef}> | ||
<div className="space-y-4"> | ||
{messages.map(({ _id, message, senderId, createdAt }) => { | ||
const isSenderMessage = senderId === (sender === 'patient' ? chat.doctorId : chat.patientId); | ||
|
||
if (isSenderMessage) { | ||
return ( | ||
<div key={_id} className="flex items-end space-x-2 justify-start"> | ||
<Avatar className="w-8 h-8 ring-2 ring-blue-600"> | ||
<AvatarImage src={getSenderData(sender, chat.doctorProfile!, chat.patientProfile!) || `/assets/icons/circle-user.svg`} alt="sender" /> | ||
<AvatarFallback>{getSenderData(sender, chat.doctorName!, chat.patientName!).charAt(0)}</AvatarFallback> | ||
</Avatar> | ||
<div className="flex flex-col items-start max-w-[70%]"> | ||
<div className="bg-blue-600 text-white rounded-2xl rounded-tl-sm p-3 shadow-md"> | ||
<p className="text-sm">{message}</p> | ||
</div> | ||
<span className="text-xs text-gray-400 mt-1"> | ||
{format(new Date(createdAt!), "HH:mm")} | ||
</span> | ||
</div> | ||
</div> | ||
); | ||
} else { | ||
return ( | ||
<div key={_id} className="flex items-end space-x-2 justify-end"> | ||
<div className="flex flex-col items-end max-w-[70%]"> | ||
<div className="bg-gray-200 text-gray-900 rounded-2xl rounded-br-sm p-3 shadow-md"> | ||
<p className="text-sm">{message}</p> | ||
</div> | ||
<span className="text-xs text-gray-400 mt-1"> | ||
{format(new Date(createdAt!), "HH:mm")} | ||
</span> | ||
</div> | ||
<Avatar className="w-8 h-8 ring-2 ring-gray-300"> | ||
<AvatarImage src={getReceiverData(sender, chat.doctorProfile!, chat.patientProfile!) || `/assets/icons/circle-user.svg`} alt="receiver" /> | ||
<AvatarFallback>{getReceiverData(sender, chat.doctorName!, chat.patientName!).charAt(0)}</AvatarFallback> | ||
</Avatar> | ||
</div> | ||
); | ||
} | ||
})} | ||
</div> | ||
</ScrollArea> | ||
) | ||
} |
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
9240253
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Successfully deployed to the following URLs:
avm-care – ./
avm-care-sinans-projects-8d312afe.vercel.app
avm-care.vercel.app
avm-care-git-main-sinans-projects-8d312afe.vercel.app