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

use-vcs-action-state.ts

133 lines3.8 KB

1import { useAtomValue } from "@effect/atom-react";
2import { secondTick } from "@t3tools/client-runtime/clock";
3import {
4 vcsActionLabelText,
5 type VcsActionState,
6 type VcsActionTarget,
7} from "@t3tools/client-runtime/state/vcs";
8import { Atom } from "effect/reactivity";
9import * as Haptics from "expo-haptics";
10import { msg, useMessages } from "gt-react-native";
11import { useSyncExternalStore } from "react";
12​
13import type { MessageResolver } from "../i18n/messages";
14import { appAtomRegistry } from "./atom-registry";
15import { vcsActionManager } from "./vcs";
16​
17export function useVcsActionState(target: VcsActionTarget): VcsActionState {
18 return useAtomValue(vcsActionManager.stateAtom(target));
20​
21export interface GitActionResultNotification {
22 readonly type: "success" | "error";
23 readonly title: string;
24 readonly description?: string;
25 readonly prUrl?: string;
27​
28const RESULT_DISMISS_MS = 5_000;
29​
30const gitActionResultAtom = Atom.make<GitActionResultNotification | null>(null).pipe(
31 Atom.keepAlive,
32 Atom.withLabel("mobile:git-action-result"),
33);
34let dismissTimer: ReturnType<typeof setTimeout> | null = null;
35​
36function broadcast(result: GitActionResultNotification | null): void {
37 appAtomRegistry.set(gitActionResultAtom, result);
39​
40export 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);
54​
55export function dismissGitActionResult(): void {
56 if (dismissTimer) clearTimeout(dismissTimer);
57 dismissTimer = null;
58 broadcast(null);
60​
61function useGitActionResultNotification(): {
62 readonly result: GitActionResultNotification | null;
63 readonly dismiss: () => void;
64} {
65 const result = useAtomValue(gitActionResultAtom);
66 return { result, dismiss: dismissGitActionResult };
68​
69export type GitActionProgressPhase = "idle" | "running" | "success" | "error";
70​
71export interface GitActionProgress {
72 readonly phase: GitActionProgressPhase;
73 readonly label: string | null;
74 readonly description: string | null;
75 readonly prUrl?: string;
77​
78const subscribeNever = () => () => {};
79​
80const EMPTY_PROGRESS: GitActionProgress = {
81 phase: "idle",
82 label: null,
83 description: null,
84};
85​
86function 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) }));
96​
97export 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;