Skip to content

Commit d1f7202

Browse files
committed
Added Profile Picture Rendering in Mentions
1 parent 8bc6ab1 commit d1f7202

1 file changed

Lines changed: 50 additions & 12 deletions

File tree

sdks/js/DittoChatUI/src/components/MessageInput.tsx

Lines changed: 50 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,49 @@ import ChatUser from "@dittolive/ditto-chat-core/dist/types/ChatUser";
66
import Avatar from "./Avatar";
77
import { Mention } from "@dittolive/ditto-chat-core/dist/types/Message";
88
import { clsx } from "clsx";
9+
import { useImageAttachment } from "../utils/useImageAttachment";
10+
import { AttachmentToken } from "@dittolive/ditto";
11+
12+
interface UserMentionItemProps {
13+
user: ChatUser;
14+
isHighlighted: boolean;
15+
onSelect: () => void;
16+
fetchAttachment?: (
17+
token: AttachmentToken,
18+
onProgress: (progress: number) => void,
19+
onComplete: (result: { success: boolean; data?: Uint8Array; metadata?: Record<string, string>; error?: Error }) => void
20+
) => void;
21+
}
22+
23+
function UserMentionItem({
24+
user,
25+
isHighlighted,
26+
onSelect,
27+
fetchAttachment,
28+
}: UserMentionItemProps) {
29+
const profilePictureThumbnail = user.profilePictureThumbnail;
30+
31+
const { imageUrl: avatarUrl } = useImageAttachment({
32+
token: profilePictureThumbnail
33+
? (profilePictureThumbnail as unknown as AttachmentToken)
34+
: null,
35+
fetchAttachment,
36+
autoFetch: true,
37+
});
38+
39+
return (
40+
<button
41+
onClick={onSelect}
42+
className={clsx(
43+
"w-full text-left px-3 py-2 flex items-center space-x-3 hover:bg-(--secondary-bg)",
44+
isHighlighted ? "bg-(--secondary-bg)" : "",
45+
)}
46+
>
47+
<Avatar isUser={true} imageUrl={avatarUrl || undefined} />
48+
<span className="font-semibold">{user.name}</span>
49+
</button>
50+
);
51+
}
952

1053
export interface MessageInputProps {
1154
onSendMessage: (content: string, mentions: Mention[]) => void;
@@ -25,6 +68,7 @@ function MessageInput({
2568
onSaveEdit,
2669
}: MessageInputProps) {
2770
const users: ChatUser[] = useDittoChatStore((state) => state.allUsers);
71+
const fetchAttachment = useDittoChatStore((state) => state.fetchAttachment);
2872
const [text, setText] = useState("");
2973
const [mentions, setMentions] = useState<Mention[]>([]);
3074
const [isAttachMenuOpen, setIsAttachMenuOpen] = useState(false);
@@ -406,18 +450,12 @@ function MessageInput({
406450
<ul className="max-h-60 overflow-y-auto">
407451
{filteredMentionUsers.map((user, index) => (
408452
<li key={user._id}>
409-
<button
410-
onClick={() => handleMentionSelect(user)}
411-
className={clsx(
412-
"w-full text-left px-3 py-2 flex items-center space-x-3 hover:bg-(--secondary-bg)",
413-
index === highlightedMentionIndex
414-
? "bg-(--secondary-bg)"
415-
: "",
416-
)}
417-
>
418-
<Avatar isUser={true} />
419-
<span className="font-semibold">{user.name}</span>
420-
</button>
453+
<UserMentionItem
454+
user={user}
455+
isHighlighted={index === highlightedMentionIndex}
456+
onSelect={() => handleMentionSelect(user)}
457+
fetchAttachment={fetchAttachment}
458+
/>
421459
</li>
422460
))}
423461
</ul>

0 commit comments

Comments
 (0)