Open History to see the last commit
1import type { EnvironmentConnectionPhase } from "@t3tools/client-runtime/connection";
2
3/**
4 * What the floating pill says. Connection, syncing, and working share one
5 * element so the label swaps in place instead of one pill fading out for
6 * another. The connection variant is tappable and triggers a reconnect.
7 */
8export type FloatingWorkingStatus =
9 | { readonly kind: "working"; readonly startedAt: string }
10 | { readonly kind: "syncing"; readonly label: string }
11 | { readonly kind: "compacting" }
12 // The turn settled while background work it started still runs.
13 | { readonly kind: "waiting"; readonly label: string; readonly accessibilityLabel: string }
14 // A task whose thread the server has not created yet: the worktree may
15 // still be checking out, so there is no turn to time.
16 | { readonly kind: "preparing"; readonly label: string }
17 | {
18 readonly kind: "connection";
19 readonly tone: "reconnecting" | "unavailable";
20 readonly label: string;
21 readonly onPress: () => void;
22 };
23
24/**
25 * The pill's connection variant, or null once the environment is connected and
26 * the pill is free to report sync and working state instead.
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 }
69}