Open History to see the last commit
1import type { EnvironmentConnectionPhase } from "@t3tools/client-runtime/connection";
2import type { EnvironmentShellStatus } from "@t3tools/client-runtime/state/shell";
3import type { EnvironmentThreadStatus } from "@t3tools/client-runtime/state/threads";
4import * as Option from "effect/Option";
5
6export function threadRouteIsHydrating(input: {
7 readonly isLoadingConnections: boolean;
8 readonly environmentRegistered: boolean;
9 readonly localCacheRead: boolean;
10 readonly connectionState: EnvironmentConnectionPhase;
11 readonly shellStatus: EnvironmentShellStatus;
12 readonly shellHasError: boolean;
13 readonly detailStatus: EnvironmentThreadStatus;
14 readonly detailHasError: boolean;
15}): boolean {
16 if (input.detailStatus === "deleted" || input.shellHasError || input.detailHasError) {
17 return false;
18 }
19 if (input.isLoadingConnections) {
20 return true;
21 }
22 if (!input.environmentRegistered) {
23 return false;
24 }
25 if (!input.localCacheRead) {
26 return true;
27 }
28 // Only a first attempt is worth waiting out; a reconnecting environment has already failed.
29 if (
30 input.connectionState === "available" ||
31 input.connectionState === "offline" ||
32 input.connectionState === "reconnecting" ||
33 input.connectionState === "error"
34 ) {
35 return false;
36 }
37 return (
38 input.connectionState === "connecting" ||
39 input.shellStatus === "synchronizing" ||
40 (input.connectionState === "connected" && input.shellStatus === "empty") ||
41 input.detailStatus === "synchronizing" ||
42 (input.connectionState === "connected" && input.detailStatus === "empty")
43 );
44}
45
46/**
47 * Replacing the route with a draft can't be undone, so only a thread this route already showed and
48 * the environment then deleted gives up its route. A linked thread that never arrived keeps waiting.
49 */
50export function threadRouteOpensDraft(input: {
51 readonly detailStatus: EnvironmentThreadStatus;
52 readonly threadWasShown: boolean;
53}): boolean {
54 return input.detailStatus === "deleted" && input.threadWasShown;
55}
56
57/** Until its environment connects, a thread that hasn't arrived may still exist. */
58export function threadRouteConnectionWait(input: {
59 readonly environmentActive: boolean;
60 readonly connectionState: EnvironmentConnectionPhase;
61 readonly detailStatus: EnvironmentThreadStatus;
62}): "connecting" | "unreachable" | null {
63 if (!input.environmentActive || input.detailStatus === "deleted") {
64 return null;
65 }
66 switch (input.connectionState) {
67 case "available":
68 case "connecting":
69 return "connecting";
70 case "offline":
71 case "reconnecting":
72 case "error":
73 return "unreachable";
74 case "connected":
75 case "unsupported":
76 return null;
77 }
78}
79
80export type ThreadUnavailableReason =
81 | "missing"
82 | "switched-off"
83 | "unknown-environment"
84 | "unsupported";
85
86export function threadUnavailableReason(input: {
87 readonly environmentRegistered: boolean;
88 readonly environmentEnabled: boolean;
89 readonly connectionState: EnvironmentConnectionPhase;
90}): ThreadUnavailableReason {
91 if (!input.environmentRegistered) return "unknown-environment";
92 if (!input.environmentEnabled) return "switched-off";
93 return input.connectionState === "unsupported" ? "unsupported" : "missing";
94}
95
96/**
97 * A thread keeps its sync error until the new connection resubscribes, so the first renders after
98 * a reconnect still show the old connection's error. Only an error the live connection reported
99 * counts.
100 */
101export function threadDetailErrorIsCurrent<E>(input: {
102 readonly connectionState: EnvironmentConnectionPhase;
103 readonly error: Option.Option<E>;
104 readonly errorBeforeConnected: Option.Option<E>;
105}): boolean {
106 return (
107 Option.isSome(input.error) &&
108 !(input.connectionState === "connected" && input.error === input.errorBeforeConnected)
109 );
110}