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 loading collaborators from storage #4522

Merged
merged 2 commits into from
Nov 21, 2023
Merged
Show file tree
Hide file tree
Changes from all 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
4 changes: 2 additions & 2 deletions editor/src/components/canvas/multiplayer-cursors.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ import { useDispatch } from '../editor/store/dispatch-context'
import { Substores, useEditorState } from '../editor/store/store-hook'
import CanvasActions from './canvas-actions'
import { canvasPointToWindowPoint, windowToCanvasCoordinates } from './dom-lookup'
import { useAddMyselfToCollaborators } from '../../core/commenting/comment-hooks'
import { getCollaborator, useAddMyselfToCollaborators } from '../../core/commenting/comment-hooks'

export const MultiplayerPresence = React.memo(() => {
const dispatch = useDispatch()
Expand Down Expand Up @@ -103,7 +103,7 @@ const MultiplayerCursors = React.memo(() => {
const presences = normalizeOthersList(me.id, list)
return presences.map((p) => ({
presenceInfo: p,
userInfo: collabs[p.id],
userInfo: getCollaborator(collabs, p),
}))
})

Expand Down
22 changes: 11 additions & 11 deletions editor/src/components/user-bar.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import React from 'react'
import { useOthers, useSelf, useStatus, useStorage } from '../../liveblocks.config'
import { useOthers, useStatus, useStorage } from '../../liveblocks.config'
import { getUserPicture, isLoggedIn } from '../common/user'
import type { MultiplayerColor } from '../core/shared/multiplayer'
import {
Expand All @@ -18,8 +18,8 @@ import { useDispatch } from './editor/store/dispatch-context'
import { showToast, switchEditorMode } from './editor/actions/action-creators'
import type { EditorAction } from './editor/action-types'
import { EditorModes, isFollowMode } from './editor/editor-modes'
import { getCollaborator, useMyUserAndPresence } from '../core/commenting/comment-hooks'
import { notice } from './common/notice'
import { useMyUserAndPresence } from '../core/commenting/comment-hooks'

const MAX_VISIBLE_OTHER_PLAYERS = 4

Expand Down Expand Up @@ -65,18 +65,18 @@ SinglePlayerUserBar.displayName = 'SinglePlayerUserBar'
const MultiplayerUserBar = React.memo(() => {
const dispatch = useDispatch()
const colorTheme = useColorTheme()
const collabs = useStorage((store) => store.collaborators)

const { user: myUser } = useMyUserAndPresence()
const myName = normalizeMultiplayerName(myUser.name)
const myName = React.useMemo(() => normalizeMultiplayerName(myUser.name), [myUser])

const others = useOthers((list) =>
normalizeOthersList(myUser.id, list).map((other) => ({
id: other.id,
name: myUser.name,
colorIndex: myUser.colorIndex,
picture: myUser.avatar,
following: other.presence.following,
})),
normalizeOthersList(myUser.id, list).map((other) => {
return {
...getCollaborator(collabs, other),
following: other.presence.following,
}
}),
)

const visibleOthers = React.useMemo(() => {
Expand Down Expand Up @@ -161,7 +161,7 @@ const MultiplayerUserBar = React.memo(() => {
name={multiplayerInitialsFromName(name)}
tooltip={name}
color={multiplayerColorFromIndex(other.colorIndex)}
picture={other.picture}
picture={other.avatar}
border={true}
coloredTooltip={true}
onClick={toggleFollowing(other.id)}
Expand Down
35 changes: 30 additions & 5 deletions editor/src/core/commenting/comment-hooks.tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import React from 'react'
import type { User } from '@liveblocks/client'
import { LiveObject, type ThreadData } from '@liveblocks/client'
import type { ThreadMetadata } from '../../../liveblocks.config'
import { useMutation, useRoom, useSelf, useStorage, useThreads } from '../../../liveblocks.config'
import type { Presence, ThreadMetadata, UserMeta } from '../../../liveblocks.config'
import { useMutation, useSelf, useStorage, useThreads } from '../../../liveblocks.config'
import { Substores, useEditorState } from '../../components/editor/store/store-hook'
import { normalizeMultiplayerName, possiblyUniqueColor } from '../shared/multiplayer'
import { isLoggedIn } from '../../common/user'
Expand All @@ -12,12 +13,36 @@ export function useCanvasCommentThread(x: number, y: number): ThreadData<ThreadM
return thread
}

export function useMyUserAndPresence() {
function placeholderUserMeta(user: User<Presence, UserMeta>): UserMeta {
return {
id: user.id,
name: null,
avatar: null,
colorIndex: null,
}
}

interface Collaborators {
[key: string]: UserMeta
}

export function getCollaborator(
collabs: Collaborators,
source: User<Presence, UserMeta>,
): UserMeta {
return collabs[source.id] ?? placeholderUserMeta(source)
}

export function useMyUserAndPresence(): {
presence: User<Presence, UserMeta>
user: UserMeta
} {
const me = useSelf()
const myUser = useStorage((store) => store.collaborators[me.id])
const collabs = useStorage((store) => store.collaborators)
const myUser: UserMeta | null = getCollaborator(collabs, me)
return {
presence: me,
user: myUser,
user: myUser ?? placeholderUserMeta(me),
}
}

Expand Down