Skip to content

Commit 3c2186e

Browse files
committedJul 30, 2024
feat(mentions) : added the mentions feature.
1 parent 32900bd commit 3c2186e

File tree

2 files changed

+35
-2
lines changed

2 files changed

+35
-2
lines changed
 

‎app/provider.tsx

+13-1
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,14 @@
11
'use client';
22

33
import Loader from "@/components/Loaders";
4-
import { getClerkUsers } from "@/lib/actions/user.actions";
4+
import { getClerkUsers, getDocumentUsers } from "@/lib/actions/user.actions";
5+
import { useUser } from "@clerk/nextjs";
56
import { ClientSideSuspense, LiveblocksProvider } from "@liveblocks/react/suspense";
67
import { ReactNode } from "react";
78

89
const Provider = ({ children }: { children: ReactNode}) => {
10+
const { user: clerkUser } = useUser();
11+
912
return (
1013
<LiveblocksProvider
1114
authEndpoint="/api/liveblocks-auth"
@@ -14,6 +17,15 @@ const Provider = ({ children }: { children: ReactNode}) => {
1417

1518
return users;
1619
}}
20+
resolveMentionSuggestions={async ({ text, roomId }) => {
21+
const roomUsers = await getDocumentUsers({
22+
roomId,
23+
currentUser: clerkUser?.emailAddresses[0].emailAddress!,
24+
text,
25+
})
26+
27+
return roomUsers;
28+
}}
1729
>
1830
<ClientSideSuspense fallback={<Loader/>}>
1931
{children}

‎lib/actions/user.actions.ts

+22-1
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
import { clerkClient } from "@clerk/nextjs/server";
44
import { parseStringify } from "../utils";
5+
import { liveblocks } from "../liveblocks";
56

67
export const getClerkUsers = async ({ userIds } : { userIds: string[]}) => {
78
try {
@@ -18,8 +19,28 @@ export const getClerkUsers = async ({ userIds } : { userIds: string[]}) => {
1819

1920
const sortedUsers = userIds.map((email) => users.find((user) => user.email === email));
2021

21-
return parseStringify(sortedUsers);
22+
return parseStringify(sortedUsers);
2223
} catch (error) {
2324
console.log(`Error fetching users: ${error}`)
2425
}
26+
}
27+
28+
export const getDocumentUsers = async ({ roomId, currentUser, text } : {roomId: string, currentUser: string, text: string }) => {
29+
try {
30+
const room = await liveblocks.getRoom(roomId);
31+
32+
const users = Object.keys(room.usersAccesses).filter((email) => email !== currentUser);
33+
34+
if (text.length) {
35+
const lowerCaseText = text.toLocaleLowerCase();
36+
37+
const filteredUsers = users.filter((email: string) => email.toLowerCase().includes(lowerCaseText))
38+
39+
return parseStringify(filteredUsers);
40+
}
41+
42+
return parseStringify(users);
43+
} catch (error) {
44+
console.log(`Error fetching document users: ${error}`);
45+
}
2546
}

0 commit comments

Comments
 (0)
Please sign in to comment.