Repository navigation
fix(mobile): a linked thread waits for its environment instead of opening a draft #40607
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
012d251
2580399
57bb7e0
fdf56b0
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,5 +1,7 @@ | ||
| import * as Dialog from "../../components/ui/Dialog"; | ||
| import { errorDisplayMessage } from "@t3tools/client-runtime/errors"; | ||
| import { useAtomValue } from "@effect/atom-react"; | ||
| import { environmentCatalog } from "../../connection/catalog"; | ||
| import { useRememberThreadProject } from "./useRememberThreadProject"; | ||
| import { useOpenPullRequest } from "../../native/useOpenPullRequest"; | ||
| import { makeTurnCommandMetadata } from "../../lib/commandMetadata"; | ||
|
|
@@ -116,9 +118,12 @@ import { ThreadFileNavigatorPane } from "../files/thread-file-navigator-pane"; | |
| import { InspectorSheetContext } from "../layout/workspace-inspector-sheet"; | ||
| import type { InspectorContent } from "../layout/inspector-panes"; | ||
| import { | ||
| THREAD_ROUTE_CONNECT_GRACE_MS, | ||
| threadDetailErrorIsCurrent, | ||
| threadRouteAwaitsConnection, | ||
| threadRouteIsHydrating, | ||
| threadRouteOpensDraft, | ||
| threadRouteStartsConnection, | ||
| } from "./thread-route-hydration"; | ||
| import { ThreadHeader } from "./ThreadHeader"; | ||
| import { resumeThread } from "./resume-thread"; | ||
|
|
@@ -267,6 +272,10 @@ function LiveThreadRouteController(props: ThreadRouteScreenProps) { | |
| const { onReconnectEnvironment } = useRemoteConnections(); | ||
| const navigation = useNavigation(); | ||
| const routeName = useRoute().name; | ||
| const catalog = useAtomValue(environmentCatalog.catalogValueAtom); | ||
| const routeCatalogEntry = environmentId ? catalog.entries.get(environmentId) : undefined; | ||
| const routeEnvironmentEnabled = | ||
| routeCatalogEntry?.enabled !== false && routeCatalogEntry?.unsupportedReason === undefined; | ||
| const routeConnectionState = | ||
| routeEnvironmentRuntime?.connectionState ?? (environmentId ? "available" : connectionState); | ||
| const routeThreadKey = | ||
|
|
@@ -277,6 +286,48 @@ function LiveThreadRouteController(props: ThreadRouteScreenProps) { | |
| selectedThread === null | ||
| ? null | ||
| : scopedThreadKey(selectedThread.environmentId, selectedThread.id); | ||
| const routeThreadShown = selectedThread !== null && selectedThreadKey === routeThreadKey; | ||
| const [shownThreadKey, setShownThreadKey] = useState<string | null>(null); | ||
| if (routeThreadShown && shownThreadKey !== routeThreadKey) { | ||
| setShownThreadKey(routeThreadKey); | ||
| } | ||
| // Each Reconnect restarts the wait for the environment to deliver the thread. | ||
| const [connectAttempt, setConnectAttempt] = useState(0); | ||
| const connectAttemptKey = routeThreadKey === null ? null : `${routeThreadKey}#${connectAttempt}`; | ||
| const [graceExpiredKey, setGraceExpiredKey] = useState<string | null>(null); | ||
| // A shown thread that later drops out waits again instead of reusing an earlier timeout. | ||
| if (routeThreadShown && graceExpiredKey !== null) { | ||
| setGraceExpiredKey(null); | ||
| } | ||
| const graceExpired = connectAttemptKey !== null && graceExpiredKey === connectAttemptKey; | ||
| // oxlint-disable-next-line t3code/no-unreasoned-state-sync -- ends the wait for a missing thread's environment once the grace period passes | ||
| useEffect(() => { | ||
| if (connectAttemptKey === null || routeThreadShown) return; | ||
| const timer = setTimeout( | ||
| () => setGraceExpiredKey(connectAttemptKey), | ||
| THREAD_ROUTE_CONNECT_GRACE_MS, | ||
| ); | ||
| return () => clearTimeout(timer); | ||
| }, [connectAttemptKey, routeThreadShown]); | ||
| const startConnection = useEffectEvent((target: EnvironmentId) => { | ||
| onReconnectEnvironment(target); | ||
| }); | ||
| const connectionStartedKey = useRef<string | null>(null); | ||
| const routeStartsConnection = | ||
| environmentId !== null && | ||
| routeEnvironmentRuntime !== null && | ||
| routeEnvironmentEnabled && | ||
| !routeThreadShown && | ||
| !graceExpired && | ||
| threadRouteStartsConnection(routeConnectionState); | ||
| // A link opened on a resume often finds its environment backing off, so it retries now. | ||
| // oxlint-disable-next-line t3code/no-unreasoned-state-sync -- starts the route environment's connection once per wait while a linked thread is missing | ||
| useEffect(() => { | ||
| if (!routeStartsConnection || environmentId === null) return; | ||
| if (connectionStartedKey.current === connectAttemptKey) return; | ||
| connectionStartedKey.current = connectAttemptKey; | ||
|
Comment on lines
+327
to
+328
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Reset retry ownership when the thread is shown. If an offline missing thread triggers an automatic retry, If the same thread disappears while offline, the controller starts another grace timer. Line 327 then suppresses the automatic retry because the attempt key is unchanged. The new wait cannot bypass connection backoff. Clear
|
||
| startConnection(environmentId); | ||
| }, [routeStartsConnection, environmentId, connectAttemptKey]); | ||
| const selectedThreadDetailState = useSelectedThreadDetailState(); | ||
| const detailError = selectedThreadDetailState.error; | ||
| const [errorBeforeConnected, setErrorBeforeConnected] = useState(() => | ||
|
|
@@ -302,7 +353,7 @@ function LiveThreadRouteController(props: ThreadRouteScreenProps) { | |
| return withInspectorHeader(<OpeningThreadLoadingScreen />); | ||
| } | ||
|
|
||
| if (selectedThread !== null && selectedThreadKey === routeThreadKey) { | ||
| if (routeThreadShown) { | ||
| // ThreadRouteContent draws its own inspector header so it can show the sync status. | ||
| return ( | ||
| <InspectorHeaderShownContext value={props.inspectorPresentation !== undefined}> | ||
|
|
@@ -328,16 +379,25 @@ function LiveThreadRouteController(props: ThreadRouteScreenProps) { | |
| }), | ||
| }); | ||
|
|
||
| if (stillHydrating) { | ||
| if ( | ||
| stillHydrating || | ||
| (routeEnvironmentRuntime !== null && | ||
| threadRouteAwaitsConnection({ | ||
| connectionState: routeConnectionState, | ||
| environmentEnabled: routeEnvironmentEnabled, | ||
| detailStatus: selectedThreadDetailState.status, | ||
| graceExpired, | ||
| })) | ||
| ) { | ||
| return withInspectorHeader(<OpeningThreadLoadingScreen />); | ||
| } | ||
|
|
||
| if ( | ||
| routeName === "Thread" && | ||
| !props.inspectorPresentation && | ||
| threadRouteOpensDraft({ | ||
| connectionState: routeConnectionState, | ||
| detailStatus: selectedThreadDetailState.status, | ||
| threadWasShown: shownThreadKey === routeThreadKey, | ||
| }) | ||
| ) { | ||
| return <UnavailableThreadDraftRedirect environmentId={environmentId} />; | ||
|
|
@@ -350,6 +410,9 @@ function LiveThreadRouteController(props: ThreadRouteScreenProps) { | |
| } | ||
| onAction={() => { | ||
| if (routeEnvironmentRuntime !== null) { | ||
| // This reconnects itself, so the new wait must not start the connection again. | ||
| connectionStartedKey.current = `${routeThreadKey}#${connectAttempt + 1}`; | ||
| setConnectAttempt(connectAttempt + 1); | ||
| onReconnectEnvironment(environmentId); | ||
| return; | ||
| } | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Clear the recorded grace expiry when the route moves to another thread.
graceExpiredKeyholds a single value. A thread that times out leaves its key in that slot. If the route then moves to another thread that is not shown, and the user returns before that thread's wait ends, the returning thread is treated as already expired. The loading wait and the automatic connection attempt are skipped, and the unavailable screen appears at once. Returning to a thread must start a new wait, with the automatic attempt, as it does on a first visit.