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

floating-working-status.ts

69 lines2.2 KB

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