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

Avoid follow mode loops #4515

Merged
merged 6 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
2 changes: 2 additions & 0 deletions editor/liveblocks.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,13 +19,15 @@ export type Presence = {
cursor: WindowPoint | null
canvasScale: number | null
canvasOffset: CanvasVector | null
following: string | null
}

export function initialPresence(): Presence {
return {
cursor: null,
canvasScale: null,
canvasOffset: null,
following: null,
}
}

Expand Down
14 changes: 11 additions & 3 deletions editor/src/components/canvas/multiplayer-cursors.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -46,16 +46,24 @@ export const MultiplayerPresence = React.memo(() => {
(store) => store.editor.canvas.roundedCanvasOffset,
'MultiplayerPresence canvasOffset',
)
const mode = useEditorState(
Substores.restOfEditor,
(store) => store.editor.mode,
'MultiplayerPresence mode',
)

useAddMyselfToCollaborators()

React.useEffect(() => {
if (!isLoggedIn(loginState)) {
return
}
// when the canvas is panned or zoomed, update the presence
updateMyPresence({ canvasScale, canvasOffset })
}, [canvasScale, canvasOffset, updateMyPresence, loginState])
updateMyPresence({
canvasScale,
canvasOffset,
following: isFollowMode(mode) ? mode.playerId : null,
})
}, [canvasScale, canvasOffset, updateMyPresence, loginState, mode])

React.useEffect(() => {
// when the mouse moves over the canvas, update the presence cursor
Expand Down
41 changes: 33 additions & 8 deletions editor/src/components/user-bar.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { useOthers, useSelf, useStatus, useStorage } from '../../liveblocks.conf
import { getUserPicture, isLoggedIn } from '../common/user'
import type { MultiplayerColor } from '../core/shared/multiplayer'
import {
canFollowTarget,
isDefaultAuth0AvatarURL,
multiplayerColorFromIndex,
multiplayerInitialsFromName,
Expand All @@ -14,13 +15,16 @@ import { Substores, useEditorState } from './editor/store/store-hook'
import { unless, when } from '../utils/react-conditionals'
import { MultiplayerWrapper } from '../utils/multiplayer-wrapper'
import { useDispatch } from './editor/store/dispatch-context'
import { switchEditorMode } from './editor/actions/action-creators'
import { showToast, switchEditorMode } from './editor/actions/action-creators'
import type { EditorAction } from './editor/action-types'
import { EditorModes, isFollowMode } from './editor/editor-modes'
import { notice } from './common/notice'
import { useMyUserAndPresence } from '../core/commenting/comment-hooks'

const MAX_VISIBLE_OTHER_PLAYERS = 4

export const cannotFollowToastId = 'cannot-follow-toast-id'

export const UserBar = React.memo(() => {
const loginState = useEditorState(
Substores.userState,
Expand Down Expand Up @@ -71,6 +75,7 @@ const MultiplayerUserBar = React.memo(() => {
name: myUser.name,
colorIndex: myUser.colorIndex,
picture: myUser.avatar,
following: other.presence.following,
})),
)

Expand All @@ -88,15 +93,35 @@ const MultiplayerUserBar = React.memo(() => {
)

const toggleFollowing = React.useCallback(
(id: string) => () => {
const newMode =
isFollowMode(mode) && mode.playerId === id
? EditorModes.selectMode(null, false, 'none')
: EditorModes.followMode(id)
let actions: EditorAction[] = [switchEditorMode(newMode)]
(targetId: string) => () => {
let actions: EditorAction[] = []
if (
!canFollowTarget(
myUser.id,
targetId,
others.map((o) => o),
)
) {
actions.push(
showToast(
notice(
'Cannot follow this player at the moment.',
'WARNING',
false,
cannotFollowToastId,
),
),
)
} else {
const newMode =
isFollowMode(mode) && mode.playerId === targetId
? EditorModes.selectMode(null, false, 'none')
: EditorModes.followMode(targetId)
actions.push(switchEditorMode(newMode))
}
dispatch(actions)
},
[dispatch, mode],
[dispatch, mode, myUser, others],
)

if (myUser.name == null) {
Expand Down
37 changes: 36 additions & 1 deletion editor/src/core/shared/multiplayer.spec.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { multiplayerInitialsFromName } from './multiplayer'
import { canFollowTarget, multiplayerInitialsFromName } from './multiplayer'

describe('multiplayer', () => {
describe('multiplayerInitialsFromName', () => {
Expand All @@ -18,4 +18,39 @@ describe('multiplayer', () => {
expect(multiplayerInitialsFromName('')).toEqual('XX')
})
})

describe('canFollowTarget', () => {
it('can follow a single player', () => {
expect(canFollowTarget('foo', 'bar', [{ id: 'bar', following: null }])).toBe(true)
})
it('can follow a player that follows another player', () => {
expect(
canFollowTarget('foo', 'bar', [
{ id: 'bar', following: 'baz' },
{ id: 'baz', following: null },
]),
).toBe(true)
})
it('can follow a player that follows another player indirectly', () => {
expect(
canFollowTarget('foo', 'bar', [
{ id: 'bar', following: 'baz' },
{ id: 'baz', following: 'qux' },
{ id: 'qux', following: null },
]),
).toBe(true)
})
it('cannot follow a player back', () => {
expect(canFollowTarget('foo', 'bar', [{ id: 'bar', following: 'foo' }])).toBe(false)
})
it('cannot follow a player that has an indirect loop', () => {
expect(
canFollowTarget('foo', 'bar', [
{ id: 'bar', following: 'baz' },
{ id: 'baz', following: 'qux' },
{ id: 'qux', following: 'foo' },
]),
).toBe(false)
})
})
})
21 changes: 21 additions & 0 deletions editor/src/core/shared/multiplayer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,27 @@ export function isDefaultAuth0AvatarURL(s: string | null): boolean {
)
}

export function canFollowTarget(
selfId: string,
targetId: string | null,
others: { id: string; following: string | null }[],
): boolean {
let followChain: Set<string> = new Set()

let id = targetId
while (id != null) {
if (followChain.has(id)) {
return false
}
followChain.add(id)

const target = others.find((o) => o.id === id)
id = target?.following ?? null
}

return !followChain.has(selfId)
}

export function projectIdToRoomId(projectId: string): string {
return `project-room-${projectId}`
}
Loading