diff --git a/src/renderer/src/components/cloud-workspaces/cloud-server-name.test.ts b/src/renderer/src/components/cloud-workspaces/cloud-server-name.test.ts new file mode 100644 index 00000000000..0debf959873 --- /dev/null +++ b/src/renderer/src/components/cloud-workspaces/cloud-server-name.test.ts @@ -0,0 +1,26 @@ +import { describe, expect, it, vi } from 'vitest' + +vi.mock('@/i18n/i18n', () => ({ + translate: (_key: string, fallback: string, values?: Record) => + fallback.replace('{{value0}}', values?.value0 ?? '') +})) + +import { pickCloudServerName } from './cloud-server-name' + +describe('the server name a cloud workspace is paired under', () => { + it('a second workspace from the same repository gets its W-number instead of failing', () => { + expect(pickCloudServerName('xterm.js', 715, [])).toBe('xterm.js (cloud)') + expect(pickCloudServerName('xterm.js', 715, [{ name: 'xterm.js (cloud)' }])).toBe( + 'xterm.js W-715 (cloud)' + ) + expect( + pickCloudServerName('xterm.js', 715, [ + { name: 'XTERM.JS (cloud)' }, + { name: 'xterm.js W-715 (cloud)' } + ]) + ).toBe('xterm.js W-715 2 (cloud)') + expect(pickCloudServerName('xterm.js', null, [{ name: 'xterm.js (cloud)' }])).toBe( + 'xterm.js 2 (cloud)' + ) + }) +}) diff --git a/src/renderer/src/components/cloud-workspaces/cloud-server-name.ts b/src/renderer/src/components/cloud-workspaces/cloud-server-name.ts new file mode 100644 index 00000000000..98c4d39f986 --- /dev/null +++ b/src/renderer/src/components/cloud-workspaces/cloud-server-name.ts @@ -0,0 +1,38 @@ +import { translate } from '@/i18n/i18n' +import { formatWorkspaceNumber } from '../../../../shared/cloud-workspaces/cloud-workspaces-wire' +import { findCollidingEnvironmentName } from '../../../../shared/remote-runtime/host-catalog/runtime-environment-name-collision' + +/** + * The server name this computer saves for a cloud workspace. Server names are unique, and two + * workspaces from the same repository share a title, so the second one couldn't be paired ("Couldn't + * open the workspace on the sandbox"). A taken name gets the workspace's W-number, then a counter. + */ +export function pickCloudServerName( + base: string, + number: number | null, + existing: readonly { name: string }[] +): string { + const label = (value: string): string => + translate('auto.components.cloudWorkspaces.environmentName', '{{value0}} (cloud)', { + value0: value + }).slice(0, 120) + const free = (candidate: string): boolean => !findCollidingEnvironmentName(existing, candidate) + const first = label(base) + if (free(first)) { + return first + } + const numbered = number ? `${base} ${formatWorkspaceNumber(number)}` : base + // "W-715", then "W-715 2", "W-715 3"; without a number, "2", "3". + for (let n = number ? 1 : 2; n < 100; n++) { + const candidate = label(n === 1 ? numbered : `${numbered} ${n}`) + if (free(candidate)) { + return candidate + } + } + return label(`${numbered} ${Date.now()}`) +} + +export async function uniqueCloudServerName(base: string, number: number | null): Promise { + const environments = await window.api.runtimeEnvironments.list().catch(() => []) + return pickCloudServerName(base, number, environments) +} diff --git a/src/renderer/src/components/cloud-workspaces/open-cloud-workspace.ts b/src/renderer/src/components/cloud-workspaces/open-cloud-workspace.ts index 005773ddd6c..58899b31b87 100644 --- a/src/renderer/src/components/cloud-workspaces/open-cloud-workspace.ts +++ b/src/renderer/src/components/cloud-workspaces/open-cloud-workspace.ts @@ -1,6 +1,7 @@ // Opening a cloud workspace from the team list or a link. The owner already has it locally (the // hosted runtime they created); anyone else pairs this device through a per-device E2EE grant and // the workspace's server joins the sidebar like any remote Orca server. +import { uniqueCloudServerName } from './cloud-server-name' import { hasLocalSidebarRow } from './cloud-local-row' import { toast } from 'sonner' import { translate } from '@/i18n/i18n' @@ -242,9 +243,7 @@ async function pairAndReveal( } } const added = await window.api.runtimeEnvironments.verifyAndAddFromPairingCode({ - name: translate('auto.components.cloudWorkspaces.environmentName', '{{value0}} (cloud)', { - value0: name - }).slice(0, 120), + name: await uniqueCloudServerName(name, workspace.number ?? null), pairingCode: grant.value.pairingCode }) if (!added.ok) {