The best way to control Claude Code, Codex and more from any computer or phone

main
ts 90 lines 3.27 KB
Raw
1 import type { UsageProviderKind, UsageSummary } from "@t3tools/contracts";
2 import { mFallback, msg } from "gt-i18n";
3 import type { MFunctionType } from "gt-i18n/types";
4
5 interface UsageProgressEnvironment {
6 readonly label: string;
7 readonly isConnected: boolean;
8 readonly isPending: boolean;
9 readonly error: string | null;
10 readonly summary: UsageSummary | null;
11 }
12
13 export function usageEnvironmentProgress(
14 environment: UsageProgressEnvironment,
15 refreshing = false,
16 ) {
17 const { summary } = environment;
18 if (!environment.isConnected || environment.error !== null) return { phase: "inactive" } as const;
19 if (summary === null) return { phase: "loading" } as const;
20 if (refreshing) return { phase: "stale" } as const;
21 if (!environment.isPending) return { phase: "ready" } as const;
22 const providers = [
23 ...new Set(
24 summary.sources.flatMap((source) => (source.refreshing ? [source.fingerprint.provider] : [])),
25 ),
26 ];
27 return providers.length > 0
28 ? ({ phase: "partway", providers } as const)
29 : ({ phase: "stale" } as const);
30 }
31
32 export function updatingProvidersLabel(
33 providers: readonly UsageProviderKind[],
34 providerLabel: (provider: UsageProviderKind) => string,
35 resolve: MFunctionType = mFallback,
36 ) {
37 return providers.length === 1
38 ? resolve(msg("Updating {provider}…", { provider: providerLabel(providers[0]!) }))
39 : resolve(msg("Updating {count} providers…", { count: providers.length }));
40 }
41
42 export function usageProgress(
43 environments: readonly UsageProgressEnvironment[],
44 {
45 refreshing = false,
46 providerLabel,
47 }: {
48 readonly refreshing?: boolean;
49 readonly providerLabel: (provider: UsageProviderKind) => string;
50 },
51 resolve: MFunctionType = mFallback,
52 ) {
53 const progress = environments.map((environment) => ({
54 label: environment.label,
55 ...usageEnvironmentProgress(environment, refreshing),
56 }));
57 const waiting = progress.filter(({ phase }) => phase === "loading" || phase === "stale");
58 const providers = [
59 ...new Set(progress.flatMap((entry) => (entry.phase === "partway" ? entry.providers : []))),
60 ];
61 const answered = progress.some(({ phase }) => phase === "partway" || phase === "ready");
62 return {
63 dimmed: !answered && progress.some(({ phase }) => phase === "stale"),
64 label:
65 waiting.length === 0
66 ? providers.length === 0
67 ? null
68 : updatingProvidersLabel(providers, providerLabel, resolve)
69 : environments.length === 1
70 ? resolve(msg("Updating…"))
71 : waiting.length === 1
72 ? resolve(msg("Updating {environment}…", { environment: waiting[0]!.label }))
73 : resolve(msg("Updating {count} environments…", { count: waiting.length })),
74 };
75 }
76
77 export function usageLoadingState(
78 environments: readonly UsageProgressEnvironment[],
79 refreshing = false,
80 ) {
81 const providers = new Set<UsageProviderKind>();
82 let everyProvider = false;
83 for (const environment of environments) {
84 const progress = usageEnvironmentProgress(environment, refreshing);
85 if (progress.phase === "loading" || progress.phase === "stale") everyProvider = true;
86 if (progress.phase === "partway")
87 for (const provider of progress.providers) providers.add(provider);
88 }
89 return { partial: everyProvider || providers.size > 0, everyProvider, providers };
90 }