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

floating-working-status.ts

77 lines2.8 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 // The turn settled while background work it started still runs. `waiting`
13 // is false when only commands remain, such as a dev server: the agent is done.
14 | {
15 readonly kind: "background";
16 readonly label: string;
17 readonly accessibilityLabel: string;
18 readonly waiting: boolean;
19 }
20 // A native /goal on an idle thread: paused, blocked, complete, or set.
21 | { readonly kind: "goal"; readonly label: string; readonly accessibilityLabel: string }
22 // A task whose thread the server has not created yet: the worktree may
23 // still be checking out, so there is no turn to time.
24 | { readonly kind: "preparing"; readonly label: string }
25 | {
26 readonly kind: "connection";
27 readonly tone: "reconnecting" | "unavailable";
28 readonly label: string;
29 readonly onPress: () => void;
30 };
31​
32/**
33 * The pill's connection variant, or null once the environment is connected and
34 * the pill is free to report sync and working state instead.
35 */
36export function connectionFloatingStatus(input: {
37 readonly connectionError: string | null;
38 readonly connectionState: EnvironmentConnectionPhase;
39 readonly environmentLabel: string | null;
40 readonly onReconnect: () => void;
41}): FloatingWorkingStatus | null {
42 const environmentLabel = input.environmentLabel ?? "Environment";
43 const unavailable = (label: string): FloatingWorkingStatus => ({
44 kind: "connection",
45 tone: "unavailable",
46 label,
47 onPress: input.onReconnect,
48 });
49​
50 switch (input.connectionState) {
51 case "connecting":
52 case "reconnecting":
53 return {
54 kind: "connection",
55 tone: "reconnecting",
56 label:
57 input.connectionError === null
58 ? `Reconnecting to ${environmentLabel}...`
59 : `Failed to connect. Retrying ${environmentLabel}...`,
60 onPress: input.onReconnect,
61 };
62 case "offline":
63 return unavailable("You are offline");
64 case "unsupported":
65 return unavailable("Client not supported");
66 case "error":
67 return unavailable(
68 input.connectionError
69 ? `Failed to connect to ${environmentLabel}: ${input.connectionError}`
70 : `Failed to connect to ${environmentLabel}`,
71 );
72 case "available":
73 return unavailable(`${environmentLabel} is not connected`);
74 case "connected":
75 return null;
76 }