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

usePullRequestLiveChecks.ts

42 lines1.9 KB

1import { useFocusEffect } from "@react-navigation/native";
2import {
3 pullRequestChecksRefreshIntervalMs,
4 resolvePullRequestDetailChecks,
5} from "@t3tools/client-runtime/pull-request-checks";
6import type { PullRequestDetail } from "@t3tools/contracts";
7import { useEnvironmentServerConfig } from "../../state/entities";
8import { composerPullRequests } from "../../state/pull-requests";
9import { useEnvironmentQuery } from "../../state/query";
10import { usePullRequestConnected } from "./usePullRequestConnected";
11import { pullRequestTarget, type PullRequestParams } from "./usePullRequestDiff";
12​
13/**
14 * Re-reads only the checks while an open change request is on screen, so a running CI shows up
15 * without paying for a full detail read every poll.
16 */
17export function usePullRequestLiveChecks(
18 params: PullRequestParams,
19 detail: PullRequestDetail | null,
20): PullRequestDetail | null {
21 const target = pullRequestTarget(params);
22 const connected = usePullRequestConnected(target.environmentId);
23 const supported =
24 useEnvironmentServerConfig(target.environmentId)?.environment.capabilities.pullRequestChecks ===
25 true;
26 const checks = useEnvironmentQuery(
27 supported && detail !== null ? composerPullRequests.checks(target) : null,
28 );
29 // Both reads carry the server's observedAt; without it the poll is the fresher stream.
30 const live = resolvePullRequestDetailChecks(detail, checks.data, true);
31 // A failed read keeps polling; only a host that answers "no checks" (null) stops it.
32 const enabled =
33 connected && supported && live?.state === "open" && !(checks.isSuccess && checks.data === null);
34 const intervalMs = pullRequestChecksRefreshIntervalMs(live?.checks ?? []);
35 const { refresh } = checks;
36 useFocusEffect(() => {
37 if (!enabled) return;
38 const timer = setInterval(refresh, intervalMs);
39 return () => clearInterval(timer);
40 });
41 return live;