Skip to content

Commit 66a7621

Browse files
perf(web): welcome wizard keeps its auto-selected computers in lazy state
Co-Authored-By: Claude Opus 5.5 <[email protected]>
1 parent a91e9d8 commit 66a7621

2 files changed

Lines changed: 79 additions & 81 deletions

File tree

‎apps/web/reactCompiledFiles.ts‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,4 +44,5 @@ export const REACT_COMPILED_FILES = [
4444
"apps/web/src/components/desktop/SnapShotCoordinator.tsx",
4545
"apps/web/src/components/BranchToolbar.tsx",
4646
"apps/web/src/components/DiffPanel.tsx",
47+
"apps/web/src/components/onboarding/WelcomeWizard.tsx",
4748
] as const;

‎apps/web/src/components/onboarding/WelcomeWizard.tsx‎

Lines changed: 78 additions & 81 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ import {
3232
MonitorIcon,
3333
TerminalIcon,
3434
} from "lucide-react";
35-
import { useCallback, useEffect, useMemo, useRef, useState } from "react";
35+
import { useEffect, useRef, useState } from "react";
3636

3737
import { TYPOGRAPHY_ADVANCED_STORAGE_KEY } from "../../appearanceFonts";
3838
import { useLocalStorage } from "../../hooks/useLocalStorage";
@@ -120,7 +120,9 @@ export function WelcomeWizard({
120120
const [step, setStep] = useState<WizardStep>(resumeEnvironmentId ? "agents" : "connection");
121121
const { environments } = useEnvironments();
122122
const [selection, setSelection] = useState<ReadonlySet<EnvironmentId> | null>(null);
123-
const autoSelectedComputers = useRef(new Set<EnvironmentId>());
123+
// One registry for the wizard's lifetime, shared with the connect list, so a computer is
124+
// auto-selected once even after the user unchecks it.
125+
const [autoSelectedComputers] = useState(() => new Set<EnvironmentId>());
124126
const [setupIds, setSetupIds] = useState<readonly EnvironmentId[]>(
125127
resumeEnvironmentId ? [resumeEnvironmentId] : [],
126128
);
@@ -130,22 +132,23 @@ export function WelcomeWizard({
130132
const primaryEnvironment = usePrimaryEnvironment();
131133
useEffect(() => {
132134
const newComputers = environments.filter(
133-
(environment) => !autoSelectedComputers.current.has(environment.environmentId),
135+
(environment) => !autoSelectedComputers.has(environment.environmentId),
134136
);
135137
if (newComputers.length === 0) return;
136138
for (const environment of newComputers) {
137-
autoSelectedComputers.current.add(environment.environmentId);
139+
autoSelectedComputers.add(environment.environmentId);
138140
}
139141
const enabledComputers = newComputers.filter((environment) => environment.entry.enabled);
140142
if (enabledComputers.length === 0) return;
143+
// oxlint-disable-next-line react/set-state-in-effect -- The registry marks each computer once, and only after commit; render must not mutate it.
141144
setSelection(
142145
(current) =>
143146
new Set([
144147
...(current ?? []),
145148
...enabledComputers.map((environment) => environment.environmentId),
146149
]),
147150
);
148-
}, [environments]);
151+
}, [autoSelectedComputers, environments]);
149152
const selectedIds =
150153
selection ?? new Set(primaryEnvironment ? [primaryEnvironment.environmentId] : []);
151154
const scans = useProjectScans(step === "import" ? setupIds : NO_ENVIRONMENTS);
@@ -159,59 +162,60 @@ export function WelcomeWizard({
159162
setStep("agents");
160163
};
161164
const stageIndex = step === "agents" ? 1 : step === "import" ? 2 : 0;
162-
const finish = useCallback(
163-
(projectRef?: ScopedProjectRef, importWarning?: string, importedThreadCount = 0) => {
164-
if (finishingPromiseRef.current !== null) return finishingPromiseRef.current;
165-
if (completionErrorToastIdRef.current !== null) {
166-
toastManager.close(completionErrorToastIdRef.current);
167-
completionErrorToastIdRef.current = null;
168-
}
165+
const finish = (
166+
projectRef?: ScopedProjectRef,
167+
importWarning?: string,
168+
importedThreadCount = 0,
169+
) => {
170+
if (finishingPromiseRef.current !== null) return finishingPromiseRef.current;
171+
if (completionErrorToastIdRef.current !== null) {
172+
toastManager.close(completionErrorToastIdRef.current);
173+
completionErrorToastIdRef.current = null;
174+
}
169175

170-
const completion = completeOnboarding()
171-
.then(async () => {
172-
if (completionErrorToastIdRef.current !== null) {
173-
toastManager.close(completionErrorToastIdRef.current);
174-
completionErrorToastIdRef.current = null;
175-
}
176-
await onDone(projectRef);
177-
if (importWarning) {
178-
toastManager.add({
179-
type: "warning",
180-
title: "Some history was not imported",
181-
description: importWarning,
182-
timeout: 0,
183-
});
184-
} else if (importedThreadCount > 0) {
185-
toastManager.add({
186-
type: "success",
187-
title: `Imported ${importedThreadCount} ${importedThreadCount === 1 ? "thread" : "threads"}`,
188-
});
189-
}
190-
return true;
191-
})
192-
.catch(() => {
193-
const errorToast = {
194-
type: "error",
195-
title: "Could not finish setup",
196-
description: "Your settings could not be saved. Try again.",
197-
} as const;
198-
if (completionErrorToastIdRef.current === null) {
199-
completionErrorToastIdRef.current = toastManager.add(errorToast);
200-
} else {
201-
toastManager.update(completionErrorToastIdRef.current, errorToast);
202-
}
203-
return false;
204-
})
205-
.finally(() => {
206-
if (finishingPromiseRef.current === completion) {
207-
finishingPromiseRef.current = null;
208-
}
209-
});
210-
finishingPromiseRef.current = completion;
211-
return completion;
212-
},
213-
[completeOnboarding, onDone],
214-
);
176+
const completion = completeOnboarding()
177+
.then(async () => {
178+
if (completionErrorToastIdRef.current !== null) {
179+
toastManager.close(completionErrorToastIdRef.current);
180+
completionErrorToastIdRef.current = null;
181+
}
182+
await onDone(projectRef);
183+
if (importWarning) {
184+
toastManager.add({
185+
type: "warning",
186+
title: "Some history was not imported",
187+
description: importWarning,
188+
timeout: 0,
189+
});
190+
} else if (importedThreadCount > 0) {
191+
toastManager.add({
192+
type: "success",
193+
title: `Imported ${importedThreadCount} ${importedThreadCount === 1 ? "thread" : "threads"}`,
194+
});
195+
}
196+
return true;
197+
})
198+
.catch(() => {
199+
const errorToast = {
200+
type: "error",
201+
title: "Could not finish setup",
202+
description: "Your settings could not be saved. Try again.",
203+
} as const;
204+
if (completionErrorToastIdRef.current === null) {
205+
completionErrorToastIdRef.current = toastManager.add(errorToast);
206+
} else {
207+
toastManager.update(completionErrorToastIdRef.current, errorToast);
208+
}
209+
return false;
210+
})
211+
.finally(() => {
212+
if (finishingPromiseRef.current === completion) {
213+
finishingPromiseRef.current = null;
214+
}
215+
});
216+
finishingPromiseRef.current = completion;
217+
return completion;
218+
};
215219

216220
return (
217221
<Dialog open disablePointerDismissal onOpenChange={(_, event) => event.cancel()}>
@@ -248,7 +252,7 @@ export function WelcomeWizard({
248252
<ConnectionStep
249253
expandPairingInitially={!localAvailable && !hasCloudPublicConfig()}
250254
selectedIds={selectedIds}
251-
autoSelectedComputers={autoSelectedComputers.current}
255+
autoSelectedComputers={autoSelectedComputers}
252256
onSelectionChange={setSelection}
253257
onToggleEnvironment={(environmentId, checked) =>
254258
setSelection((current) => {
@@ -461,7 +465,7 @@ function ConnectAccountOption({
461465
const { openAuthPrompt } = useT3ConnectAuthPrompt();
462466
const [expanded, setExpanded] = useState(true);
463467
const [discoveryReady, setDiscoveryReady] = useState(false);
464-
const onDiscoveryReady = useCallback(() => setDiscoveryReady(true), []);
468+
const onDiscoveryReady = () => setDiscoveryReady(true);
465469

466470
return (
467471
<div className="rounded-lg border border-border bg-background">
@@ -709,7 +713,7 @@ function ConnectedAgentsStep({
709713
void refreshProviders({ environmentId, input: {} });
710714
}, [environmentId, refreshProviders]);
711715

712-
const byDriver = useMemo(() => selectOnboardingProvidersByDriver(providers), [providers]);
716+
const byDriver = selectOnboardingProvidersByDriver(providers);
713717

714718
const primaryAgents = PRIMARY_AGENT_DRIVERS.flatMap((driver) => {
715719
const instances =
@@ -1013,10 +1017,7 @@ function AgentInstallTerminal({
10131017
const setupGenerationRef = useRef(0);
10141018
const activeSetupGenerationRef = useRef<number | null>(null);
10151019
const [terminalId] = useState(() => `onboarding-${driver}-${randomUUID()}`);
1016-
const threadRef = useMemo(
1017-
() => scopeThreadRef(environmentId, AGENT_ONBOARDING_THREAD_ID),
1018-
[environmentId],
1019-
);
1020+
const threadRef = scopeThreadRef(environmentId, AGENT_ONBOARDING_THREAD_ID);
10201021
const [setupAttempt, setSetupAttempt] = useState(0);
10211022
const [setupState, setSetupState] = useState<
10221023
"preparing" | "ready" | "openFailed" | "writeFailed"
@@ -1030,6 +1031,7 @@ function AgentInstallTerminal({
10301031
const generation = setupGenerationRef.current + 1;
10311032
setupGenerationRef.current = generation;
10321033
activeSetupGenerationRef.current = generation;
1034+
// oxlint-disable-next-line react/set-state-in-effect -- A retry or a new command reopens the terminal, which shows as preparing until the open settles.
10331035
setSetupState("preparing");
10341036

10351037
setupQueueRef.current = setupQueueRef.current.then(async () => {
@@ -1087,6 +1089,7 @@ function AgentInstallTerminal({
10871089
environmentId,
10881090
openTerminal,
10891091
providerInstanceId,
1092+
// oxlint-disable-next-line react/exhaustive-effect-dependencies -- Retry bumps setupAttempt to open the terminal again.
10901093
setupAttempt,
10911094
terminalId,
10921095
writeTerminal,
@@ -1208,6 +1211,7 @@ function ImportStep({
12081211
project.environmentId === landingProject.environmentId,
12091212
)
12101213
) {
1214+
// oxlint-disable-next-line react/set-state-in-effect -- The landing project is known only once the projects subscription delivers it.
12111215
setLandingProject(null);
12121216
void onDone(landingProject, importWarningRef.current, importedThreadCountRef.current).then(
12131217
(completed) => {
@@ -1217,23 +1221,16 @@ function ImportStep({
12171221
}
12181222
}, [landingProject, onDone, projects, setIsImporting]);
12191223

1220-
const { available: candidates, recent } = useMemo(
1221-
() =>
1222-
partitionOnboardingProjects(
1223-
scans.flatMap((scan) =>
1224-
(scan.data?.candidates ?? []).map((candidate) => ({
1225-
...candidate,
1226-
environmentId: scan.environmentId,
1227-
key: onboardingProjectKey(scan.environmentId, candidate.path),
1228-
})),
1229-
),
1230-
),
1231-
[scans],
1232-
);
1233-
const selectedKeys = useMemo(
1234-
() => selectedPaths ?? new Set(recent.map((candidate) => candidate.key)),
1235-
[selectedPaths, recent],
1224+
const { available: candidates, recent } = partitionOnboardingProjects(
1225+
scans.flatMap((scan) =>
1226+
(scan.data?.candidates ?? []).map((candidate) => ({
1227+
...candidate,
1228+
environmentId: scan.environmentId,
1229+
key: onboardingProjectKey(scan.environmentId, candidate.path),
1230+
})),
1231+
),
12361232
);
1233+
const selectedKeys = selectedPaths ?? new Set(recent.map((candidate) => candidate.key));
12371234
const selected = candidates.filter((candidate) => selectedKeys.has(candidate.key));
12381235

12391236
const canImport = selected.every((candidate) =>
@@ -1531,7 +1528,7 @@ function ImportCandidateList({
15311528
readonly selectedKeys: ReadonlySet<string>;
15321529
readonly onSelectionChange: (next: ReadonlySet<string>) => void;
15331530
}) {
1534-
const { repositories, other } = useMemo(() => groupOnboardingProjects(candidates), [candidates]);
1531+
const { repositories, other } = groupOnboardingProjects(candidates);
15351532
const setKeys = (keys: ReadonlyArray<string>, checked: boolean) => {
15361533
const next = new Set(selectedKeys);
15371534
for (const key of keys) {

0 commit comments

Comments
 (0)