Skip to content

Commit 785774d

Browse files
t3dotggclaude
andcommitted
fix(mobile): the thread list changes once when environments connect, not once per environment
Each environment synchronizes at its own time, and the home list followed each one. Lists now read steady shell snapshots: rows stay as shown until their environment is synchronized, and environments that synchronize within two seconds of each other show together. The splash screen waits for the cached shells, and a thread whose run ended long ago leaves the Working shelf without the two second grace. Co-Authored-By: Claude Fable 5.2 <[email protected]>
1 parent eceeab4 commit 785774d

16 files changed

Lines changed: 512 additions & 31 deletions

‎apps/mobile/src/App.tsx‎

Lines changed: 21 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import { installThreadOutboxDelivery } from "./state/threadOutboxDelivery";
44
import * as Linking from "expo-linking";
55
import * as SplashScreen from "expo-splash-screen";
66
import { requireNativeModule } from "expo";
7-
import { useEffect } from "react";
7+
import { useEffect, useState } from "react";
88
import { Platform, StatusBar } from "react-native";
99
import { GestureHandlerRootView } from "react-native-gesture-handler";
1010
import { KeyboardProvider } from "react-native-keyboard-controller";
@@ -21,7 +21,7 @@ import {
2121
import { pipe } from "effect/Function";
2222
import { DEFAULT_HOSTED_APP_URL } from "@t3tools/shared/connectAuth";
2323

24-
import { RegistryContext } from "@effect/atom-react";
24+
import { RegistryContext, useAtomValue } from "@effect/atom-react";
2525
import { ProjectChipIconRasterizer } from "./native/ProjectChipIconImages";
2626
import { ThreadArrangementHost } from "./features/threads/ThreadArrangementSheet";
2727
import * as Dialog from "./components/ui/Dialog";
@@ -50,7 +50,7 @@ import { RootStack, rootPathnameFromState, workspaceLocationFromState } from "./
5050
import { appAtomRegistry } from "./state/atom-registry";
5151
import { subscribeComposerDraftModelReconciliation } from "./state/use-composer-drafts";
5252
import { environmentThreadShells, threadEnvironment } from "./state/threads";
53-
import { environmentShell } from "./state/shell";
53+
import { environmentShell, shellCachesReadAtom } from "./state/shell";
5454
import { OverlayPortalHost } from "./components/ui/OverlayPortal";
5555
import { shouldHandleAppLink, restoreDraftLinkState } from "./lib/appLinking";
5656
import { hasCloudPublicConfig } from "./features/cloud/publicConfig";
@@ -204,13 +204,28 @@ disposeOnFoundationReplace(typeof module === "undefined" ? undefined : module.ho
204204
for (const dispose of appServiceDisposers) dispose();
205205
});
206206

207+
// Cached shells are local reads that end in a fraction of this time; the limit only keeps a
208+
// failed read from holding the splash screen.
209+
const SPLASH_SHELL_CACHE_WAIT_MS = 1_500;
210+
207211
function SplashScreenCoordinator() {
208212
const { isReady } = useAppearancePreferences();
213+
const shellCachesRead = useAtomValue(shellCachesReadAtom);
214+
const [waitEnded, setWaitEnded] = useState(false);
215+
216+
// oxlint-disable-next-line t3code/no-unreasoned-state-sync -- starts the limit on how long the native splash screen waits for cached shells
217+
useEffect(() => {
218+
const timer = setTimeout(() => setWaitEnded(true), SPLASH_SHELL_CACHE_WAIT_MS);
219+
return () => clearTimeout(timer);
220+
}, []);
209221

210-
// oxlint-disable-next-line t3code/no-unreasoned-state-sync -- hides the native splash screen once appearance preferences have loaded
222+
// The thread list paints every environment's cached rows at once when the splash hides
223+
// after they are read.
224+
const canHide = isReady && (shellCachesRead || waitEnded);
225+
// oxlint-disable-next-line t3code/no-unreasoned-state-sync -- hides the native splash screen once preferences and cached shells have loaded
211226
useEffect(() => {
212-
if (isReady) void SplashScreen.hide();
213-
}, [isReady]);
227+
if (canHide) void SplashScreen.hide();
228+
}, [canHide]);
214229

215230
return null;
216231
}

‎apps/mobile/src/features/home/HomeRouteScreen.tsx‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ import {
1515
} from "@t3tools/client-runtime/state/project-grouping";
1616

1717
import { useProjects } from "../../state/entities";
18-
import { environmentThreadShells } from "../../state/threads";
18+
import { steadyNavigationThreadShellsAtom } from "../../state/threads";
1919
import { useLaunchingThreads, usePendingNewTasks } from "../../state/use-pending-new-tasks";
2020
import { useWorkspaceState } from "../../state/workspace";
2121
import { useSavedRemoteConnections } from "../../state/use-remote-environment-registry";
@@ -47,7 +47,7 @@ export function HomeRouteScreen() {
4747
// Stop rebuilding the streaming thread list while Thread covers Home.
4848
const visible = useHomeRouteVisible();
4949
const threads = useLaunchingThreads(
50-
useAtomValueWhileVisible(environmentThreadShells.navigationThreadShellsAtom, visible),
50+
useAtomValueWhileVisible(steadyNavigationThreadShellsAtom, visible),
5151
);
5252
const { environments: workspaceEnvironments, state: catalogState } = useWorkspaceState();
5353
const { savedConnectionsById } = useSavedRemoteConnections();

‎apps/mobile/src/features/home/useThreadListActions.test.ts‎

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -121,10 +121,8 @@ vi.mock("../../state/thread-order", () => ({
121121
},
122122
}));
123123
vi.mock("../../state/threads", () => ({
124-
environmentThreadShells: {
125-
threadShellsAtom: "thread-shells",
126-
navigationThreadShellsAtom: "thread-shells",
127-
},
124+
environmentThreadShells: { threadShellsAtom: "thread-shells" },
125+
steadyNavigationThreadShellsAtom: "thread-shells",
128126
threadEnvironment: Object.fromEntries(
129127
[
130128
"archive",

‎apps/mobile/src/features/home/useThreadListActions.ts‎

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,11 @@ import { appAtomRegistry } from "../../state/atom-registry";
1717
import { environmentServerConfigsAtom } from "../../state/server";
1818
import { connectedEnvironmentIdsAtom } from "../../state/presentation";
1919
import { readEnvironmentScope } from "../../state/session";
20-
import { environmentThreadShells, threadEnvironment } from "../../state/threads";
20+
import {
21+
environmentThreadShells,
22+
steadyNavigationThreadShellsAtom,
23+
threadEnvironment,
24+
} from "../../state/threads";
2125
import { queuedThreadKeysAtom } from "../../state/use-thread-outbox";
2226
import { useAtomCommand } from "../../state/use-atom-command";
2327
import {
@@ -572,7 +576,7 @@ export function useThreadListActions(): {
572576
const moveThread = async (thread: EnvironmentThreadShell, direction: ThreadMoveDestination) => {
573577
if (!checkThreadOperationPermission(thread, "Could not move thread")) return false;
574578
if (getPendingThreadOrder() !== null || appAtomRegistry.get(threadDropBusyAtom)) return false;
575-
const shells = appAtomRegistry.get(environmentThreadShells.navigationThreadShellsAtom);
579+
const shells = appAtomRegistry.get(steadyNavigationThreadShellsAtom);
576580
const current = shells.find(
577581
(row) => row.id === thread.id && row.environmentId === thread.environmentId,
578582
);

‎apps/mobile/src/features/threads/ThreadArrangementSheet.tsx‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ import { scopedThreadKey } from "../../lib/scopedEntities";
1818
import { deriveThreadArrangementSizing } from "../../lib/layout";
1919
import { environmentServerConfigsAtom } from "../../state/server";
2020
import { threadListEnvironmentsAtom } from "../../state/presentation";
21-
import { environmentThreadShells } from "../../state/threads";
21+
import { steadyNavigationThreadShellsAtom } from "../../state/threads";
2222
import { sortSettledThreads } from "@t3tools/client-runtime/state/thread-sort";
2323
import { useOlderSettledShelf, useOlderSettledShelfGrowth } from "../../state/olderSettledShelf";
2424
import {
@@ -170,7 +170,7 @@ function ThreadArrangementSheet(props: { onClose: () => void }) {
170170
baseFontSize: appearance.baseFontSize,
171171
fontScale,
172172
});
173-
const threads = useAtomValue(environmentThreadShells.navigationThreadShellsAtom);
173+
const threads = useAtomValue(steadyNavigationThreadShellsAtom);
174174
const configs = useAtomValue(environmentServerConfigsAtom);
175175
const listEnvironments = useAtomValue(threadListEnvironmentsAtom);
176176
const queuedThreadKeys = useAtomValue(queuedThreadKeysAtom);

‎apps/mobile/src/state/entities.ts‎

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,11 @@ import { projectFaviconUrlAtom } from "./assets";
3535
import { orchestrationEnvironment } from "./orchestration";
3636
import { environmentProjects } from "./projects";
3737
import { environmentServerConfigsAtom, serverEnvironment } from "./server";
38-
import { environmentThreadDetails, environmentThreadShells } from "./threads";
38+
import {
39+
environmentThreadDetails,
40+
environmentThreadShells,
41+
steadyNavigationThreadShellsAtom,
42+
} from "./threads";
3943

4044
const EMPTY_PROJECT_ATOM = Atom.make<EnvironmentProject | null>(null).pipe(
4145
Atom.withLabel("mobile-project:empty"),
@@ -201,7 +205,7 @@ export function useVisibleThreadShells(): ReadonlyArray<EnvironmentThreadShell>
201205
}
202206

203207
export function useNavigationThreadShells(): ReadonlyArray<EnvironmentThreadShell> {
204-
return useAtomValue(environmentThreadShells.navigationThreadShellsAtom);
208+
return useAtomValue(steadyNavigationThreadShellsAtom);
205209
}
206210

207211
export function useProject(ref: ScopedProjectRef | null): EnvironmentProject | null {

‎apps/mobile/src/state/shell.ts‎

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import { enabledEnvironmentIds } from "@t3tools/client-runtime/state/connections";
12
import {
23
createEnvironmentShellAtoms,
34
createEnvironmentShellSummaryAtom,
@@ -27,6 +28,23 @@ const shellHasError = (state: EnvironmentShellState) => Option.isSome(state.erro
2728
// The empty placeholder stands until the environment has read its cached shell.
2829
const shellCacheRead = (state: EnvironmentShellState) => state !== EMPTY_SHELL_STATE;
2930

31+
const shellCacheReadAtom = Atom.family((environmentId: EnvironmentId) =>
32+
Atom.make((get) => shellCacheRead(get(environmentShell.stateValueAtom(environmentId)))),
33+
);
34+
35+
/**
36+
* True once every enabled environment has read its cached shell. A list that paints before
37+
* then gets the cached rows of one environment at a time.
38+
*/
39+
export const shellCachesReadAtom = Atom.make((get) => {
40+
const catalog = get(environmentCatalog.catalogValueAtom);
41+
if (!catalog.isReady) return false;
42+
for (const environmentId of enabledEnvironmentIds(catalog)) {
43+
if (!get(shellCacheReadAtom(environmentId))) return false;
44+
}
45+
return true;
46+
}).pipe(Atom.withLabel("mobile-shell-caches-read"));
47+
3048
export function useEnvironmentShellReadiness(environmentId: EnvironmentId | null) {
3149
const atom =
3250
environmentId === null

‎apps/mobile/src/state/thread-order.test.ts‎

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ import {
1212
pendingThreadOrderAtom,
1313
} from "./thread-order";
1414
import { environmentServerConfigsAtom } from "./server";
15-
import { environmentThreadShells } from "./threads";
15+
import { steadyNavigationThreadShellsAtom } from "./threads";
1616

1717
vi.mock("./atom-registry", async () => {
1818
const { AtomRegistry } = await import("effect/reactivity");
@@ -21,7 +21,7 @@ vi.mock("./atom-registry", async () => {
2121
vi.mock("./threads", async () => {
2222
const { Atom } = await import("effect/reactivity");
2323
return {
24-
environmentThreadShells: { navigationThreadShellsAtom: Atom.make([]).pipe(Atom.keepAlive) },
24+
steadyNavigationThreadShellsAtom: Atom.make([]).pipe(Atom.keepAlive),
2525
};
2626
});
2727
vi.mock("./server", async () => {
@@ -36,7 +36,7 @@ vi.mock("./use-thread-outbox", async () => {
3636
type ServerConfigs = Atom.Type<typeof environmentServerConfigsAtom>;
3737
type ServerConfig = ServerConfigs extends ReadonlyMap<EnvironmentId, infer Config> ? Config : never;
3838
const configsAtom = environmentServerConfigsAtom as Atom.Writable<ServerConfigs, ServerConfigs>;
39-
const shellsAtom = environmentThreadShells.navigationThreadShellsAtom as Atom.Writable<
39+
const shellsAtom = steadyNavigationThreadShellsAtom as Atom.Writable<
4040
readonly EnvironmentThreadShell[],
4141
readonly EnvironmentThreadShell[]
4242
>;

‎apps/mobile/src/state/thread-order.ts‎

Lines changed: 4 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ import {
1212
} from "../features/threads/threadListV2";
1313
import { appAtomRegistry } from "./atom-registry";
1414
import { environmentServerConfigsAtom } from "./server";
15-
import { environmentThreadShells } from "./threads";
15+
import { steadyNavigationThreadShellsAtom } from "./threads";
1616
import { queuedThreadKeysAtom } from "./use-thread-outbox";
1717

1818
export const threadArrangementOpenAtom = Atom.make(false).pipe(Atom.keepAlive);
@@ -64,7 +64,7 @@ export function beginPendingThreadOrder(pending: PendingThreadOrder) {
6464
const current = appAtomRegistry.get(pendingThreadOrderAtom);
6565
if (current === null) return;
6666
const configs = appAtomRegistry.get(environmentServerConfigsAtom);
67-
const threads = appAtomRegistry.get(environmentThreadShells.navigationThreadShellsAtom);
67+
const threads = appAtomRegistry.get(steadyNavigationThreadShellsAtom);
6868
const nowMs = Date.now();
6969
const ordered = getThreadListV2OrderedSection({
7070
threads,
@@ -87,17 +87,12 @@ export function beginPendingThreadOrder(pending: PendingThreadOrder) {
8787
teardownPendingOrder = teardown;
8888
appAtomRegistry.set(pendingThreadOrderAtom, pending);
8989
unsubscribers.push(
90-
appAtomRegistry.subscribe(environmentThreadShells.navigationThreadShellsAtom, refresh),
90+
appAtomRegistry.subscribe(steadyNavigationThreadShellsAtom, refresh),
9191
appAtomRegistry.subscribe(environmentServerConfigsAtom, refresh),
9292
appAtomRegistry.subscribe(queuedThreadKeysAtom, refresh),
9393
minuteTick.subscribe(refresh),
9494
);
95-
armWake(
96-
nextSnoozeWakeAfter(
97-
appAtomRegistry.get(environmentThreadShells.navigationThreadShellsAtom),
98-
Date.now(),
99-
),
100-
);
95+
armWake(nextSnoozeWakeAfter(appAtomRegistry.get(steadyNavigationThreadShellsAtom), Date.now()));
10196
return {
10297
isPending: () => {
10398
refresh();

‎apps/mobile/src/state/threads.ts‎

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import {
1010
createThreadEnvironmentAtoms,
1111
createThreadFamilyAtoms,
1212
} from "@t3tools/client-runtime/state/threads";
13+
import { createSteadyShellAtoms } from "@t3tools/client-runtime/state/shell";
1314
import type { EnvironmentId, ThreadId } from "@t3tools/contracts";
1415
import { AsyncResult, Atom } from "effect/reactivity";
1516

@@ -46,6 +47,21 @@ export const environmentThreadShells = createEnvironmentThreadShellAtoms({
4647
snapshotAtom: threadEnvironment.snapshotAtom,
4748
hiddenThreadTagsAtom: serverEnvironment.hiddenThreadTagsAtom,
4849
});
50+
const steadyShells = createSteadyShellAtoms({
51+
catalogValueAtom: environmentCatalog.catalogValueAtom,
52+
snapshotAtom: threadEnvironment.snapshotAtom,
53+
statusAtom: environmentShell.statusAtom,
54+
connectionStateAtom: environmentCatalog.stateAtom,
55+
});
56+
/**
57+
* The threads that lists arrange. Rows wait while their environment synchronizes, so a list
58+
* changes once when environments connect, not once per environment.
59+
*/
60+
export const steadyNavigationThreadShellsAtom = createEnvironmentThreadShellAtoms({
61+
catalogValueAtom: environmentCatalog.catalogValueAtom,
62+
snapshotAtom: steadyShells.snapshotAtom,
63+
hiddenThreadTagsAtom: serverEnvironment.hiddenThreadTagsAtom,
64+
}).navigationThreadShellsAtom;
4965
export const childThreadApprovalsAtom = createChildThreadApprovalsAtom({
5066
childThreadInputsAtom: environmentThreadShells.childThreadInputsAtom,
5167
pendingRequestsAtom: environmentThreadDetails.pendingRequestsAtom,

0 commit comments

Comments
 (0)