Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix duplicate collaborator entries in collaboration panel #422

Merged
merged 3 commits into from
Jan 9, 2025
Merged
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 10 additions & 4 deletions packages/collaboration/src/collaboratorspanel.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -97,18 +97,22 @@ export class CollaboratorsPanel extends Panel {
*/
private _onAwarenessChanged = () => {
const state = this._awareness.getStates() as any;
const collaborators: ICollaboratorAwareness[] = [];
const collaboratorsMap = new Map<string, ICollaboratorAwareness>();

state.forEach((value: Partial<ICollaboratorAwareness>, key: any) => {
if (
this._currentUser.isReady &&
value.user &&
value.user.username !== this._currentUser.identity!.username
) {
collaborators.push(value as ICollaboratorAwareness);
const uniqueKey = `${value.user.username}-${value.current || 'no-current'}`;
if (!collaboratorsMap.has(uniqueKey)) {
collaboratorsMap.set(uniqueKey, value as ICollaboratorAwareness);
}
}
});
this._collaboratorsChanged.emit(collaborators);
// Convert map to array to maintain the same emit interface
this._collaboratorsChanged.emit(Array.from(collaboratorsMap.values()));
};
private _currentUser: User.IManager;
private _awareness: Awareness;
Expand All @@ -132,9 +136,11 @@ export function CollaboratorsBody(props: {

return (
<div className={COLLABORATORS_LIST_CLASS}>
{collaborators.map((collaborator, i) => {
{collaborators.map((collaborator) => {
const uniqueKey = `${collaborator.user.username}-${collaborator.current || 'no-current'}`;
return (
<Collaborator
key={uniqueKey}
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is it used?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I believe it is needed for React to know which entry is which.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, and It's the key that can avoid the warning "Warning: Each child in a list should have a unique "key" prop."

collaborator={collaborator}
fileopener={props.fileopener}
docRegistry={props.docRegistry}
Expand Down
Loading