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 { thrownErrorMessage } from "@t3tools/client-runtime/errors"; |
| 2 | import { parsePullRequestAnchor } from "@t3tools/shared/pullRequestAnchor"; |
| 3 | import type { EnvironmentId, ScopedThreadRef } from "@t3tools/contracts"; |
| 4 | import { useAtomValue } from "@effect/atom-react"; |
| 5 | import { useNavigate } from "@tanstack/react-router"; |
| 6 | import { type MouseEvent } from "react"; |
| 7 | import { msg } from "gt-react"; |
| 8 | |
| 9 | import { |
| 10 | resolveChangeRequestLinkTarget, |
| 11 | resolvePullRequestPreviewTarget, |
| 12 | } from "@t3tools/client-runtime/change-request-links"; |
| 13 | |
| 14 | import { sourceControlClients } from "@t3tools/client-runtime/source-control-clients"; |
| 15 | |
| 16 | import { canOpenLinksInApp } from "../browser/browserLinkTarget"; |
| 17 | import { useOpenLink } from "../browser/useOpenLink"; |
| 18 | import { useClientSettings } from "../hooks/useSettings"; |
| 19 | import { stackedThreadToast, toastManager } from "../components/ui/toast"; |
| 20 | import { m } from "../i18n/messages"; |
| 21 | import { useRightPanelStore } from "../rightPanelStore"; |
| 22 | import { |
| 23 | selectThreadBranchBaseRef, |
| 24 | selectThreadDiffPanelSelection, |
| 25 | useDiffPanelStore, |
| 26 | } from "../diffPanelStore"; |
| 27 | |
| 28 | import { appAtomRegistry } from "../rpc/atomRegistry"; |
| 29 | import { readProject, readProjects, readThreadShell, useProjects } from "../state/entities"; |
| 30 | import { environmentServerConfigsAtom, serverEnvironment } from "../state/server"; |
| 31 | import { usePrimaryEnvironmentId } from "../state/environments"; |
| 32 | import { reviewEnvironment } from "../state/review"; |
| 33 | import { useAtomQueryRunner } from "../state/use-atom-query-runner"; |
| 34 | |
| 35 | export function usePullRequestPreviewTarget(environmentId: EnvironmentId | null, url: string) { |
| 36 | const projects = useProjects(); |
| 37 | const serverConfig = useAtomValue(serverEnvironment.configValueAtom(environmentId)); |
| 38 | return resolvePullRequestPreviewTarget({ |
| 39 | environmentId, |
| 40 | projects, |
| 41 | pullRequestsEnabled: serverConfig !== null, |
| 42 | url, |
| 43 | }); |
| 44 | } |
| 45 | |
| 46 | /** Where a click on this link opens the in-app panel, read at call time as the click reads it. */ |
| 47 | export function pullRequestLinkPanelTarget(input: { |
| 48 | readonly url: string; |
| 49 | readonly environmentId: EnvironmentId | undefined; |
| 50 | readonly primaryEnvironmentId: EnvironmentId | null; |
| 51 | readonly lendHostProject: boolean; |
| 52 | }) { |
| 53 | // Read at click time: every provider status push replaces the config map, and |
| 54 | // subscribing would hand every rendered link a new handler. Projects likewise, so a |
| 55 | // link can resolve against a snapshot that arrived after this handler was built. |
| 56 | const serverConfigs = appAtomRegistry.get(environmentServerConfigsAtom); |
| 57 | return resolveChangeRequestLinkTarget({ |
| 58 | url: input.url, |
| 59 | projects: readProjects(), |
| 60 | configLoaded: (environmentId) => serverConfigs.has(environmentId), |
| 61 | environmentId: input.environmentId, |
| 62 | primaryEnvironmentId: input.primaryEnvironmentId, |
| 63 | lendHostProject: input.lendHostProject, |
| 64 | }); |
| 65 | } |
| 66 | |
| 67 | function shouldOpenPullRequestExternally( |
| 68 | event: Pick<MouseEvent<HTMLElement>, "metaKey" | "ctrlKey"> & |
| 69 | Partial<Pick<MouseEvent<HTMLElement>, "button">>, |
| 70 | ): boolean { |
| 71 | return event.metaKey || event.ctrlKey || event.button === 1; |
| 72 | } |
| 73 | |
| 74 | export function useOpenChangeRequestLink( |
| 75 | threadRef?: ScopedThreadRef, |
| 76 | panelRef?: ScopedThreadRef, |
| 77 | ): ( |
| 78 | event: Pick< |
| 79 | MouseEvent<HTMLElement>, |
| 80 | "preventDefault" | "stopPropagation" | "metaKey" | "ctrlKey" |
| 81 | > & |
| 82 | Partial<Pick<MouseEvent<HTMLElement>, "button" | "shiftKey" | "altKey">>, |
| 83 | targetUrl: string, |
| 84 | targetThreadRef?: ScopedThreadRef, |
| 85 | targetEnvironmentId?: EnvironmentId, |
| 86 | onCommitUnavailable?: () => void, |
| 87 | ) => boolean { |
| 88 | const navigate = useNavigate(); |
| 89 | const primaryEnvironmentId = usePrimaryEnvironmentId(); |
| 90 | const openInApp = useClientSettings((settings) => settings.pullRequestLinksOpenInApp); |
| 91 | const ignoreWhitespace = useClientSettings((settings) => settings.diffIgnoreWhitespace); |
| 92 | const browserLinkTarget = useClientSettings((settings) => settings.browserLinkTarget); |
| 93 | const readDiffPreview = useAtomQueryRunner(reviewEnvironment.diffPreview, { |
| 94 | reportFailure: false, |
| 95 | refresh: true, |
| 96 | }); |
| 97 | const openLink = useOpenLink(threadRef); |
| 98 | return (event, targetUrl, targetThreadRef, targetEnvironmentId, onCommitUnavailable) => { |
| 99 | if (!openInApp || shouldOpenPullRequestExternally(event)) return false; |
| 100 | const resolvedThreadRef = targetThreadRef ?? threadRef; |
| 101 | const resolvedPanelRef = panelRef ?? resolvedThreadRef; |
| 102 | const openPullRequest = () => { |
| 103 | const target = pullRequestLinkPanelTarget({ |
| 104 | url: targetUrl, |
| 105 | environmentId: resolvedThreadRef?.environmentId ?? targetEnvironmentId, |
| 106 | primaryEnvironmentId, |
| 107 | lendHostProject: resolvedPanelRef !== undefined, |
| 108 | }); |
| 109 | if (target === null) return false; |
| 110 | const { project, repository, number, host } = target; |
| 111 | const revealUrl = parsePullRequestAnchor(targetUrl) === null ? undefined : targetUrl; |
| 112 | const pullRequestsSearch = { |
| 113 | repository, |
| 114 | number, |
| 115 | ...(host === undefined ? {} : { selectedHost: host }), |
| 116 | selectedProjectId: project.id, |
| 117 | selectedEnvironmentId: project.environmentId, |
| 118 | }; |
| 119 | if (resolvedPanelRef) { |
| 120 | useRightPanelStore.getState().openPullRequest(resolvedPanelRef, { |
| 121 | // The standalone PR panel has a synthetic ref; each tab keeps its real environment. |
| 122 | ...(resolvedPanelRef.environmentId === project.environmentId |
| 123 | ? {} |
| 124 | : { environmentId: project.environmentId }), |
| 125 | projectId: project.id, |
| 126 | ...(host === undefined ? {} : { host }), |
| 127 | repository, |
| 128 | url: targetUrl, |
| 129 | ...(revealUrl === undefined ? {} : { revealUrl }), |
| 130 | number, |
| 131 | }); |
| 132 | if (!resolvedThreadRef) { |
| 133 | void navigate({ |
| 134 | to: "/pull-requests", |
| 135 | search: (previous) => ({ |
| 136 | ...previous, |
| 137 | involvement: previous.involvement ?? "all", |
| 138 | state: previous.state ?? "all", |
| 139 | ...pullRequestsSearch, |
| 140 | }), |
| 141 | replace: true, |
| 142 | }); |
| 143 | } |
| 144 | return true; |
| 145 | } |
| 146 | void navigate({ |
| 147 | to: "/pull-requests", |
| 148 | search: { |
| 149 | involvement: "all", |
| 150 | state: "all", |
| 151 | ...pullRequestsSearch, |
| 152 | ...(revealUrl === undefined ? {} : { selectedUrl: revealUrl }), |
| 153 | }, |
| 154 | }); |
| 155 | return true; |
| 156 | }; |
| 157 | const commitThread = resolvedThreadRef ? readThreadShell(resolvedThreadRef) : null; |
| 158 | const commitProject = |
| 159 | commitThread && resolvedThreadRef |
| 160 | ? readProject({ |
| 161 | environmentId: resolvedThreadRef.environmentId, |
| 162 | projectId: commitThread.projectId, |
| 163 | }) |
| 164 | : null; |
| 165 | const identity = commitProject?.repositoryIdentity; |
| 166 | const parsedCommit = sourceControlClients.parseCommitUrl(targetUrl, identity?.provider); |
| 167 | // Shift and Alt keep their browser meaning (new window, download) on commit links. |
| 168 | if (parsedCommit !== null && (event.shiftKey || event.altKey)) return false; |
| 169 | // A plain commit link stays a workspace commit; one naming a file or line opens the PR's diff. |
| 170 | const anchor = parsePullRequestAnchor(targetUrl); |
| 171 | if (anchor !== null && anchor.kind !== "code" && openPullRequest()) { |
| 172 | event.preventDefault(); |
| 173 | event.stopPropagation(); |
| 174 | return true; |
| 175 | } |
| 176 | |
| 177 | if ( |
| 178 | parsedCommit !== null && |
| 179 | commitThread && |
| 180 | resolvedThreadRef && |
| 181 | commitProject?.environmentId === resolvedThreadRef.environmentId && |
| 182 | identity && |
| 183 | identity.provider === parsedCommit.definition.kind && |
| 184 | parsedCommit.definition.isChangeRequestInRepository(identity, parsedCommit.link) |
| 185 | ) { |
| 186 | const commit = parsedCommit.link; |
| 187 | event.preventDefault(); |
| 188 | event.stopPropagation(); |
| 189 | const userActionRevision = useRightPanelStore.getState().markUserAction(resolvedThreadRef); |
| 190 | const baseRef = selectThreadBranchBaseRef(useDiffPanelStore.getState(), resolvedThreadRef); |
| 191 | const selection = selectThreadDiffPanelSelection( |
| 192 | useDiffPanelStore.getState().byThreadKey, |
| 193 | resolvedThreadRef, |
| 194 | ); |
| 195 | void readDiffPreview({ |
| 196 | environmentId: resolvedThreadRef.environmentId, |
| 197 | input: { |
| 198 | cwd: commitThread.worktreePath ?? commitProject.workspaceRoot, |
| 199 | ...(baseRef ? { baseRef } : {}), |
| 200 | commitSha: commit.sha, |
| 201 | ignoreWhitespace, |
| 202 | }, |
| 203 | }) |
| 204 | .then((result) => { |
| 205 | if ( |
| 206 | useRightPanelStore.getState().getUserActionRevision(resolvedThreadRef) !== |
| 207 | userActionRevision || |
| 208 | selectThreadDiffPanelSelection( |
| 209 | useDiffPanelStore.getState().byThreadKey, |
| 210 | resolvedThreadRef, |
| 211 | ) !== selection |
| 212 | ) |
| 213 | return; |
| 214 | if ( |
| 215 | result._tag === "Success" && |
| 216 | result.value.sources.some( |
| 217 | (source) => source.kind === "commit" && source.headRef === commit.sha, |
| 218 | ) |
| 219 | ) { |
| 220 | useDiffPanelStore.getState().selectCommit(resolvedThreadRef, commit.sha); |
| 221 | useRightPanelStore.getState().open(resolvedThreadRef, "diff"); |
| 222 | return; |
| 223 | } |
| 224 | if (openPullRequest()) return; |
| 225 | if (onCommitUnavailable) return onCommitUnavailable(); |
| 226 | // A popup opened after the lookup has lost the click's activation, so browsers block it. |
| 227 | if ( |
| 228 | window.desktopBridge === undefined && |
| 229 | (browserLinkTarget !== "app" || !canOpenLinksInApp(resolvedThreadRef)) |
| 230 | ) { |
| 231 | const toastId = toastManager.add( |
| 232 | stackedThreadToast({ |
| 233 | type: "info", |
| 234 | title: m(msg("This commit is not in the thread's workspace")), |
| 235 | actionProps: { |
| 236 | children: m(msg("Open link")), |
| 237 | onClick: () => { |
| 238 | toastManager.close(toastId); |
| 239 | window.open(targetUrl, "_blank", "noopener,noreferrer"); |
| 240 | }, |
| 241 | }, |
| 242 | }), |
| 243 | ); |
| 244 | return; |
| 245 | } |
| 246 | return openLink(targetUrl, { |
| 247 | event: { metaKey: event.metaKey, ctrlKey: event.ctrlKey, button: event.button }, |
| 248 | threadRef: resolvedThreadRef, |
| 249 | }); |
| 250 | }) |
| 251 | .catch((error: unknown) => console.error(error)); |
| 252 | return true; |
| 253 | } |
| 254 | if (!openPullRequest()) return false; |
| 255 | event.preventDefault(); |
| 256 | event.stopPropagation(); |
| 257 | return true; |
| 258 | }; |
| 259 | } |
| 260 | |
| 261 | export function useOpenPrLink(threadRef?: ScopedThreadRef) { |
| 262 | const openChangeRequest = useOpenChangeRequestLink(threadRef); |
| 263 | const openLink = useOpenLink(threadRef); |
| 264 | return (event: MouseEvent<HTMLElement>, prUrl: string, targetThreadRef?: ScopedThreadRef) => { |
| 265 | if (event.button > 1) return false; |
| 266 | event.stopPropagation(); |
| 267 | const openInBrowser = shouldOpenPullRequestExternally(event); |
| 268 | const isAnchor = |
| 269 | event.currentTarget instanceof HTMLAnchorElement && event.currentTarget.href.length > 0; |
| 270 | // Native links handle modified clicks; buttons still need openExternal. |
| 271 | if (openInBrowser && isAnchor) return false; |
| 272 | |
| 273 | event.preventDefault(); |
| 274 | if (!openInBrowser && openChangeRequest(event, prUrl, targetThreadRef)) return true; |
| 275 | |
| 276 | void openLink(prUrl, { event, threadRef: targetThreadRef }).catch((error: unknown) => { |
| 277 | console.error(error); |
| 278 | toastManager.add( |
| 279 | stackedThreadToast({ |
| 280 | type: "error", |
| 281 | title: m(msg("Unable to open pull request link")), |
| 282 | description: thrownErrorMessage(error, m) ?? m(msg("An error occurred.")), |
| 283 | }), |
| 284 | ); |
| 285 | }); |
| 286 | return false; |
| 287 | }; |
| 288 | } |