GeezerVison is showing github.com — fix(mobile): open threads from the local cache instead of "Thread una… · pingdotgg/t3code@a54ba53
File treeExpand file treeCollapse file tree Open diff view settings packages/client-runtime/src/state
Expand file treeCollapse file tree Open diff view settings Collapse file Copy file name to clipboardExpand all lines: apps/mobile/src/features/threads/ThreadRouteScreen.tsx +39-1Lines changed: 39 additions & 1 deletion | Original file line number | Diff line number | Diff line change |
|---|
@@ -5,7 +5,12 @@ import { buildProjectThreadStartTurnInput } from "../../lib/projectThreadStartTu | 5 | 5 | import { useWorktreeSetup } from "./use-worktree-setup"; | 6 | 6 | import { worktreeSetupAgentStarted } from "@t3tools/client-runtime/worktree-setup"; | 7 | 7 | import { AppBar } from "../../components/ui/AppBar"; | 8 | | -import { StackActions, useNavigation, type StaticScreenProps } from "@react-navigation/native"; | | 8 | +import { | | 9 | + StackActions, | | 10 | + useNavigation, | | 11 | + useRoute, | | 12 | + type StaticScreenProps, | | 13 | +} from "@react-navigation/native"; | 9 | 14 | import { useEffect, useEffectEvent, useRef, useState, type ReactNode } from "react"; | 10 | 15 | import * as Option from "effect/Option"; | 11 | 16 | import { | @@ -74,6 +79,9 @@ import { useThreadComposerState } from "../../state/use-thread-composer-state"; | 74 | 79 | import { resolveMergeBackTargetThreadId } from "@t3tools/client-runtime/state/thread-relationships"; | 75 | 80 | import { resolveLatestMergeBackRun } from "@t3tools/client-runtime/state/thread-workflows"; | 76 | 81 | import { threadEnvironment, useThreadFamily } from "../../state/threads"; | | 82 | +import { EMPTY_ENVIRONMENT_THREAD_STATE } from "@t3tools/client-runtime/state/threads"; | | 83 | +import { useProjects } from "../../state/entities"; | | 84 | +import { newTaskDestination } from "./new-task-project-selection"; | 77 | 85 | import { projectThreadContentPresentation } from "./threadContentPresentation"; | 78 | 86 | import { useAppearancePreferences } from "../settings/appearance/AppearancePreferencesProvider"; | 79 | 87 | import { | @@ -191,6 +199,27 @@ function ThreadUnavailableScreen(props: { | 191 | 199 | ); | 192 | 200 | } | 193 | 201 |
| | 202 | +/** A thread that can't load opens a new task in its environment instead of a dead end. */ | | 203 | +function UnavailableThreadDraftRedirect(props: { readonly environmentId: EnvironmentId }) { | | 204 | + const navigation = useNavigation(); | | 205 | + const projects = useProjects(); | | 206 | + const { layout } = useAdaptiveWorkspaceLayout(); | | 207 | + const openDraft = useEffectEvent(() => { | | 208 | + const project = projects.find((candidate) => candidate.environmentId === props.environmentId); | | 209 | + navigation.dispatch( | | 210 | + StackActions.replace( | | 211 | + "Thread", | | 212 | + newTaskDestination(layout.usesSplitView, projects, project, uuidv4()), | | 213 | + ), | | 214 | + ); | | 215 | + }); | | 216 | + // oxlint-disable-next-line t3code/react-effect-budget -- replaces this route in the navigator | | 217 | + useEffect(() => { | | 218 | + openDraft(); | | 219 | + }, []); | | 220 | + return <OpeningThreadLoadingScreen />; | | 221 | +} | | 222 | + | 194 | 223 | export function ThreadRouteScreen(props: ThreadRouteScreenProps) { | 195 | 224 | return ( | 196 | 225 | <ThreadScreenHost> | @@ -225,6 +254,7 @@ function LiveThreadRouteController(props: ThreadRouteScreenProps) { | 225 | 254 | const routeEnvironmentShellState = useEnvironmentShellReadiness(environmentId); | 226 | 255 | const { onReconnectEnvironment } = useRemoteConnections(); | 227 | 256 | const navigation = useNavigation(); | | 257 | + const routeName = useRoute().name; | 228 | 258 | const routeConnectionState = | 229 | 259 | routeEnvironmentRuntime?.connectionState ?? (environmentId ? "available" : connectionState); | 230 | 260 | const routeThreadKey = | @@ -261,6 +291,10 @@ function LiveThreadRouteController(props: ThreadRouteScreenProps) { | 261 | 291 |
| 262 | 292 | const stillHydrating = threadRouteIsHydrating({ | 263 | 293 | isLoadingConnections: !connectionsReady, | | 294 | + environmentRegistered: routeEnvironmentRuntime !== null, | | 295 | + localCacheRead: | | 296 | + routeEnvironmentShellState.cacheRead && | | 297 | + selectedThreadDetailState !== EMPTY_ENVIRONMENT_THREAD_STATE, | 264 | 298 | connectionState: routeConnectionState, | 265 | 299 | shellStatus: routeEnvironmentShellState.status, | 266 | 300 | shellHasError: routeEnvironmentShellState.hasError, | @@ -272,6 +306,10 @@ function LiveThreadRouteController(props: ThreadRouteScreenProps) { | 272 | 306 | return withInspectorHeader(<OpeningThreadLoadingScreen />); | 273 | 307 | } | 274 | 308 |
| | 309 | + if (routeName === "Thread" && !props.inspectorPresentation) { | | 310 | + return <UnavailableThreadDraftRedirect environmentId={environmentId} />; | | 311 | + } | | 312 | + | 275 | 313 | return withInspectorHeader( | 276 | 314 | <ThreadUnavailableScreen | 277 | 315 | actionLabel={ | |
Collapse file Copy file name to clipboardExpand all lines: apps/mobile/src/features/threads/thread-route-hydration.test.ts +17Lines changed: 17 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -4,6 +4,8 @@ import { threadRouteIsHydrating } from "./thread-route-hydration"; | 4 | 4 |
| 5 | 5 | const settled = { | 6 | 6 | isLoadingConnections: false, | | 7 | + environmentRegistered: true, | | 8 | + localCacheRead: true, | 7 | 9 | connectionState: "connected" as const, | 8 | 10 | shellStatus: "live" as const, | 9 | 11 | shellHasError: false, | @@ -19,6 +21,21 @@ describe("threadRouteIsHydrating", () => { | 19 | 21 | expect(threadRouteIsHydrating({ ...settled, detailStatus: "empty" })).toBe(true); | 20 | 22 | }); | 21 | 23 |
| | 24 | + it("waits for the local cache before trusting an offline connection", () => { | | 25 | + const offline = { | | 26 | + ...settled, | | 27 | + connectionState: "offline" as const, | | 28 | + detailStatus: "empty" as const, | | 29 | + }; | | 30 | + expect(threadRouteIsHydrating({ ...offline, localCacheRead: false })).toBe(true); | | 31 | + expect(threadRouteIsHydrating(offline)).toBe(false); | | 32 | + expect(threadRouteIsHydrating({ ...offline, connectionState: "reconnecting" })).toBe(false); | | 33 | + expect(threadRouteIsHydrating({ ...offline, connectionState: "connecting" })).toBe(true); | | 34 | + expect( | | 35 | + threadRouteIsHydrating({ ...offline, localCacheRead: false, environmentRegistered: false }), | | 36 | + ).toBe(false); | | 37 | + }); | | 38 | + | 22 | 39 | it("stops waiting once an empty detail has an actionable outcome", () => { | 23 | 40 | expect( | 24 | 41 | threadRouteIsHydrating({ | |
Collapse file Copy file name to clipboardExpand all lines: apps/mobile/src/features/threads/thread-route-hydration.ts +10-1Lines changed: 10 additions & 1 deletion | Original file line number | Diff line number | Diff line change |
|---|
@@ -4,6 +4,8 @@ import type { EnvironmentThreadStatus } from "@t3tools/client-runtime/state/thre | 4 | 4 |
| 5 | 5 | export function threadRouteIsHydrating(input: { | 6 | 6 | readonly isLoadingConnections: boolean; | | 7 | + readonly environmentRegistered: boolean; | | 8 | + readonly localCacheRead: boolean; | 7 | 9 | readonly connectionState: EnvironmentConnectionPhase; | 8 | 10 | readonly shellStatus: EnvironmentShellStatus; | 9 | 11 | readonly shellHasError: boolean; | @@ -16,16 +18,23 @@ export function threadRouteIsHydrating(input: { | 16 | 18 | if (input.isLoadingConnections) { | 17 | 19 | return true; | 18 | 20 | } | | 21 | + if (!input.environmentRegistered) { | | 22 | + return false; | | 23 | + } | | 24 | + if (!input.localCacheRead) { | | 25 | + return true; | | 26 | + } | | 27 | + // Only a first attempt is worth waiting out; a reconnecting environment has already failed. | 19 | 28 | if ( | 20 | 29 | input.connectionState === "available" || | 21 | 30 | input.connectionState === "offline" || | | 31 | + input.connectionState === "reconnecting" || | 22 | 32 | input.connectionState === "error" | 23 | 33 | ) { | 24 | 34 | return false; | 25 | 35 | } | 26 | 36 | return ( | 27 | 37 | input.connectionState === "connecting" || | 28 | | - input.connectionState === "reconnecting" || | 29 | 38 | input.shellStatus === "synchronizing" || | 30 | 39 | (input.connectionState === "connected" && input.shellStatus === "empty") || | 31 | 40 | input.detailStatus === "synchronizing" || | |
Collapse file Copy file name to clipboardExpand all lines: apps/mobile/src/features/workspace/workspace-selection-store.ts +15-5Lines changed: 15 additions & 5 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -19,7 +19,11 @@ import { workspaceRoutesOf } from "../layout/workspace-overlay-routes"; | 19 | 19 | import type { WorkspaceSelections } from "../../persistence/mobile-preferences"; | 20 | 20 | import { appAtomRegistry } from "../../state/atom-registry"; | 21 | 21 | import { environmentPresentations } from "../../state/presentation"; | 22 | | -import { environmentThreadShells, threadEnvironment } from "../../state/threads"; | | 22 | +import { | | 23 | + environmentThreadShells, | | 24 | + startOpeningEnvironmentThread, | | 25 | + threadEnvironment, | | 26 | +} from "../../state/threads"; | 23 | 27 | import { | 24 | 28 | rememberedSettingsPage, | 25 | 29 | workspaceSelectionsIn, | @@ -137,11 +141,17 @@ export function settingsLinkPage() { | 137 | 141 | */ | 138 | 142 | export async function launchWorkspaceURL(): Promise<string | null> { | 139 | 143 | if (!REMEMBERS) return null; | 140 | | - await Promise.all([ | 141 | | - hydrateWorkspaceSelections(), | 142 | | - firstNativeLayoutMetrics(NATIVE_METRICS_TIMEOUT_MS), | 143 | | - ]); | | 144 | + const metrics = firstNativeLayoutMetrics(NATIVE_METRICS_TIMEOUT_MS); | | 145 | + await hydrateWorkspaceSelections(); | 144 | 146 | const thread = selections.threads; | | 147 | + // Read the chat's cached copy while the layout metrics are still on their way. | | 148 | + if (thread) { | | 149 | + startOpeningEnvironmentThread( | | 150 | + thread.environmentId as EnvironmentId, | | 151 | + thread.threadId as ThreadId, | | 152 | + ); | | 153 | + } | | 154 | + await metrics; | 145 | 155 | if (!thread || !workspaceHasColumns() || !workspaceSelectionCheck.thread(thread)) return null; | 146 | 156 | return Linking.createURL( | 147 | 157 | `threads/${encodeURIComponent(thread.environmentId)}/${encodeURIComponent(thread.threadId)}`, | |
Collapse file Copy file name to clipboardExpand all lines: apps/mobile/src/state/shell.ts +3Lines changed: 3 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -26,6 +26,8 @@ const EMPTY_ENVIRONMENT_SHELL_STATE_ATOM = Atom.make<EnvironmentShellState>(EMPT | 26 | 26 |
| 27 | 27 | const shellStatus = (state: EnvironmentShellState) => state.status; | 28 | 28 | const shellHasError = (state: EnvironmentShellState) => Option.isSome(state.error); | | 29 | +// The empty placeholder stands until the environment has read its cached shell. | | 30 | +const shellCacheRead = (state: EnvironmentShellState) => state !== EMPTY_SHELL_STATE; | 29 | 31 |
| 30 | 32 | export function useEnvironmentShellReadiness(environmentId: EnvironmentId | null) { | 31 | 33 | const atom = | @@ -35,5 +37,6 @@ export function useEnvironmentShellReadiness(environmentId: EnvironmentId | null | 35 | 37 | return { | 36 | 38 | status: useAtomValue(atom, shellStatus), | 37 | 39 | hasError: useAtomValue(atom, shellHasError), | | 40 | + cacheRead: useAtomValue(atom, shellCacheRead), | 38 | 41 | }; | 39 | 42 | } |
Collapse file Copy file name to clipboardExpand all lines: packages/client-runtime/src/state/presentation.test.ts +22Lines changed: 22 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -135,3 +135,25 @@ describe("presentations with providers", () => { | 135 | 135 | } | 136 | 136 | }); | 137 | 137 | }); | | 138 | + | | 139 | +describe("connection presentation", () => { | | 140 | + it("presents an enabled environment as connecting until its supervisor reports", () => { | | 141 | + const h = harness(); | | 142 | + const phase = () => h.registry.get(h.full.presentationAtom(FIRST))?.connection.phase; | | 143 | + const stop = h.registry.subscribe(h.full.presentationAtom(FIRST), () => {}); | | 144 | + try { | | 145 | + expect(phase()).toBe("connecting"); | | 146 | + h.registry.set(h.catalog, { | | 147 | + isReady: true, | | 148 | + entries: new Map([[FIRST, { ...entry(FIRST), enabled: false }]]), | | 149 | + }); | | 150 | + expect(phase()).toBe("available"); | | 151 | + h.registry.set(h.catalog, { isReady: true, entries: new Map([[FIRST, entry(FIRST)]]) }); | | 152 | + h.registry.set(h.state, AsyncResult.success({ ...AVAILABLE_CONNECTION_STATE })); | | 153 | + expect(phase()).toBe("available"); | | 154 | + } finally { | | 155 | + stop(); | | 156 | + h.registry.dispose(); | | 157 | + } | | 158 | + }); | | 159 | +}); |
Collapse file Copy file name to clipboardExpand all lines: packages/client-runtime/src/state/presentation.ts +6-3Lines changed: 6 additions & 3 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -82,10 +82,13 @@ export function createEnvironmentPresentationAtoms<E>(input: { | 82 | 82 | () => AVAILABLE_CONNECTION_STATE, | 83 | 83 | ); | 84 | 84 | const previous = Option.getOrNull(get.self<EnvironmentPresentation | null>()); | | 85 | + // The supervisor never publishes this placeholder; an enabled one starts out connecting. | 85 | 86 | const connection: EnvironmentConnectionPresentation = | 86 | | - entry.unsupportedReason === undefined | 87 | | - ? presentEnvironmentConnection(state) | 88 | | - : { phase: "unsupported", error: entry.unsupportedReason, traceId: null }; | | 87 | + entry.unsupportedReason !== undefined | | 88 | + ? { phase: "unsupported", error: entry.unsupportedReason, traceId: null } | | 89 | + : state === AVAILABLE_CONNECTION_STATE && entry.enabled | | 90 | + ? { phase: "connecting", error: null, traceId: null } | | 91 | + : presentEnvironmentConnection(state); | 89 | 92 | const next = { | 90 | 93 | entry, | 91 | 94 | connection: | |
|