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 type { EnvironmentId, ScopedThreadRef, ThreadPullRequestLink } from "@t3tools/contracts"; |
| 2 | import { |
| 3 | isAtomCommandInterrupted, |
| 4 | squashAtomCommandFailure, |
| 5 | } from "@t3tools/client-runtime/state/runtime"; |
| 6 | import { |
| 7 | planThreadPullRequestMutation, |
| 8 | threadPullRequestLinkMode, |
| 9 | } from "@t3tools/client-runtime/thread-pull-request-compatibility"; |
| 10 | import { |
| 11 | threadPullRequestKeysEqual, |
| 12 | visibleThreadPullRequests, |
| 13 | } from "@t3tools/client-runtime/thread-pull-requests"; |
| 14 | import { sourceControlClients } from "@t3tools/client-runtime/source-control-clients"; |
| 15 | import { useAtomValue } from "@effect/atom-react"; |
| 16 | import { Atom } from "effect/reactivity"; |
| 17 | import { msg } from "gt-react"; |
| 18 | |
| 19 | import { m } from "~/i18n/messages"; |
| 20 | import { appAtomRegistry } from "~/rpc/atomRegistry"; |
| 21 | import { environmentServerConfigsAtom } from "~/state/server"; |
| 22 | import { threadEnvironment } from "~/state/threads"; |
| 23 | import { useAtomCommand } from "~/state/use-atom-command"; |
| 24 | |
| 25 | // A primitive mode prevents unchanged status pushes from re-rendering every link. |
| 26 | const linkModeAtom = Atom.family((environmentId: EnvironmentId | null) => |
| 27 | Atom.make((get) => |
| 28 | threadPullRequestLinkMode( |
| 29 | environmentId === null |
| 30 | ? undefined |
| 31 | : get(environmentServerConfigsAtom).get(environmentId)?.environment.capabilities, |
| 32 | ), |
| 33 | ).pipe(Atom.withLabel("pull-request-link-mode")), |
| 34 | ); |
| 35 | |
| 36 | export function usePullRequestLinking(environmentId: EnvironmentId | null | undefined) { |
| 37 | const mode = useAtomValue(linkModeAtom(environmentId ?? null)); |
| 38 | const link = useAtomCommand(threadEnvironment.linkPullRequest, { reportFailure: false }); |
| 39 | const unlink = useAtomCommand(threadEnvironment.unlinkPullRequest, { reportFailure: false }); |
| 40 | const canLink = (url: string) => |
| 41 | mode !== "unsupported" && sourceControlClients.parseChangeRequestUrl(url) !== null; |
| 42 | const isLinked = ( |
| 43 | thread: { readonly pullRequests?: readonly ThreadPullRequestLink[] } | null, |
| 44 | url: string, |
| 45 | ) => { |
| 46 | if (thread === null || mode === "unsupported") return false; |
| 47 | const parsed = sourceControlClients.parseChangeRequestUrl(url)?.link; |
| 48 | return ( |
| 49 | parsed !== undefined && |
| 50 | visibleThreadPullRequests(thread.pullRequests ?? []).some((entry) => |
| 51 | threadPullRequestKeysEqual(entry, parsed), |
| 52 | ) |
| 53 | ); |
| 54 | }; |
| 55 | const changeLink = async (threadRef: ScopedThreadRef, url: string, linked: boolean) => { |
| 56 | const parsed = sourceControlClients.parseChangeRequestUrl(url)?.link; |
| 57 | if ( |
| 58 | parsed === undefined || |
| 59 | threadRef.environmentId !== environmentId || |
| 60 | (linked && !canLink(url)) |
| 61 | ) |
| 62 | throw new Error(m(msg("The pull request is not available in this environment."))); |
| 63 | const mutation = planThreadPullRequestMutation({ |
| 64 | capabilities: appAtomRegistry.get(environmentServerConfigsAtom).get(threadRef.environmentId) |
| 65 | ?.environment.capabilities, |
| 66 | threadId: threadRef.threadId, |
| 67 | reference: { ...parsed, url }, |
| 68 | linked, |
| 69 | }); |
| 70 | if (mutation === null) |
| 71 | throw new Error(m(msg("This environment does not support linking this pull request."))); |
| 72 | const result = await (mutation.type === "thread.pull-request.link" |
| 73 | ? link({ environmentId: threadRef.environmentId, input: mutation.input }) |
| 74 | : unlink({ environmentId: threadRef.environmentId, input: mutation.input })); |
| 75 | if (result._tag === "Failure") { |
| 76 | if (isAtomCommandInterrupted(result)) throw new Error(m(msg("Link update interrupted."))); |
| 77 | throw squashAtomCommandFailure(result); |
| 78 | } |
| 79 | }; |
| 80 | return { mode, canLink, isLinked, changeLink }; |
| 81 | } |