Skip to content
Source
Sign inSign up
pingdotgg/t3code
Open History to see the last commit

floating-working-status.ts

101 lines3.3 KB

1import type { EnvironmentConnectionPhase } from "@t3tools/client-runtime/connection";
2import type { EnvironmentId } from "@t3tools/contracts";
3​
4export interface FloatingStatusAction {
5 readonly label: string;
6 readonly accessibilityLabel: string;
7 readonly disabled: boolean;
8 readonly onPress: () => void;
9}
10​
11export type FloatingWorkingStatus =
12 | { readonly kind: "working"; readonly environmentId: EnvironmentId; readonly startedAt: string }
13 | { readonly kind: "syncing"; readonly label: string }
14 | { readonly kind: "compacting" }
15 | {
16 readonly kind: "child-input";
17 readonly label: string;
18 readonly accessibilityLabel: string;
19 readonly onPress: () => void;
20 }
21 | {
22 readonly kind: "background";
23 readonly label: string;
24 readonly accessibilityLabel: string;
25 readonly waiting: boolean;
26 readonly onPress: () => void;
27 readonly action: FloatingStatusAction;
28 }
29 | {
30 readonly kind: "lifecycle";
31 readonly state: "snoozed" | "settled" | "woke";
32 readonly label: string;
33 readonly action: FloatingStatusAction;
34 }
35 | {
36 readonly kind: "stop";
37 readonly label: string;
38 readonly unresponsive: boolean;
39 readonly action: FloatingStatusAction | null;
40 }
41 | { readonly kind: "goal"; readonly label: string; readonly accessibilityLabel: string }
42 | { readonly kind: "extension"; readonly label: string; readonly onPress: () => void }
43 | { readonly kind: "preparing"; readonly label: string }
44 | {
45 readonly kind: "connection";
46 readonly tone: "reconnecting" | "unavailable";
47 readonly label: string;
48 readonly onPress: () => void;
49 };
50​
51export function connectionFloatingStatus(input: {
52 readonly connectionError: string | null;
53 readonly connectionState: EnvironmentConnectionPhase;
54 readonly connectionAwaitingApproval?: boolean;
55 readonly environmentLabel: string | null;
56 readonly onReconnect: () => void;
57}): FloatingWorkingStatus | null {
58 const environmentLabel = input.environmentLabel ?? "Environment";
59 const unavailable = (label: string): FloatingWorkingStatus => ({
60 kind: "connection",
61 tone: "unavailable",
62 label,
63 onPress: input.onReconnect,
64 });
65​
66 if (input.connectionState === "reconnecting" && input.connectionAwaitingApproval) {
67 return {
68 kind: "connection",
69 tone: "reconnecting",
70 label: `Waiting for ${environmentLabel} to approve this device`,
71 onPress: input.onReconnect,
72 };
73 }
74 switch (input.connectionState) {
75 case "connecting":
76 case "reconnecting":
77 return {
78 kind: "connection",
79 tone: "reconnecting",
80 label:
81 input.connectionError === null
82 ? `Reconnecting to ${environmentLabel}...`
83 : `Failed to connect. Retrying ${environmentLabel}...`,
84 onPress: input.onReconnect,
85 };
86 case "offline":
87 return unavailable("You are offline");
88 case "unsupported":
89 return unavailable("Client not supported");
90 case "error":
91 return unavailable(
92 input.connectionError
93 ? `Failed to connect to ${environmentLabel}: ${input.connectionError}`
94 : `Failed to connect to ${environmentLabel}`,
95 );
96 case "available":
97 return unavailable(`${environmentLabel} is not connected`);
98 case "connected":
99 return null;
100 }