Skip to content
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
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ type CreateProjectInput = {
};
const state = vi.hoisted(() => ({
selectedEnvironment: "primary",
addedEnvironments: [] as string[],
sessions: new Map<string, unknown>(),
projects: null as unknown,
providers: null as unknown,
Expand Down Expand Up @@ -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) => {
Expand Down Expand Up @@ -232,6 +235,12 @@ async function click(label: string) {
await act(async () => target.click());
}

async function clickStage(label: string) {
const stage = document.querySelector<HTMLButtonElement>(`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();
Expand All @@ -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(
Expand Down Expand Up @@ -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");
Comment thread
coderabbitai[bot] marked this conversation as resolved.
Resolved

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<void>();
Expand Down
36 changes: 30 additions & 6 deletions apps/web/src/components/onboarding/WelcomeWizard.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,7 @@ export function WelcomeWizard({
const [setupIds, setSetupIds] = useState<readonly EnvironmentId[]>(
resumeEnvironmentId ? [resumeEnvironmentId] : [],
);
const [importSelection, setImportSelection] = useState<ImportSelection | null>(null);
const [isImporting, setIsImporting] = useState(false);
const finishingPromiseRef = useRef<Promise<boolean> | null>(null);
const completionErrorToastIdRef = useRef<ReturnType<typeof toastManager.add> | null>(null);
Expand Down Expand Up @@ -279,6 +280,8 @@ export function WelcomeWizard({
) : (
<ImportStep
scans={scans}
importSelection={importSelection}
onImportSelectionChange={setImportSelection}
isImporting={isImporting}
setIsImporting={setIsImporting}
onDone={finish}
Expand Down Expand Up @@ -1172,13 +1175,23 @@ function AgentInstallTerminal({

const IMPORT_PERMISSION_MESSAGE = "This connection cannot import projects or thread history.";

interface ImportSelection {
// Computers whose projects were listed when the selection changed; later ones get the defaults.
readonly environmentIds: ReadonlySet<EnvironmentId>;
readonly keys: ReadonlySet<string>;
}

function ImportStep({
scans,
importSelection,
onImportSelectionChange,
isImporting,
setIsImporting,
onDone,
}: {
readonly scans: ReturnType<typeof useProjectScans>;
readonly importSelection: ImportSelection | null;
readonly onImportSelectionChange: (next: ImportSelection) => void;
readonly isImporting: boolean;
readonly setIsImporting: (value: boolean) => void;
readonly onDone: (
Expand All @@ -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<ReadonlySet<string> | null>(null);
const importWarningRef = useRef("");
const importedThreadCountRef = useRef(0);
const importedProjectsRef = useRef(new Map<string, ScopedProjectRef>());
Expand Down Expand Up @@ -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<string>) =>
onImportSelectionChange({
environmentIds: new Set(
scans.flatMap((scan) => (scan.data === null ? [] : [scan.environmentId])),
),
keys,
});

const canImport = selected.every((candidate) =>
writableEnvironments.has(candidate.environmentId),
Expand Down Expand Up @@ -1430,15 +1454,15 @@ 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
</Button>
<Button
variant="ghost"
size="xs"
disabled={isImporting || selected.length === 0}
onClick={() => setSelectedPaths(new Set())}
onClick={() => setSelectedKeys(new Set())}
>
Select none
</Button>
Expand Down Expand Up @@ -1491,7 +1515,7 @@ function ImportStep({
<ImportCandidateList
candidates={scanCandidates}
selectedKeys={selectedKeys}
onSelectionChange={setSelectedPaths}
onSelectionChange={setSelectedKeys}
/>
</fieldset>
);
Expand Down
Loading