@devries-cameron20 / t3code Mirror · read-only
Fork 0

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

Mirrored from https://github.com/Camo67/t3code.git

Synced Nov 21, 04:45 UTC

main
ts 133 lines 3.78 KB
Raw
1 import { useAtomValue } from "@effect/atom-react";
2 import { secondTick } from "@t3tools/client-runtime/clock";
3 import {
4 vcsActionLabelText,
5 type VcsActionState,
6 type VcsActionTarget,
7 } from "@t3tools/client-runtime/state/vcs";
8 import { Atom } from "effect/reactivity";
9 import * as Haptics from "expo-haptics";
10 import { msg, useMessages } from "gt-react-native";
11 import { useSyncExternalStore } from "react";
12
13 import type { MessageResolver } from "../i18n/messages";
14 import { appAtomRegistry } from "./atom-registry";
15 import { vcsActionManager } from "./vcs";
16
17 export function useVcsActionState(target: VcsActionTarget): VcsActionState {
18 return useAtomValue(vcsActionManager.stateAtom(target));
19 }
20
21 export interface GitActionResultNotification {
22 readonly type: "success" | "error";
23 readonly title: string;
24 readonly description?: string;
25 readonly prUrl?: string;
26 }
27
28 const RESULT_DISMISS_MS = 5_000;
29
30 const gitActionResultAtom = Atom.make<GitActionResultNotification | null>(null).pipe(
31 Atom.keepAlive,
32 Atom.withLabel("mobile:git-action-result"),
33 );
34 let dismissTimer: ReturnType<typeof setTimeout> | null = null;
35
36 function broadcast(result: GitActionResultNotification | null): void {
37 appAtomRegistry.set(gitActionResultAtom, result);
38 }
39
40 export function showGitActionResult(result: GitActionResultNotification): void {
41 // Best effort: devices without a haptics engine reject.
42 Haptics.notificationAsync(
43 result.type === "success"
44 ? Haptics.NotificationFeedbackType.Success
45 : Haptics.NotificationFeedbackType.Error,
46 ).catch(() => undefined);
47 if (dismissTimer) clearTimeout(dismissTimer);
48 broadcast(result);
49 dismissTimer = setTimeout(() => {
50 dismissTimer = null;
51 broadcast(null);
52 }, RESULT_DISMISS_MS);
53 }
54
55 export function dismissGitActionResult(): void {
56 if (dismissTimer) clearTimeout(dismissTimer);
57 dismissTimer = null;
58 broadcast(null);
59 }
60
61 function useGitActionResultNotification(): {
62 readonly result: GitActionResultNotification | null;
63 readonly dismiss: () => void;
64 } {
65 const result = useAtomValue(gitActionResultAtom);
66 return { result, dismiss: dismissGitActionResult };
67 }
68
69 export type GitActionProgressPhase = "idle" | "running" | "success" | "error";
70
71 export interface GitActionProgress {
72 readonly phase: GitActionProgressPhase;
73 readonly label: string | null;
74 readonly description: string | null;
75 readonly prUrl?: string;
76 }
77
78 const subscribeNever = () => () => {};
79
80 const EMPTY_PROGRESS: GitActionProgress = {
81 phase: "idle",
82 label: null,
83 description: null,
84 };
85
86 function formatElapsedSeconds(
87 ms: number | null,
88 nowMs: number,
89 resolve: MessageResolver,
90 ): string | null {
91 if (ms === null) return null;
92 const elapsed = Math.max(0, Math.floor((nowMs - ms) / 1000));
93 if (elapsed < 2) return null;
94 return resolve(msg("Running for {elapsed}s", { elapsed: String(elapsed) }));
95 }
96
97 export function useGitActionProgress(target: VcsActionTarget): GitActionProgress {
98 const actionState = useVcsActionState(target);
99 const { result } = useGitActionResultNotification();
100 const resolve = useMessages();
101
102 // Only a running action shows elapsed time, so only it subscribes to the second tick.
103 const nowMs = useSyncExternalStore(
104 actionState.isRunning ? secondTick.subscribe : subscribeNever,
105 secondTick.getSnapshot,
106 );
107
108 if (actionState.isRunning) {
109 const description =
110 actionState.lastOutputLine ??
111 formatElapsedSeconds(
112 actionState.hookStartedAtMs ?? actionState.phaseStartedAtMs,
113 nowMs,
114 resolve,
115 );
116 return {
117 phase: "running",
118 label: vcsActionLabelText(actionState, resolve),
119 description,
120 };
121 }
122
123 if (result) {
124 return {
125 phase: result.type,
126 label: result.title,
127 description: result.description ?? null,
128 prUrl: result.prUrl,
129 };
130 }
131
132 return EMPTY_PROGRESS;
133 }