-
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.
day wise messages added in message list and each quill renderer for e…
…ach message
- Loading branch information
Showing
8 changed files
with
217 additions
and
4 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
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,92 @@ | ||
import { format, isToday, isYesterday } from "date-fns"; | ||
import { GetMessageReturnType } from "@/features/messages/api/use-get-messages"; | ||
import { Message } from "./message"; | ||
|
||
interface MessageListProps { | ||
memberName?: string; | ||
memberImage?: string; | ||
channelName?: string; | ||
channelCreationTime?: number; | ||
variant?: "channel" | "thread" | "conversation"; | ||
data: GetMessageReturnType | undefined; | ||
loadMore: () => void; | ||
isLoadingMore: boolean; | ||
canLoadMore: boolean; | ||
} | ||
|
||
const formatDateLabel = (dateStr: string) => { | ||
console.log(dateStr); | ||
|
||
// Replace double hyphens with single hyphen | ||
const correctedDateStr = dateStr.replace(/--/g, "-"); | ||
const date = new Date(correctedDateStr); | ||
|
||
console.log(date); | ||
|
||
if (isToday(date)) return "Today"; | ||
if (isYesterday(date)) return "Yesterday"; | ||
return format(date, "EEEE, MMMM d"); | ||
}; | ||
|
||
export const MessageList = ({ | ||
memberName, | ||
memberImage, | ||
channelName, | ||
channelCreationTime, | ||
data, | ||
variant = "channel", | ||
loadMore, | ||
isLoadingMore, | ||
canLoadMore, | ||
}: MessageListProps) => { | ||
const groupedMessages = data?.reduce( | ||
(groups, message) => { | ||
const date = new Date(message._creationTime); | ||
const dateKey = format(date, "yyyy--MM-dd"); | ||
if (!groups[dateKey]) { | ||
groups[dateKey] = []; | ||
} | ||
groups[dateKey].unshift(message); | ||
return groups; | ||
}, | ||
{} as Record<string, typeof data> | ||
); | ||
return ( | ||
<div className="flex-1 flex flex-col-reverse pb-4 overflow-y-auto messages-scrollbar"> | ||
{Object.entries(groupedMessages || {}).map(([dateKey, messages]) => ( | ||
<div key={dateKey}> | ||
<div className="text-center my-2 relative"> | ||
<hr className="absolute top-1/2 left-0 right-0 border-t border-gray-300" /> | ||
<span className="relative inline-block px-4 py-1 rounded-full text-xs broder border-gray-300 shadow-sm"> | ||
{formatDateLabel(dateKey)} | ||
</span> | ||
</div> | ||
{messages.map((message, index) => { | ||
return ( | ||
<Message | ||
key={message._id} | ||
id={message._id} | ||
memberId={message.memberId} | ||
authorImage={message.user.image} | ||
authorName={message.user.name} | ||
isAuthor={false} | ||
reactions={message.reactions} | ||
body={message.body} | ||
image={message.image} | ||
updatedAt={message.updatedAt} | ||
createdAt={message._creationTime} | ||
isEditing={false} | ||
setEditingId={() => {}} | ||
isCompact={false} | ||
hideThreadButton={false} | ||
threadCount={message.threadCount} | ||
threadImage = {message.threadImage} | ||
threadTimestamp={message.threadTimestamp} | ||
/> | ||
); | ||
})} | ||
</div> | ||
))} | ||
</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,53 @@ | ||
import { Doc, Id } from "../../convex/_generated/dataModel"; | ||
import dynamic from "next/dynamic"; | ||
|
||
const Renderer = dynamic(() => import("@/components/renderer"), { ssr: false }); | ||
interface MessageProps { | ||
id: Id<"messages">; | ||
memberId: Id<"members">; | ||
authorImage?: string; | ||
authorName?: string; | ||
isAuthor?: boolean; | ||
reactions: Array< | ||
Omit<Doc<"reactions">, "memberId"> & { | ||
count: number; | ||
memberIds: Id<"members">[]; | ||
} | ||
>; | ||
body: Doc<"messages">["body"]; | ||
image: string | null | undefined; | ||
createdAt: Doc<"messages">["_creationTime"]; | ||
updatedAt: Doc<"messages">["updatedAt"]; | ||
isEditing: boolean; | ||
isCompact?: boolean; | ||
setEditingId: (id: Id<"messages"> | null) => void; | ||
hideThreadButton?: boolean; | ||
threadCount?: number; | ||
threadImage?: string; | ||
threadTimestamp?: number; | ||
} | ||
export const Message = ({ | ||
id, | ||
isAuthor, | ||
memberId, | ||
authorImage, | ||
authorName = "Member", | ||
reactions, | ||
body, | ||
image, | ||
createdAt, | ||
updatedAt, | ||
isEditing, | ||
isCompact, | ||
setEditingId, | ||
hideThreadButton, | ||
threadCount, | ||
threadImage, | ||
threadTimestamp, | ||
}: MessageProps) => { | ||
return ( | ||
<div className="flex flex-col gap-2 p-1.5 px-5 hover:bg-gray-100"> | ||
<Renderer value={body} /> | ||
</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,42 @@ | ||
import Quill from "quill"; | ||
import { useEffect, useRef, useState } from "react"; | ||
|
||
interface RendererProps { | ||
value: string; | ||
} | ||
|
||
const Renderer = ({ value }: RendererProps) => { | ||
const [isEmpty, setIsEmpty] = useState(false); | ||
const rendererRef = useRef<HTMLDivElement>(null); | ||
|
||
useEffect(() => { | ||
if (!rendererRef.current) return; | ||
const container = rendererRef.current; | ||
const quill = new Quill(document.createElement("div"), { | ||
theme: "snow", | ||
}); | ||
quill.enable(false); | ||
const contents = JSON.parse(value); | ||
quill.setContents(contents); | ||
const isEmpty = | ||
quill | ||
.getText() | ||
.replace(/,(.|\n)*?>/g, "") | ||
.trim().length === 0; | ||
|
||
setIsEmpty(isEmpty); | ||
|
||
container.innerHTML = quill.root.innerHTML; | ||
|
||
return () => { | ||
if (container) { | ||
container.innerHTML = ""; | ||
} | ||
}; | ||
}, [value]); | ||
|
||
if (isEmpty) return null; | ||
return <div ref={rendererRef} className="ql-editor ql-renderer" />; | ||
}; | ||
|
||
export default Renderer; |