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

floating-working-status.ts

91 lines2.9 KB

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