| 1 | import { useAtomValue } from "@effect/atom-react"; |
| 2 | import { |
| 3 | createMergebotDeploymentsAtomFamily, |
| 4 | createMergebotPullRequestStatusAtomFamily, |
| 5 | createMergebotQueueAtomFamily, |
| 6 | } from "@t3tools/client-runtime/state/pull-requests"; |
| 7 | import { createEnvironmentRpcCommand } from "@t3tools/client-runtime/state/runtime"; |
| 8 | import { WS_METHODS, type EnvironmentId } from "@t3tools/contracts"; |
| 9 | import type { AsyncResult } from "effect/reactivity"; |
| 10 | import { environmentCatalog } from "../connection/catalog"; |
| 11 | import { connectionAtomRuntime } from "../connection/runtime"; |
| 12 | |
| 13 | const mergebotQueue = createMergebotQueueAtomFamily(connectionAtomRuntime); |
| 14 | const mergebotPullRequestStatus = createMergebotPullRequestStatusAtomFamily(connectionAtomRuntime); |
| 15 | const mergebotDeployments = createMergebotDeploymentsAtomFamily(connectionAtomRuntime); |
| 16 | |
| 17 | /** A live Mergebot value, or null while disconnected, loading, or failed. */ |
| 18 | function useConnectedValue<A, E>( |
| 19 | environmentId: EnvironmentId, |
| 20 | result: AsyncResult.AsyncResult<A, E>, |
| 21 | ) { |
| 22 | const connection = useAtomValue(environmentCatalog.stateAtom(environmentId)); |
| 23 | if (connection._tag !== "Success" || connection.value.phase !== "connected") return null; |
| 24 | return result._tag === "Success" ? result.value : null; |
| 25 | } |
| 26 | |
| 27 | export function useMergebotQueue(environmentId: EnvironmentId, repository: string) { |
| 28 | const result = useAtomValue(mergebotQueue({ environmentId, input: { repository } })); |
| 29 | return useConnectedValue(environmentId, result); |
| 30 | } |
| 31 | |
| 32 | export function useMergebotPullRequestStatus( |
| 33 | environmentId: EnvironmentId, |
| 34 | repository: string, |
| 35 | number: number, |
| 36 | ) { |
| 37 | const result = useAtomValue( |
| 38 | mergebotPullRequestStatus({ environmentId, input: { repository, number } }), |
| 39 | ); |
| 40 | return useConnectedValue(environmentId, result); |
| 41 | } |
| 42 | |
| 43 | export function useMergebotDeployments(environmentId: EnvironmentId, repository: string) { |
| 44 | const result = useAtomValue(mergebotDeployments({ environmentId, input: { repository } })); |
| 45 | return useConnectedValue(environmentId, result); |
| 46 | } |
| 47 | |
| 48 | export const mergebotEnvironment = { |
| 49 | openPullRequest: createEnvironmentRpcCommand(connectionAtomRuntime, { |
| 50 | label: "environment-data:mergebot:open-pull-request", |
| 51 | tag: WS_METHODS.mergebotOpenPullRequest, |
| 52 | }), |
| 53 | setAutoMerge: createEnvironmentRpcCommand(connectionAtomRuntime, { |
| 54 | label: "environment-data:mergebot:set-auto-merge", |
| 55 | tag: WS_METHODS.mergebotSetAutoMerge, |
| 56 | }), |
| 57 | updatePullRequest: createEnvironmentRpcCommand(connectionAtomRuntime, { |
| 58 | label: "environment-data:mergebot:update-pull-request", |
| 59 | tag: WS_METHODS.mergebotUpdatePullRequest, |
| 60 | }), |
| 61 | deployProduction: createEnvironmentRpcCommand(connectionAtomRuntime, { |
| 62 | label: "environment-data:mergebot:deploy-production", |
| 63 | tag: WS_METHODS.mergebotDeployProduction, |
| 64 | }), |
| 65 | }; |