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

floating-working-status.ts

65 lines2.3 KB

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 // A task whose thread the server has not created yet: the worktree may
13 // still be checking out, so there is no turn to time.
14 | { readonly kind: "preparing"; readonly label: string }
15 | {
16 readonly kind: "connection";
17 readonly tone: "reconnecting" | "unavailable";
18 readonly label: string;
19 readonly onPress: () => void;
20 };
21​
22/**
23 * The pill's connection variant, or null once the environment is connected and
24 * the pill is free to report sync and working state instead.
25 */
26export function connectionFloatingStatus(input: {
27 readonly connectionError: string | null;
28 readonly connectionState: EnvironmentConnectionPhase;
29 readonly environmentLabel: string | null;
30 readonly onReconnect: () => void;
31}): FloatingWorkingStatus | null {
32 const environmentLabel = input.environmentLabel ?? "Environment";
33 const unavailable = (label: string): FloatingWorkingStatus => ({
34 kind: "connection",
35 tone: "unavailable",
36 label,
37 onPress: input.onReconnect,
38 });
39​
40 switch (input.connectionState) {
41 case "connecting":
42 case "reconnecting":
43 return {
44 kind: "connection",
45 tone: "reconnecting",
46 label:
47 input.connectionError === null
48 ? `Reconnecting to ${environmentLabel}...`
49 : `Failed to connect. Retrying ${environmentLabel}...`,
50 onPress: input.onReconnect,
51 };
52 case "offline":
53 return unavailable("You are offline");
54 case "error":
55 return unavailable(
56 input.connectionError
57 ? `Failed to connect to ${environmentLabel}: ${input.connectionError}`
58 : `Failed to connect to ${environmentLabel}`,
59 );
60 case "available":
61 return unavailable(`${environmentLabel} is not connected`);
62 case "connected":
63 return null;
64 }