The best way to control Claude Code, Codex and more from any computer or phone
| 1 | import type { EnvironmentId, ProjectId } from "@t3tools/contracts"; |
| 2 | import { useRef, useState } from "react"; |
| 3 | |
| 4 | import { useStableCallback } from "~/hooks/useStableCallback"; |
| 5 | import { pullRequestEnvironment } from "~/state/pullRequests"; |
| 6 | import { useEnvironmentQuery } from "~/state/query"; |
| 7 | |
| 8 | import { pullRequestPanelReference } from "./usePullRequestDetail"; |
| 9 | |
| 10 | const INTENT_DELAY_MS = 150; |
| 11 | |
| 12 | interface PullRequestIntentTarget { |
| 13 | readonly environmentId: EnvironmentId; |
| 14 | readonly projectId: ProjectId; |
| 15 | readonly host: string; |
| 16 | readonly repository: string; |
| 17 | readonly number: number; |
| 18 | } |
| 19 | |
| 20 | /** |
| 21 | * Reads the detail and activity a row's panel will ask for once the pointer or focus rests on it, |
| 22 | * so the click paints from them. Passing over rows asks the host nothing, and leaving a row stops |
| 23 | * reading it. |
| 24 | */ |
| 25 | export function usePullRequestIntentPrefetch( |
| 26 | isListed: (target: PullRequestIntentTarget) => boolean, |
| 27 | ) { |
| 28 | const [target, setTarget] = useState<PullRequestIntentTarget | null>(null); |
| 29 | const timer = useRef<ReturnType<typeof setTimeout> | undefined>(undefined); |
| 30 | // A removed row fires no leave event. |
| 31 | const listed = target !== null && isListed(target); |
| 32 | if (target !== null && !listed) setTarget(null); |
| 33 | const reference = |
| 34 | target === null || !listed |
| 35 | ? null |
| 36 | : pullRequestPanelReference({ ...target, host: target.host.toLowerCase() }); |
| 37 | useEnvironmentQuery( |
| 38 | target === null || reference === null |
| 39 | ? null |
| 40 | : pullRequestEnvironment.detail({ |
| 41 | environmentId: target.environmentId, |
| 42 | input: { ...reference, allowStale: false }, |
| 43 | }), |
| 44 | ); |
| 45 | useEnvironmentQuery( |
| 46 | target === null || reference === null |
| 47 | ? null |
| 48 | : pullRequestEnvironment.activity({ environmentId: target.environmentId, input: reference }), |
| 49 | ); |
| 50 | return useStableCallback((next: PullRequestIntentTarget | null) => { |
| 51 | clearTimeout(timer.current); |
| 52 | if (next === null) { |
| 53 | setTarget(null); |
| 54 | return; |
| 55 | } |
| 56 | timer.current = setTimeout(() => setTarget(next), INTENT_DELAY_MS); |
| 57 | }); |
| 58 | } |