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

use-vcs-action-state.ts

111 lines3.3 KB

1import { useAtomValue } from "@effect/atom-react";
2import { type VcsActionState, type VcsActionTarget } from "@t3tools/client-runtime/state/vcs";
3import { Atom } from "effect/reactivity";
4import { useEffect, useState } from "react";
5​
6import { appAtomRegistry } from "./atom-registry";
7import { vcsActionManager } from "./vcs";
8​
9export function useVcsActionState(target: VcsActionTarget): VcsActionState {
10 return useAtomValue(vcsActionManager.stateAtom(target));
12​
13export interface GitActionResultNotification {
14 readonly type: "success" | "error";
15 readonly title: string;
16 readonly description?: string;
17 readonly prUrl?: string;
19​
20const RESULT_DISMISS_MS = 5_000;
21​
22const gitActionResultAtom = Atom.make<GitActionResultNotification | null>(null).pipe(
23 Atom.keepAlive,
24 Atom.withLabel("mobile:git-action-result"),
25);
26let dismissTimer: ReturnType<typeof setTimeout> | null = null;
27​
28function broadcast(result: GitActionResultNotification | null): void {
29 appAtomRegistry.set(gitActionResultAtom, result);
31​
32export function showGitActionResult(result: GitActionResultNotification): void {
33 if (dismissTimer) clearTimeout(dismissTimer);
34 broadcast(result);
35 dismissTimer = setTimeout(() => {
36 dismissTimer = null;
37 broadcast(null);
38 }, RESULT_DISMISS_MS);
40​
41export function dismissGitActionResult(): void {
42 if (dismissTimer) clearTimeout(dismissTimer);
43 dismissTimer = null;
44 broadcast(null);
46​
47export function useGitActionResultNotification(): {
48 readonly result: GitActionResultNotification | null;
49 readonly dismiss: () => void;
50} {
51 const result = useAtomValue(gitActionResultAtom);
52 return { result, dismiss: dismissGitActionResult };
54​
55export type GitActionProgressPhase = "idle" | "running" | "success" | "error";
56​
57export interface GitActionProgress {
58 readonly phase: GitActionProgressPhase;
59 readonly label: string | null;
60 readonly description: string | null;
61 readonly prUrl?: string;
63​
64const EMPTY_PROGRESS: GitActionProgress = {
65 phase: "idle",
66 label: null,
67 description: null,
68};
69​
70function formatElapsedSeconds(ms: number | null, nowMs: number): string | null {
71 if (ms === null) return null;
72 const elapsed = Math.max(0, Math.floor((nowMs - ms) / 1000));
73 if (elapsed < 2) return null;
74 return `Running for ${elapsed}s`;
76​
77export function useGitActionProgress(target: VcsActionTarget): GitActionProgress {
78 const actionState = useVcsActionState(target);
79 const { result } = useGitActionResultNotification();
80​
81 // Keep clock state; the compiler caches render-time Date.now() and would freeze the label.
82 const [nowMs, setNowMs] = useState(() => Date.now());
83 useEffect(() => {
84 if (!actionState.isRunning) return;
85 setNowMs(Date.now());
86 const interval = setInterval(() => setNowMs(Date.now()), 1000);
87 return () => clearInterval(interval);
88 }, [actionState.isRunning]);
89​
90 if (actionState.isRunning) {
91 const description =
92 actionState.lastOutputLine ??
93 formatElapsedSeconds(actionState.hookStartedAtMs ?? actionState.phaseStartedAtMs, nowMs);
94 return {
95 phase: "running",
96 label: actionState.currentLabel,
97 description,
98 };
99 }
100​
101 if (result) {
102 return {
103 phase: result.type,
104 label: result.title,
105 description: result.description ?? null,
106 prUrl: result.prUrl,
107 };
108 }
109​
110 return EMPTY_PROGRESS;