The best way to control Claude Code, Codex and more from any computer or phone
Mirrored from https://github.com/Camo67/t3code.git
Synced Nov 21, 04:45 UTC
| 1 | import { threadPullRequestLinkMode } from "@t3tools/client-runtime/thread-pull-request-compatibility"; |
| 2 | import type { PullRequestDetail } from "@t3tools/contracts"; |
| 3 | import { sourceControlClients } from "@t3tools/client-runtime/source-control-clients"; |
| 4 | import { useAtomValue } from "@effect/atom-react"; |
| 5 | import { Atom } from "effect/reactivity"; |
| 6 | import { useMemo } from "react"; |
| 7 | import { useEnvironmentServerConfig, useNavigationThreadShells } from "../../state/entities"; |
| 8 | import { pullRequestStackAtom } from "../../state/pull-requests"; |
| 9 | import { useEnvironmentQuery } from "../../state/query"; |
| 10 | import { savedPullRequestStack } from "./pullRequestStack"; |
| 11 | import { pullRequestTarget, type PullRequestParams } from "./usePullRequestDiff"; |
| 12 | |
| 13 | const NOT_ANSWERED = Atom.make(false); |
| 14 | |
| 15 | export function usePullRequestStack(params: PullRequestParams, pr: PullRequestDetail | null) { |
| 16 | const target = pullRequestTarget(params); |
| 17 | const config = useEnvironmentServerConfig(target.environmentId); |
| 18 | const threads = useNavigationThreadShells(); |
| 19 | const supported = |
| 20 | threadPullRequestLinkMode(config?.environment.capabilities) === "multiple" && |
| 21 | pr?.capabilities.stacks === true; |
| 22 | const reference = useMemo( |
| 23 | () => ({ |
| 24 | ...pullRequestTarget(params).input, |
| 25 | host: |
| 26 | params.host ?? |
| 27 | (pr ? sourceControlClients.parseChangeRequestUrl(pr.url)?.link.host : undefined), |
| 28 | }), |
| 29 | [params, pr], |
| 30 | ); |
| 31 | const saved = useMemo( |
| 32 | () => |
| 33 | savedPullRequestStack( |
| 34 | threads |
| 35 | .filter((thread) => thread.environmentId === target.environmentId) |
| 36 | .flatMap((thread) => thread.pullRequests ?? []), |
| 37 | reference, |
| 38 | ), |
| 39 | [threads, target.environmentId, reference], |
| 40 | ); |
| 41 | const query = useEnvironmentQuery( |
| 42 | supported ? pullRequestStackAtom({ ...target, input: reference }) : null, |
| 43 | ); |
| 44 | const answered = useAtomValue( |
| 45 | supported ? pullRequestStackAtom.answered({ ...target, input: reference }) : NOT_ANSWERED, |
| 46 | ); |
| 47 | const stack = query.isSuccess ? query.data : (query.data ?? saved); |
| 48 | return { |
| 49 | ...query, |
| 50 | stack, |
| 51 | isFresh: supported && query.isSuccess && !query.isPending && answered, |
| 52 | supportsStackActions: supported && pr?.capabilities.stackActions === true, |
| 53 | }; |
| 54 | } |
| 55 | |
| 56 | export type PullRequestStackState = ReturnType<typeof usePullRequestStack>; |