The best way to control Claude Code, Codex and more from any computer or phone
| 1 | import { useFocusEffect } from "@react-navigation/native"; |
| 2 | import { |
| 3 | pullRequestChecksRefreshIntervalMs, |
| 4 | resolvePullRequestDetailChecks, |
| 5 | } from "@t3tools/client-runtime/pull-request-checks"; |
| 6 | import type { PullRequestDetail } from "@t3tools/contracts"; |
| 7 | import { useEnvironmentServerConfig } from "../../state/entities"; |
| 8 | import { composerPullRequests } from "../../state/pull-requests"; |
| 9 | import { useEnvironmentQuery } from "../../state/query"; |
| 10 | import { usePullRequestConnected } from "./usePullRequestConnected"; |
| 11 | import { 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 | */ |
| 17 | export 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; |
| 42 | } |