Skip to content

Commit

Permalink
Make comment mentions work (#4531)
Browse files Browse the repository at this point in the history
  • Loading branch information
gbalint authored Nov 21, 2023
1 parent 49308fd commit e20f7fe
Showing 1 changed file with 32 additions and 28 deletions.
60 changes: 32 additions & 28 deletions editor/liveblocks.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -134,42 +134,46 @@ export const {
return []
}

const room = liveblocksClient.getRoom(projectIdToRoomId(projectId))
if (room == null) {
return []
}

const storage = await room.getStorage()

const collabs = storage.root.get('collaborators') as LiveObject<{ [userId: string]: User }>
if (collabs == null) {
return []
}

const users = Object.values(collabs.toObject()).map((u) => u.toObject())
const users = await getAllUsersFromRoom(projectIdToRoomId(projectId))
return users.filter((u) => userIds.includes(u.id))
},
async resolveMentionSuggestions({ text, roomId }) {
// Used only for Comments. Return a list of userIds that match `text`.
// Used only for Comments. Return a list of userIds where the name matches `text`.
// These userIds are used to create a mention list when typing in the
// composer.
//
// For example when you type "@jo", `text` will be `"jo"`, and
// you should to return an array with John and Joanna's userIds:
// ["[email protected]", "[email protected]"]
// you should to return an array with John and Joanna's userIds.

// const userIds = await __fetchAllUserIdsFromDB__(roomId);
//
// Return all userIds if no `text`
// if (!text) {
// return userIds;
// }
//
// Otherwise, filter userIds for the search `text` and return
// return userIds.filter((userId) =>
// userId.toLowerCase().includes(text.toLowerCase())
// );
const users = await getAllUsersFromRoom(roomId)

return []
if (text == null) {
return users.map((u) => u.id)
}

// Otherwise, filter user names for the search `text` and return
return users
.filter((u) => {
if (u.name == null) {
return false
}
return u.name.toLowerCase().includes(text.toLowerCase())
})
.map((u) => u.id)
},
})

async function getAllUsersFromRoom(roomId: string) {
const room = liveblocksClient.getRoom(roomId)
if (room == null) {
return []
}

const storage = await room.getStorage()

const collabs = storage.root.get('collaborators') as LiveObject<{ [userId: string]: User }>
if (collabs == null) {
return []
}
return Object.values(collabs.toObject()).map((u) => u.toObject())
}

0 comments on commit e20f7fe

Please sign in to comment.