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