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

use-vcs-action-state.ts

166 lines4.9 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 type { ScopedThreadRef } from "@t3tools/contracts";
9import { Atom } from "effect/reactivity";
10import * as Haptics from "expo-haptics";
11import { msg, useMessages } from "gt-react-native";
12import { useSyncExternalStore } from "react";
13​
14import type { MessageResolver } from "../i18n/messages";
15import { appAtomRegistry } from "./atom-registry";
16import { vcsActionManager } from "./vcs";
17​
18export function useVcsActionState(target: VcsActionTarget): VcsActionState {
19 return useAtomValue(vcsActionManager.stateAtom(target));
21​
22interface RepositoryGitAction {
23 readonly threadRef: ScopedThreadRef;
24 readonly cwd: string;
26​
27// The tracked repository a thread last ran an action in, so the thread can show its progress.
28const repositoryGitActionAtom = Atom.make<RepositoryGitAction | null>(null).pipe(
29 Atom.keepAlive,
30 Atom.withLabel("mobile:repository-git-action"),
31);
32​
33export function noteRepositoryGitAction(action: RepositoryGitAction): void {
34 appAtomRegistry.set(repositoryGitActionAtom, action);
36​
37export interface GitActionResultNotification {
38 readonly type: "success" | "error";
39 readonly title: string;
40 readonly description?: string;
41 readonly prUrl?: string;
43​
44const RESULT_DISMISS_MS = 5_000;
45​
46const gitActionResultAtom = Atom.make<GitActionResultNotification | null>(null).pipe(
47 Atom.keepAlive,
48 Atom.withLabel("mobile:git-action-result"),
49);
50let dismissTimer: ReturnType<typeof setTimeout> | null = null;
51​
52function broadcast(result: GitActionResultNotification | null): void {
53 appAtomRegistry.set(gitActionResultAtom, result);
55​
56export function showGitActionResult(result: GitActionResultNotification): void {
57 // Best effort: devices without a haptics engine reject.
58 Haptics.notificationAsync(
59 result.type === "success"
60 ? Haptics.NotificationFeedbackType.Success
61 : Haptics.NotificationFeedbackType.Error,
62 ).catch(() => undefined);
63 if (dismissTimer) clearTimeout(dismissTimer);
64 broadcast(result);
65 dismissTimer = setTimeout(() => {
66 dismissTimer = null;
67 broadcast(null);
68 }, RESULT_DISMISS_MS);
70​
71export function dismissGitActionResult(): void {
72 if (dismissTimer) clearTimeout(dismissTimer);
73 dismissTimer = null;
74 broadcast(null);
76​
77function useGitActionResultNotification(): {
78 readonly result: GitActionResultNotification | null;
79 readonly dismiss: () => void;
80} {
81 const result = useAtomValue(gitActionResultAtom);
82 return { result, dismiss: dismissGitActionResult };
84​
85export type GitActionProgressPhase = "idle" | "running" | "success" | "error";
86​
87export interface GitActionProgress {
88 readonly phase: GitActionProgressPhase;
89 readonly label: string | null;
90 readonly description: string | null;
91 readonly prUrl?: string;
93​
94const subscribeNever = () => () => {};
95​
96const EMPTY_PROGRESS: GitActionProgress = {
97 phase: "idle",
98 label: null,
99 description: null,
100};
101​
102function formatElapsedSeconds(
103 ms: number | null,
104 nowMs: number,
105 resolve: MessageResolver,
106): string | null {
107 if (ms === null) return null;
108 const elapsed = Math.max(0, Math.floor((nowMs - ms) / 1000));
109 if (elapsed < 2) return null;
110 return resolve(msg("Running for {elapsed}s", { elapsed: String(elapsed) }));
112​
113/** Progress in the thread's own folder, or in the repository the thread last acted on. */
114export function useGitActionProgress(
115 target: VcsActionTarget,
116 threadRef: ScopedThreadRef | null,
117): GitActionProgress {
118 const threadFolderState = useVcsActionState(target);
119 const repositoryCwd = useAtomValue(repositoryGitActionAtom, (action) =>
120 action !== null &&
121 threadRef !== null &&
122 action.threadRef.environmentId === threadRef.environmentId &&
123 action.threadRef.threadId === threadRef.threadId
124 ? action.cwd
125 : null,
126 );
127 const repositoryState = useVcsActionState({
128 environmentId: threadRef?.environmentId ?? null,
129 cwd: repositoryCwd,
130 });
131 const actionState = repositoryState.isRunning ? repositoryState : threadFolderState;
132 const { result } = useGitActionResultNotification();
133 const resolve = useMessages();
134​
135 // Only a running action shows elapsed time, so only it subscribes to the second tick.
136 const nowMs = useSyncExternalStore(
137 actionState.isRunning ? secondTick.subscribe : subscribeNever,
138 secondTick.getSnapshot,
139 );
140​
141 if (actionState.isRunning) {
142 const description =
143 actionState.lastOutputLine ??
144 formatElapsedSeconds(
145 actionState.hookStartedAtMs ?? actionState.phaseStartedAtMs,
146 nowMs,
147 resolve,
148 );
149 return {
150 phase: "running",
151 label: vcsActionLabelText(actionState, resolve),
152 description,
153 };
154 }
155​
156 if (result) {
157 return {
158 phase: result.type,
159 label: result.title,
160 description: result.description ?? null,
161 prUrl: result.prUrl,
162 };
163 }
164​
165 return EMPTY_PROGRESS;