|
| 1 | +import { |
| 2 | + pullRequestLinkProject, |
| 3 | + resolveLinkPullRequestInput, |
| 4 | +} from "@t3tools/client-runtime/pull-request-link-input"; |
| 5 | +import type { |
| 6 | + EnvironmentProject, |
| 7 | + EnvironmentThreadShell, |
| 8 | +} from "@t3tools/client-runtime/state/shell"; |
| 9 | +import { |
| 10 | + isAtomCommandInterrupted, |
| 11 | + squashAtomCommandFailure, |
| 12 | + type AtomCommandResult, |
| 13 | +} from "@t3tools/client-runtime/state/runtime"; |
| 14 | +import { planThreadPullRequestMutation } from "@t3tools/client-runtime/thread-pull-request-compatibility"; |
| 15 | +import { AuthOrchestrationOperateScope, type ThreadPullRequestLink } from "@t3tools/contracts"; |
| 16 | +import * as Clipboard from "expo-clipboard"; |
| 17 | +import { useCallback, useMemo } from "react"; |
| 18 | +import { Linking } from "react-native"; |
| 19 | + |
| 20 | +import * as Dialog from "../../components/ui/Dialog"; |
| 21 | +import type { MenuProps } from "../../components/ui/Menu.types"; |
| 22 | +import type { EnvironmentRuntimeState } from "../../state/remote-runtime-types"; |
| 23 | +import { useEnvironmentScope } from "../../state/session"; |
| 24 | +import { threadEnvironment } from "../../state/threads"; |
| 25 | +import { useAtomCommand } from "../../state/use-atom-command"; |
| 26 | + |
| 27 | +type MenuAction = MenuProps["actions"][number]; |
| 28 | + |
| 29 | +export function useLinkedPullRequestActions(input: { |
| 30 | + readonly thread: EnvironmentThreadShell | null; |
| 31 | + readonly project: EnvironmentProject | null; |
| 32 | + readonly runtime: EnvironmentRuntimeState | null; |
| 33 | +}) { |
| 34 | + const { thread, project, runtime } = input; |
| 35 | + const capabilities = runtime?.serverConfig?.environment.capabilities; |
| 36 | + const canOperate = useEnvironmentScope( |
| 37 | + thread?.environmentId ?? null, |
| 38 | + AuthOrchestrationOperateScope, |
| 39 | + ); |
| 40 | + const canEdit = canOperate && capabilities?.threadPullRequests === true; |
| 41 | + const canWatch = canOperate && capabilities?.threadPullRequestWatch === true; |
| 42 | + const canStartWatch = |
| 43 | + canWatch && |
| 44 | + thread !== null && |
| 45 | + thread.archivedAt === null && |
| 46 | + thread.settledAt === null && |
| 47 | + thread.settledOverride !== "settled" && |
| 48 | + thread.lineage.relationshipToParent !== "subagent"; |
| 49 | + const link = useAtomCommand(threadEnvironment.linkPullRequest, { reportFailure: false }); |
| 50 | + const unlink = useAtomCommand(threadEnvironment.unlinkPullRequest, { reportFailure: false }); |
| 51 | + const watch = useAtomCommand(threadEnvironment.watchPullRequest, { reportFailure: false }); |
| 52 | + |
| 53 | + const run = useCallback( |
| 54 | + async (failureTitle: string, pending: Promise<AtomCommandResult<unknown, unknown>>) => { |
| 55 | + const result = await pending; |
| 56 | + if (result._tag === "Success" || isAtomCommandInterrupted(result)) return; |
| 57 | + const cause = squashAtomCommandFailure(result); |
| 58 | + Dialog.alert(failureTitle, cause instanceof Error ? cause.message : "Try again."); |
| 59 | + }, |
| 60 | + [], |
| 61 | + ); |
| 62 | + |
| 63 | + const setLinked = useCallback( |
| 64 | + ( |
| 65 | + reference: { host: string; repository: string; number: number; url: string }, |
| 66 | + linked: boolean, |
| 67 | + ) => { |
| 68 | + if (thread === null) return; |
| 69 | + const mutation = planThreadPullRequestMutation({ |
| 70 | + capabilities, |
| 71 | + threadId: thread.id, |
| 72 | + reference, |
| 73 | + legacyProjectId: null, |
| 74 | + linked, |
| 75 | + }); |
| 76 | + if (mutation === null || mutation.type === "thread.meta.update") return; |
| 77 | + const environmentId = thread.environmentId; |
| 78 | + void run( |
| 79 | + linked ? "Couldn't link this pull request" : "Couldn't unlink this pull request", |
| 80 | + mutation.type === "thread.pull-request.link" |
| 81 | + ? link({ environmentId, input: mutation.input }) |
| 82 | + : unlink({ environmentId, input: mutation.input }), |
| 83 | + ); |
| 84 | + }, |
| 85 | + [capabilities, link, run, thread, unlink], |
| 86 | + ); |
| 87 | + |
| 88 | + const promptLink = useCallback(() => { |
| 89 | + Dialog.prompt({ |
| 90 | + title: "Link pull request", |
| 91 | + initialValue: "", |
| 92 | + confirmText: "Link", |
| 93 | + onConfirm: (reference) => { |
| 94 | + const resolved = resolveLinkPullRequestInput({ |
| 95 | + reference, |
| 96 | + project: pullRequestLinkProject(project?.repositoryIdentity), |
| 97 | + hasProject: () => true, |
| 98 | + }); |
| 99 | + if (resolved === null) { |
| 100 | + Dialog.alert("Couldn't link this pull request", "Use a pull request URL, 123, or #123."); |
| 101 | + return; |
| 102 | + } |
| 103 | + if ("error" in resolved) { |
| 104 | + Dialog.alert("Couldn't link this pull request", resolved.error); |
| 105 | + return; |
| 106 | + } |
| 107 | + setLinked(resolved.link, true); |
| 108 | + }, |
| 109 | + }); |
| 110 | + }, [project, setLinked]); |
| 111 | + |
| 112 | + const menuActions = useCallback( |
| 113 | + (link: ThreadPullRequestLink): MenuAction[] => { |
| 114 | + const open = link.snapshot === null || link.snapshot.state === "open"; |
| 115 | + const watching = link.watch !== undefined; |
| 116 | + return [ |
| 117 | + { id: "copy", title: "Copy link", image: "link" }, |
| 118 | + { id: "browser", title: "Open in browser", image: "safari" }, |
| 119 | + ...((watching ? canWatch : canStartWatch) && open |
| 120 | + ? [ |
| 121 | + { |
| 122 | + id: "watch", |
| 123 | + title: watching ? "Stop watching" : "Watch for changes", |
| 124 | + image: watching ? "eye.slash" : "eye", |
| 125 | + }, |
| 126 | + ] |
| 127 | + : []), |
| 128 | + ...(canEdit |
| 129 | + ? [ |
| 130 | + { |
| 131 | + id: "unlink", |
| 132 | + title: link.source === "stack" ? "Dismiss from thread" : "Unlink from thread", |
| 133 | + image: "xmark", |
| 134 | + attributes: { destructive: true }, |
| 135 | + }, |
| 136 | + ] |
| 137 | + : []), |
| 138 | + ]; |
| 139 | + }, |
| 140 | + [canEdit, canStartWatch, canWatch], |
| 141 | + ); |
| 142 | + |
| 143 | + const onMenuAction = useCallback( |
| 144 | + (link: ThreadPullRequestLink, action: string) => { |
| 145 | + if (action === "copy") void Clipboard.setStringAsync(link.url); |
| 146 | + else if (action === "browser") void Linking.openURL(link.url); |
| 147 | + else if (action === "watch" && thread !== null) |
| 148 | + void run( |
| 149 | + "Couldn't change watching", |
| 150 | + watch({ |
| 151 | + environmentId: thread.environmentId, |
| 152 | + input: { |
| 153 | + threadId: thread.id, |
| 154 | + host: link.host, |
| 155 | + repository: link.repository, |
| 156 | + number: link.number, |
| 157 | + watching: link.watch === undefined, |
| 158 | + }, |
| 159 | + }), |
| 160 | + ); |
| 161 | + else if (action === "unlink") setLinked(link, false); |
| 162 | + }, |
| 163 | + [run, setLinked, thread, watch], |
| 164 | + ); |
| 165 | + |
| 166 | + return useMemo( |
| 167 | + () => ({ canLink: canEdit, promptLink, menuActions, onMenuAction }), |
| 168 | + [canEdit, promptLink, menuActions, onMenuAction], |
| 169 | + ); |
| 170 | +} |
| 171 | + |
| 172 | +export type LinkedPullRequestActions = ReturnType<typeof useLinkedPullRequestActions>; |
0 commit comments