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

floating-working-status.ts

90 lines2.8 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: "preparing"; readonly label: string }
42 | {
43 readonly kind: "connection";
44 readonly tone: "reconnecting" | "unavailable";
45 readonly label: string;
46 readonly onPress: () => void;
47 };
48​
49export function connectionFloatingStatus(input: {
50 readonly connectionError: string | null;
51 readonly connectionState: EnvironmentConnectionPhase;
52 readonly environmentLabel: string | null;
53 readonly onReconnect: () => void;
54}): FloatingWorkingStatus | null {
55 const environmentLabel = input.environmentLabel ?? "Environment";
56 const unavailable = (label: string): FloatingWorkingStatus => ({
57 kind: "connection",
58 tone: "unavailable",
59 label,
60 onPress: input.onReconnect,
61 });
62​
63 switch (input.connectionState) {
64 case "connecting":
65 case "reconnecting":
66 return {
67 kind: "connection",
68 tone: "reconnecting",
69 label:
70 input.connectionError === null
71 ? `Reconnecting to ${environmentLabel}...`
72 : `Failed to connect. Retrying ${environmentLabel}...`,
73 onPress: input.onReconnect,
74 };
75 case "offline":
76 return unavailable("You are offline");
77 case "unsupported":
78 return unavailable("Client not supported");
79 case "error":
80 return unavailable(
81 input.connectionError
82 ? `Failed to connect to ${environmentLabel}: ${input.connectionError}`
83 : `Failed to connect to ${environmentLabel}`,
84 );
85 case "available":
86 return unavailable(`${environmentLabel} is not connected`);
87 case "connected":
88 return null;
89 }