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

use-thread-pr.ts

67 lines2.1 KB

1import type { EnvironmentThreadShell } from "@t3tools/client-runtime/state/shell";
2import type { VcsStatusResult } from "@t3tools/contracts";
3import { resolveChangeRequestPresentation } from "@t3tools/shared/sourceControl";
4​
5import { useEnvironmentQuery } from "./query";
6import { vcsEnvironment } from "./vcs";
7​
8export type ThreadPr = NonNullable<VcsStatusResult["pr"]>;
9​
10export interface ThreadPrPresentation {
11 readonly number: number;
12 readonly state: ThreadPr["state"];
13 readonly url: string;
14 /** Compact chip label, e.g. "PR open" / "MR merged". */
15 readonly label: string;
16 readonly textClassName: string;
18​
19const PR_STATE_TEXT_CLASS: Record<ThreadPr["state"], string> = {
20 open: "text-emerald-600 dark:text-emerald-400",
21 merged: "text-violet-600 dark:text-violet-400",
22 closed: "text-zinc-500 dark:text-zinc-400",
23};
24​
25export function presentThreadPr(
26 pr: ThreadPr,
27 provider: VcsStatusResult["sourceControlProvider"] | null | undefined,
28): ThreadPrPresentation {
29 const shortName = resolveChangeRequestPresentation(provider).shortName;
30 return {
31 number: pr.number,
32 state: pr.state,
33 url: pr.url,
34 label: `${shortName} ${pr.state}`,
35 textClassName: PR_STATE_TEXT_CLASS[pr.state],
36 };
38​
39/**
40 * Live PR status for a thread's branch. Subscriptions are deduplicated per
41 * (environmentId, cwd) by the atom family, so many rows on the same worktree
42 * or project root share one stream — and virtualization means only visible
43 * rows subscribe at all.
44 */
45export function useThreadPr(
46 thread: EnvironmentThreadShell,
47 projectCwd: string | null,
48): ThreadPrPresentation | null {
49 const cwd = thread.worktreePath ?? projectCwd;
50 const gitStatus = useEnvironmentQuery(
51 thread.branch !== null && cwd !== null
52 ? vcsEnvironment.status({
53 environmentId: thread.environmentId,
54 input: { cwd },
55 })
56 : null,
57 );
58​
59 const status = gitStatus.data;
60 if (status === null || thread.branch === null || status.refName !== thread.branch) {
61 return null;
62 }
63 if (!status.pr) {
64 return null;
65 }
66 return presentThreadPr(status.pr, status.sourceControlProvider);