AI-native design editor. Open-source Figma alternative.
fix(cloud): stop offering to leave a Cloud document's room (#1308)
Danila Poyarkovb402ba085e46
diff --git a/src/app/collab/rooms.ts b/src/app/collab/rooms.tsindex e6f4f3a48..2e5ccceaf 100644--- a/src/app/collab/rooms.ts+++ b/src/app/collab/rooms.ts@@ -67,13 +67,16 @@ export function openDocumentRoom(identity?: RoomIdentity): RoomSession {const existing = roomForStore(store)- if (existing) return existing+ if (existing?.roomId === roomId) return existing+ // A room the document was shared into by hand gives way to its own; leaving keeps the document.+ if (existing) leaveRoom(existing)const session = openRoomSession({roomId,store,origin: 'shared',joinRoom: transport,holdsDocument: true,+ leavable: false,identity})addSession(session)diff --git a/src/app/collab/session.ts b/src/app/collab/session.tsindex 601ae0685..cbbb25895 100644--- a/src/app/collab/session.ts+++ b/src/app/collab/session.ts@@ -51,6 +51,8 @@ export interface RoomSession {readonly voice: RoomVoice/** Who this tab is in the room. */readonly identity: RoomIdentity+ /** Whether the person may leave; a stored document's room stays open while its tab does. */+ readonly leavable: booleanupdateCursor(x: number, y: number, pageId: string): voidupdateSelection(ids: string[]): void/** Writes the tab's whole document into the room and makes its root the room's. */@@ -96,6 +98,8 @@ export interface RoomSessionOptions {* it counts as having the document until the room says it holds another.*/holdsDocument?: boolean+ /** False for a room the tab's document lives in, which the person never leaves by hand. */+ leavable?: boolean}function openIndexedDBCopy(roomId: string, ydoc: Y.Doc): RoomSavedCopy {@@ -113,7 +117,8 @@ export function openRoomSession({openSavedCopy = openIndexedDBCopy,unreachableMs = ROOM_UNREACHABLE_MS,tickMs = ROOM_STATUS_TICK_MS,- holdsDocument = false+ holdsDocument = false,+ leavable = true}: RoomSessionOptions): RoomSession {const ydoc = new Y.Doc()const awareness = new awarenessProtocol.Awareness(ydoc)@@ -264,6 +269,7 @@ export function openRoomSession({hasDocument,voice,identity,+ leavable,updateCursor(x, y, pageId) {awareness.setLocalStateField('cursor', { x, y, pageId, zoom: store.state.zoom })},diff --git a/src/app/collab/types.ts b/src/app/collab/types.tsindex 93414ea9b..4c685ce65 100644--- a/src/app/collab/types.ts+++ b/src/app/collab/types.ts@@ -28,6 +28,8 @@ export interface VoiceMembership {export interface CollabState {/** Whether the active tab is in a room. */inRoom: boolean+ /** Whether the person may leave it, which a stored document's room never offers. */+ canLeave: booleanroomId: string | nullstatus: RoomStatus | nullpeers: RemotePeer[]@@ -39,6 +41,7 @@ export interface CollabState {export const DEFAULT_COLLAB_STATE: CollabState = {inRoom: false,+ canLeave: false,roomId: null,status: null,peers: [],diff --git a/src/app/collab/use.ts b/src/app/collab/use.tsindex 4173d7250..c53224cd7 100644--- a/src/app/collab/use.ts+++ b/src/app/collab/use.ts@@ -31,6 +31,7 @@ export function useCollab() {const room = activeRoom.valuereturn {inRoom: room !== undefined,+ canLeave: room?.leavable ?? false,roomId: room?.roomId ?? null,status: room?.status.value ?? null,peers: room?.peers.value ?? [],diff --git a/src/components/MobileHud/MobilePresencePopover.vue b/src/components/MobileHud/MobilePresencePopover.vueindex 057eee5ba..853fe29fd 100644--- a/src/components/MobileHud/MobilePresencePopover.vue+++ b/src/components/MobileHud/MobilePresencePopover.vue@@ -40,7 +40,12 @@ const styles = presencePopover()@follow="hud.follow"@rename="hud.renameAgent"/>- <AppButton variant="outline" class="mt-3 w-full" @click="hud.disconnect">+ <AppButton+ v-if="hud.collabState.canLeave"+ variant="outline"+ class="mt-3 w-full"+ @click="hud.disconnect"+ >{{ hud.messages.disconnect }}</AppButton></PopoverContent>diff --git a/src/components/collab-panel/CollabAvatarStack.vue b/src/components/collab-panel/CollabAvatarStack.vueindex 32dc65cfd..6dcd1c6f5 100644--- a/src/components/collab-panel/CollabAvatarStack.vue+++ b/src/components/collab-panel/CollabAvatarStack.vue@@ -11,7 +11,7 @@ const collab = useCollabPanelContext():rows="collab.presenceRows":following="collab.following":connected="collab.state.status === 'live'"- :in-room="collab.state.inRoom"+ :can-leave="collab.state.canLeave"@follow="collab.follow"@rename="collab.renameLocalAgent"@leave="collab.disconnect"diff --git a/src/components/collab-panel/PresenceAvatars.stories.ts b/src/components/collab-panel/PresenceAvatars.stories.tsindex 5c576a749..ed51fa2da 100644--- a/src/components/collab-panel/PresenceAvatars.stories.ts+++ b/src/components/collab-panel/PresenceAvatars.stories.ts@@ -18,7 +18,7 @@ type Args = {rows: PresencePersonRow[]following: FollowTarget | nullconnected: boolean- inRoom: boolean+ canLeave: boolean}const meta = {@@ -29,7 +29,7 @@ const meta = {rows: room,following: null,connected: true,- inRoom: true,+ canLeave: true,onFollow: fn(),onRename: fn(),onLeave: fn()@@ -66,7 +66,7 @@ export const ManyPeople: Story = {}export const Alone: Story = {- args: { rows: room.slice(0, 1), connected: false, inRoom: false }+ args: { rows: room.slice(0, 1), connected: false, canLeave: false }}/** You and Ana are in the voice call: Ana is speaking and you are muted. */diff --git a/src/components/collab-panel/PresenceAvatars.vue b/src/components/collab-panel/PresenceAvatars.vueindex 00a3f09a9..fed63f986 100644--- a/src/components/collab-panel/PresenceAvatars.vue+++ b/src/components/collab-panel/PresenceAvatars.vue@@ -27,15 +27,15 @@ const {rows,following,connected = false,- inRoom = false+ canLeave = false} = defineProps<{/** Ourselves first, then everyone else in the room. */rows: PresencePersonRow[]following: FollowTarget | null/** Whether someone else in the room answered, so the room is live. */connected?: boolean- /** Whether we are in a room at all, so we can leave it. */- inRoom?: boolean+ /** Whether the self menu offers leaving the room, which a stored document's room never does. */+ canLeave?: boolean}>()const emit = defineEmits<{@@ -98,7 +98,7 @@ function toggleFollow(clientId: number) {@rename="(id, name) => emit('rename', id, name)"/><AppButton- v-if="inRoom"+ v-if="canLeave"variant="outline":class="ui.leave()"data-test-id="collab-leave-room"diff --git a/tests/app/collab/session.test.ts b/tests/app/collab/session.test.tsindex 4f24eb9b4..0feb8eb7a 100644--- a/tests/app/collab/session.test.ts+++ b/tests/app/collab/session.test.ts@@ -143,6 +143,14 @@ describe('room sessions', () => {})})+ test('a room someone shared or joined can be left', async () => {+ await withRooms(async (open) => {+ const shared = open({ roomId: ROOM_A, store: newStore('Poster'), origin: 'shared' })+ const joined = open({ roomId: ROOM_A, store: newStore(), origin: 'joined' })+ expect([shared.leavable, joined.leavable]).toEqual([true, true])+ })+ })+test('a room a tab has left no longer changes that tab or its people', async () => {await withRooms(async (open, settle) => {const host = newStore('Shared')diff --git a/tests/integration/collab/cloud-relay.test.ts b/tests/integration/collab/cloud-relay.test.tsindex 3f1ca2d65..0ac872afd 100644--- a/tests/integration/collab/cloud-relay.test.ts+++ b/tests/integration/collab/cloud-relay.test.ts@@ -14,6 +14,7 @@ import { openCloudDocumentRoom } from '@/app/cloud/rooms/live'import { versionListRevision } from '@/app/cloud/versions/history'import { useCollabIdentity } from '@/app/collab/identity'import { connectCollabRoom } from '@/app/collab/room/connection'+import { openDocumentRoom, roomById, roomForStore } from '@/app/collab/rooms'import { createEditorStore } from '@/app/editor/session'import { startTestRelay } from '#tests/helpers/collab/relay-server'@@ -281,6 +282,32 @@ describe('Cloud document rooms, version history', () => {})})+describe('Cloud document rooms, another room first', () => {+ test('a tab already in another room moves to its document’s own room', async () => {+ const relay = await startRelay()+ const documentId = crypto.randomUUID()+ const [grace] = await openedTwice()+ // Shared into a room by hand while the document's own was still being asked for.+ const elsewhere = openDocumentRoom(+ grace,+ `cloud:${crypto.randomUUID()}:0`,+ relay.join(documentId, 'Grace', 'edit', 'user')+ )+ const room = openCloudDocumentRoom(grace, {+ roomId: `cloud:${documentId}:0`,+ transport: relay.join(documentId, 'Grace', 'edit', 'user'),+ permission: 'edit'+ })+ cleanups.push(() => {+ room.stop()+ room.session.dispose()+ })+ expect(room.session).not.toBe(elsewhere)+ expect(roomForStore(grace)?.roomId).toBe(`cloud:${documentId}:0`)+ expect(roomById(elsewhere.roomId)).toBeUndefined()+ })+})+/** Tabs that each opened the same stored file, which gives each its own root. */async function openedTwice() {await initCodec()@@ -329,6 +356,8 @@ describe('Cloud document rooms', () => {await until(() => grace.graph.getNode('from:ada') !== undefined)await until(() => first.session.peers.value.length > 0)expect(compact([grace.state.autosaveEnabled, ada.state.autosaveEnabled])).toHaveLength(1)+ // The document lives in its room, so neither tab offers leaving it.+ expect([first.session.leavable, second.session.leavable]).toEqual([false, false])})test('a tab that does not save has the saver upload its latest edit', async () => {