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

use-thread-pr.ts

71 lines2.3 KB

1import type { EnvironmentThreadShell } from "@t3tools/client-runtime/state/shell";
2import {
3 createLinkedPullRequestDetailAtomFamily,
4 pullRequestDetailToVcsStatus,
5} from "@t3tools/client-runtime/state/pull-requests";
6​
7import { connectionAtomRuntime } from "../connection/runtime";
8import { useEnvironmentQuery } from "./query";
9import { presentThreadPr, type ThreadPrPresentation } from "./thread-pr-presentation";
10import { vcsEnvironment } from "./vcs";
11​
12const linkedPullRequestDetailAtom = createLinkedPullRequestDetailAtomFamily(connectionAtomRuntime);
13​
14export {
15 presentThreadPr,
16 type ThreadPr,
17 type ThreadPrPresentation,
18} from "./thread-pr-presentation";
19​
20/**
21 * Live PR status for a thread's branch. Subscriptions are deduplicated per
22 * (environmentId, cwd) by the atom family, so many rows on the same worktree
23 * or project root share one stream — and virtualization means only visible
24 * rows subscribe at all.
25 */
26export function useThreadPr(
27 thread: EnvironmentThreadShell,
28 projectCwd: string | null,
29): ThreadPrPresentation | null {
30 const cwd = thread.worktreePath ?? projectCwd;
31 const gitStatus = useEnvironmentQuery(
32 thread.linkedPullRequest == null && thread.branch !== null && cwd !== null
33 ? vcsEnvironment.status({
34 environmentId: thread.environmentId,
35 input: { cwd },
36 })
37 : null,
38 );
39 const linkedPullRequest = useEnvironmentQuery(
40 thread.linkedPullRequest == null
41 ? null
42 : linkedPullRequestDetailAtom({
43 environmentId: thread.environmentId,
44 input: {
45 projectId: thread.linkedPullRequest.projectId,
46 repository: thread.linkedPullRequest.repository,
47 number: thread.linkedPullRequest.number,
48 },
49 }),
50 );
51​
52 if (thread.linkedPullRequest != null) {
53 const detail = linkedPullRequest.data;
54 return detail === null
55 ? null
56 : presentThreadPr(pullRequestDetailToVcsStatus(detail), {
57 kind: detail.provider,
58 name: detail.provider,
59 baseUrl: "",
60 });
61 }
62​
63 const status = gitStatus.data;
64 if (status === null || thread.branch === null || status.refName !== thread.branch) {
65 return null;
66 }
67 if (!status.pr) {
68 return null;
69 }
70 return presentThreadPr(status.pr, status.sourceControlProvider);