diff --git a/apps/web/src/components/onboarding/WelcomeWizard.import.browser.test.tsx b/apps/web/src/components/onboarding/WelcomeWizard.import.browser.test.tsx index 5f4bc3fbb69e7..c55691d9ca5cc 100644 --- a/apps/web/src/components/onboarding/WelcomeWizard.import.browser.test.tsx +++ b/apps/web/src/components/onboarding/WelcomeWizard.import.browser.test.tsx @@ -22,6 +22,7 @@ type CreateProjectInput = { }; const state = vi.hoisted(() => ({ selectedEnvironment: "primary", + addedEnvironments: [] as string[], sessions: new Map(), projects: null as unknown, providers: null as unknown, @@ -67,7 +68,9 @@ vi.mock(import("../../state/environments"), async (original) => { return { ...(await original()), usePrimaryEnvironment: (() => primary) as never, - useEnvironments: (() => ({ environments: [environment(state.selectedEnvironment)] })) as never, + useEnvironments: (() => ({ + environments: [state.selectedEnvironment, ...state.addedEnvironments].map(environment), + })) as never, }; }); vi.mock(import("../../state/server"), async (original) => { @@ -232,6 +235,12 @@ async function click(label: string) { await act(async () => target.click()); } +async function clickStage(label: string) { + const stage = document.querySelector(`button[aria-label="${label}"]`); + expect(stage?.disabled).toBe(false); + await act(async () => stage?.click()); +} + async function mountImport(remote = false) { state.selectedEnvironment = remote ? "remote" : "primary"; await using stack = new AsyncDisposableStack(); @@ -253,6 +262,7 @@ async function mountImport(remote = false) { // Returns the clock spy so each test can `using` it; the spy would otherwise outlive the test. function setup() { const now = vi.spyOn(Date, "now").mockReturnValue(NOW); + state.addedEnvironments = []; state.sessions.clear(); for (const id of [primaryId, remoteId]) { state.sessions.set( @@ -282,6 +292,34 @@ function setup() { return now; } +it("keeps the project selection after going back a stage", async () => { + using _now = setup(); + await using _screen = await mountImport(); + await click("Select none"); + expect(pageText()).toContain("0 of 2 selected"); + + await clickStage("Agents, step 2"); + await click("Continue"); + expect(pageText()).toContain("Import your projects"); + expect(pageText()).toContain("0 of 2 selected"); +}); + +it("ticks the recent projects of a computer added after the selection changed", async () => { + using _now = setup(); + await using _screen = await mountImport(); + await click("Select none"); + + state.addedEnvironments = ["remote"]; + await clickStage("Connect, step 1"); + await click("Continue"); + await click("Continue"); + expect(pageText()).toContain("2 of 4 selected"); + + await click("Import 2 projects"); + const created = state.createProject.mock.calls as Array<[CreateProjectInput]>; + expect(created.map(([call]) => call.environmentId)).toEqual([remoteId, remoteId]); +}); + it("rechecks after project creation and can retry its history without recreating the project", async () => { using _now = setup(); const creation = deferred(); diff --git a/apps/web/src/components/onboarding/WelcomeWizard.tsx b/apps/web/src/components/onboarding/WelcomeWizard.tsx index 50d65192c0249..682b9c6cb7b27 100644 --- a/apps/web/src/components/onboarding/WelcomeWizard.tsx +++ b/apps/web/src/components/onboarding/WelcomeWizard.tsx @@ -124,6 +124,7 @@ export function WelcomeWizard({ const [setupIds, setSetupIds] = useState( resumeEnvironmentId ? [resumeEnvironmentId] : [], ); + const [importSelection, setImportSelection] = useState(null); const [isImporting, setIsImporting] = useState(false); const finishingPromiseRef = useRef | null>(null); const completionErrorToastIdRef = useRef | null>(null); @@ -279,6 +280,8 @@ export function WelcomeWizard({ ) : ( ; + readonly keys: ReadonlySet; +} + function ImportStep({ scans, + importSelection, + onImportSelectionChange, isImporting, setIsImporting, onDone, }: { readonly scans: ReturnType; + readonly importSelection: ImportSelection | null; + readonly onImportSelectionChange: (next: ImportSelection) => void; readonly isImporting: boolean; readonly setIsImporting: (value: boolean) => void; readonly onDone: ( @@ -1191,7 +1204,6 @@ function ImportStep({ const writableEnvironments = useEnvironmentsWithScope(scans, AuthOrchestrationOperateScope); const createProject = useAtomCommand(projectEnvironment.create, { reportFailure: false }); const importThreads = useAtomCommand(agentSessionImport, { reportFailure: false }); - const [selectedPaths, setSelectedPaths] = useState | null>(null); const importWarningRef = useRef(""); const importedThreadCountRef = useRef(0); const importedProjectsRef = useRef(new Map()); @@ -1219,8 +1231,20 @@ function ImportStep({ })), ), ); - const selectedKeys = selectedPaths ?? new Set(recent.map((candidate) => candidate.key)); - const selected = candidates.filter((candidate) => selectedKeys.has(candidate.key)); + const recentKeys = new Set(recent.map((candidate) => candidate.key)); + const selected = candidates.filter((candidate) => + importSelection?.environmentIds.has(candidate.environmentId) + ? importSelection.keys.has(candidate.key) + : recentKeys.has(candidate.key), + ); + const selectedKeys = new Set(selected.map((candidate) => candidate.key)); + const setSelectedKeys = (keys: ReadonlySet) => + onImportSelectionChange({ + environmentIds: new Set( + scans.flatMap((scan) => (scan.data === null ? [] : [scan.environmentId])), + ), + keys, + }); const canImport = selected.every((candidate) => writableEnvironments.has(candidate.environmentId), @@ -1430,7 +1454,7 @@ function ImportStep({ variant="ghost" size="xs" disabled={isImporting || selected.length === candidates.length} - onClick={() => setSelectedPaths(new Set(candidates.map((item) => item.key)))} + onClick={() => setSelectedKeys(new Set(candidates.map((item) => item.key)))} > Select all @@ -1438,7 +1462,7 @@ function ImportStep({ variant="ghost" size="xs" disabled={isImporting || selected.length === 0} - onClick={() => setSelectedPaths(new Set())} + onClick={() => setSelectedKeys(new Set())} > Select none @@ -1491,7 +1515,7 @@ function ImportStep({ );