|
1 | 1 | import type { EnvironmentId, PullRequestRef, ScopedThreadRef } from "@t3tools/contracts"; |
2 | 2 | import { useAtomValue } from "@effect/atom-react"; |
3 | 3 | import { useNavigate } from "@tanstack/react-router"; |
4 | | -import { type MouseEvent, useCallback, useMemo } from "react"; |
| 4 | +import { type MouseEvent } from "react"; |
5 | 5 |
|
6 | 6 | import { sourceControlClients } from "@t3tools/client-runtime/source-control-clients"; |
7 | 7 | import { parseChangeRequestUrl, type ChangeRequestLink } from "@t3tools/shared/changeRequestUrl"; |
@@ -73,16 +73,12 @@ export function resolvePullRequestPreviewTarget({ |
73 | 73 | export function usePullRequestPreviewTarget(environmentId: EnvironmentId | null, url: string) { |
74 | 74 | const projects = useProjects(); |
75 | 75 | const serverConfig = useAtomValue(serverEnvironment.configValueAtom(environmentId)); |
76 | | - return useMemo( |
77 | | - () => |
78 | | - resolvePullRequestPreviewTarget({ |
79 | | - environmentId, |
80 | | - projects, |
81 | | - pullRequestsEnabled: serverConfig?.environment.capabilities.pullRequests === true, |
82 | | - url, |
83 | | - }), |
84 | | - [environmentId, projects, serverConfig, url], |
85 | | - ); |
| 76 | + return resolvePullRequestPreviewTarget({ |
| 77 | + environmentId, |
| 78 | + projects, |
| 79 | + pullRequestsEnabled: serverConfig?.environment.capabilities.pullRequests === true, |
| 80 | + url, |
| 81 | + }); |
86 | 82 | } |
87 | 83 |
|
88 | 84 | export function findProjectOnChangeRequestHost( |
@@ -123,135 +119,128 @@ export function useOpenChangeRequestLink( |
123 | 119 | const allProjects = useProjects(); |
124 | 120 | const primaryEnvironmentId = usePrimaryEnvironmentId(); |
125 | 121 | const openInApp = useClientSettings((settings) => settings.pullRequestLinksOpenInApp); |
126 | | - return useCallback( |
127 | | - (event, targetUrl, targetThreadRef, targetEnvironmentId) => { |
128 | | - if (!openInApp || shouldOpenPullRequestExternally(event)) return false; |
129 | | - // Read at click time: every provider status push replaces the config map, and |
130 | | - // subscribing would hand every rendered link a new handler. |
131 | | - const serverConfigs = appAtomRegistry.get(environmentServerConfigsAtom); |
132 | | - const resolvedThreadRef = targetThreadRef ?? threadRef; |
133 | | - const resolvedPanelRef = panelRef ?? resolvedThreadRef; |
134 | | - const parsed = parseChangeRequestUrl(targetUrl); |
135 | | - if (parsed === null) return false; |
136 | | - const reads = (environmentId: string) => |
137 | | - serverConfigs.get(environmentId as EnvironmentId)?.environment.capabilities.pullRequests === |
138 | | - true; |
139 | | - // Beside a thread the panel reads on that thread's environment, so a project from another |
140 | | - // one could not be read there whatever its remote says: two environments can hold the same |
141 | | - // repository, and handing the panel the wrong one's id opens a surface that never loads. |
142 | | - const projects = resolvedThreadRef |
143 | | - ? allProjects.filter((project) => project.environmentId === resolvedThreadRef.environmentId) |
144 | | - : targetEnvironmentId |
145 | | - ? allProjects.filter((project) => project.environmentId === targetEnvironmentId) |
146 | | - : allProjects |
147 | | - .filter((project) => reads(project.environmentId)) |
148 | | - .toSorted( |
149 | | - (left, right) => |
150 | | - Number(right.environmentId === primaryEnvironmentId) - |
151 | | - Number(left.environmentId === primaryEnvironmentId), |
152 | | - ); |
153 | | - const exactProject = findProjectForChangeRequest(projects, parsed); |
154 | | - const project = |
155 | | - exactProject ?? |
156 | | - (resolvedPanelRef |
157 | | - ? findProjectOnChangeRequestHost( |
158 | | - projects.filter( |
159 | | - (candidate) => |
160 | | - serverConfigs.get(candidate.environmentId)?.environment.capabilities |
161 | | - .threadPullRequests === true, |
162 | | - ), |
163 | | - parsed, |
164 | | - ) |
165 | | - : undefined); |
166 | | - if (project === undefined || !reads(project.environmentId)) return false; |
167 | | - const repository = |
168 | | - serverConfigs.get(project.environmentId)?.environment.capabilities.threadPullRequests === |
169 | | - true |
170 | | - ? parsed.repository |
171 | | - : (sourceControlRepositorySelector(project.repositoryIdentity) ?? parsed.repository); |
172 | | - event.preventDefault(); |
173 | | - event.stopPropagation(); |
174 | | - if (resolvedPanelRef) { |
175 | | - useRightPanelStore.getState().openPullRequest(resolvedPanelRef, { |
176 | | - // The standalone PR panel has a synthetic ref; each tab keeps its real environment. |
177 | | - ...(resolvedPanelRef.environmentId === project.environmentId |
178 | | - ? {} |
179 | | - : { environmentId: project.environmentId }), |
180 | | - projectId: project.id, |
181 | | - ...(serverConfigs.get(project.environmentId)?.environment.capabilities |
182 | | - .threadPullRequests === true |
183 | | - ? { host: parsed.authority ?? parsed.host } |
184 | | - : {}), |
185 | | - repository, |
186 | | - url: targetUrl, |
187 | | - number: parsed.number, |
| 122 | + return (event, targetUrl, targetThreadRef, targetEnvironmentId) => { |
| 123 | + if (!openInApp || shouldOpenPullRequestExternally(event)) return false; |
| 124 | + // Read at click time: every provider status push replaces the config map, and |
| 125 | + // subscribing would hand every rendered link a new handler. |
| 126 | + const serverConfigs = appAtomRegistry.get(environmentServerConfigsAtom); |
| 127 | + const resolvedThreadRef = targetThreadRef ?? threadRef; |
| 128 | + const resolvedPanelRef = panelRef ?? resolvedThreadRef; |
| 129 | + const parsed = parseChangeRequestUrl(targetUrl); |
| 130 | + if (parsed === null) return false; |
| 131 | + const reads = (environmentId: string) => |
| 132 | + serverConfigs.get(environmentId as EnvironmentId)?.environment.capabilities.pullRequests === |
| 133 | + true; |
| 134 | + // Beside a thread the panel reads on that thread's environment, so a project from another |
| 135 | + // one could not be read there whatever its remote says: two environments can hold the same |
| 136 | + // repository, and handing the panel the wrong one's id opens a surface that never loads. |
| 137 | + const projects = resolvedThreadRef |
| 138 | + ? allProjects.filter((project) => project.environmentId === resolvedThreadRef.environmentId) |
| 139 | + : targetEnvironmentId |
| 140 | + ? allProjects.filter((project) => project.environmentId === targetEnvironmentId) |
| 141 | + : allProjects |
| 142 | + .filter((project) => reads(project.environmentId)) |
| 143 | + .toSorted( |
| 144 | + (left, right) => |
| 145 | + Number(right.environmentId === primaryEnvironmentId) - |
| 146 | + Number(left.environmentId === primaryEnvironmentId), |
| 147 | + ); |
| 148 | + const exactProject = findProjectForChangeRequest(projects, parsed); |
| 149 | + const project = |
| 150 | + exactProject ?? |
| 151 | + (resolvedPanelRef |
| 152 | + ? findProjectOnChangeRequestHost( |
| 153 | + projects.filter( |
| 154 | + (candidate) => |
| 155 | + serverConfigs.get(candidate.environmentId)?.environment.capabilities |
| 156 | + .threadPullRequests === true, |
| 157 | + ), |
| 158 | + parsed, |
| 159 | + ) |
| 160 | + : undefined); |
| 161 | + if (project === undefined || !reads(project.environmentId)) return false; |
| 162 | + const repository = |
| 163 | + serverConfigs.get(project.environmentId)?.environment.capabilities.threadPullRequests === true |
| 164 | + ? parsed.repository |
| 165 | + : (sourceControlRepositorySelector(project.repositoryIdentity) ?? parsed.repository); |
| 166 | + event.preventDefault(); |
| 167 | + event.stopPropagation(); |
| 168 | + if (resolvedPanelRef) { |
| 169 | + useRightPanelStore.getState().openPullRequest(resolvedPanelRef, { |
| 170 | + // The standalone PR panel has a synthetic ref; each tab keeps its real environment. |
| 171 | + ...(resolvedPanelRef.environmentId === project.environmentId |
| 172 | + ? {} |
| 173 | + : { environmentId: project.environmentId }), |
| 174 | + projectId: project.id, |
| 175 | + ...(serverConfigs.get(project.environmentId)?.environment.capabilities |
| 176 | + .threadPullRequests === true |
| 177 | + ? { host: parsed.authority ?? parsed.host } |
| 178 | + : {}), |
| 179 | + repository, |
| 180 | + url: targetUrl, |
| 181 | + number: parsed.number, |
| 182 | + }); |
| 183 | + if (!resolvedThreadRef) { |
| 184 | + void navigate({ |
| 185 | + to: "/pull-requests", |
| 186 | + search: (previous) => ({ |
| 187 | + ...previous, |
| 188 | + involvement: previous.involvement ?? "all", |
| 189 | + state: previous.state ?? "all", |
| 190 | + repository, |
| 191 | + number: parsed.number, |
| 192 | + selectedHost: parsed.authority ?? parsed.host, |
| 193 | + selectedProjectId: project.id, |
| 194 | + selectedEnvironmentId: project.environmentId, |
| 195 | + }), |
| 196 | + replace: true, |
188 | 197 | }); |
189 | | - if (!resolvedThreadRef) { |
190 | | - void navigate({ |
191 | | - to: "/pull-requests", |
192 | | - search: (previous) => ({ |
193 | | - ...previous, |
194 | | - involvement: previous.involvement ?? "all", |
195 | | - state: previous.state ?? "all", |
196 | | - repository, |
197 | | - number: parsed.number, |
198 | | - selectedHost: parsed.authority ?? parsed.host, |
199 | | - selectedProjectId: project.id, |
200 | | - selectedEnvironmentId: project.environmentId, |
201 | | - }), |
202 | | - replace: true, |
203 | | - }); |
204 | | - } |
205 | | - return true; |
206 | 198 | } |
207 | | - void navigate({ |
208 | | - to: "/pull-requests", |
209 | | - search: { |
210 | | - involvement: "all", |
211 | | - state: "all", |
212 | | - repository, |
213 | | - number: parsed.number, |
214 | | - selectedHost: parsed.authority ?? parsed.host, |
215 | | - selectedProjectId: project.id, |
216 | | - selectedEnvironmentId: project.environmentId, |
217 | | - }, |
218 | | - }); |
219 | 199 | return true; |
220 | | - }, |
221 | | - [allProjects, navigate, openInApp, panelRef, primaryEnvironmentId, threadRef], |
222 | | - ); |
| 200 | + } |
| 201 | + void navigate({ |
| 202 | + to: "/pull-requests", |
| 203 | + search: { |
| 204 | + involvement: "all", |
| 205 | + state: "all", |
| 206 | + repository, |
| 207 | + number: parsed.number, |
| 208 | + selectedHost: parsed.authority ?? parsed.host, |
| 209 | + selectedProjectId: project.id, |
| 210 | + selectedEnvironmentId: project.environmentId, |
| 211 | + }, |
| 212 | + }); |
| 213 | + return true; |
| 214 | + }; |
223 | 215 | } |
224 | 216 |
|
225 | 217 | export function useOpenPrLink(threadRef?: ScopedThreadRef) { |
226 | 218 | const openChangeRequest = useOpenChangeRequestLink(threadRef); |
227 | 219 | const openLink = useOpenLink(threadRef); |
228 | | - return useCallback( |
229 | | - (event: MouseEvent<HTMLElement>, prUrl: string, targetThreadRef?: ScopedThreadRef) => { |
230 | | - if (event.button > 1) return false; |
231 | | - event.stopPropagation(); |
232 | | - const openInBrowser = shouldOpenPullRequestExternally(event); |
233 | | - const isAnchor = |
234 | | - event.currentTarget instanceof HTMLAnchorElement && event.currentTarget.href.length > 0; |
235 | | - // A real link already knows how to cmd/ctrl/middle-click. Leave its default |
236 | | - // action alone so the browser (or Electron's window-open handler) opens |
237 | | - // the host. Buttons have no href, so they still go through openExternal. |
238 | | - if (openInBrowser && isAnchor) return false; |
| 220 | + return (event: MouseEvent<HTMLElement>, prUrl: string, targetThreadRef?: ScopedThreadRef) => { |
| 221 | + if (event.button > 1) return false; |
| 222 | + event.stopPropagation(); |
| 223 | + const openInBrowser = shouldOpenPullRequestExternally(event); |
| 224 | + const isAnchor = |
| 225 | + event.currentTarget instanceof HTMLAnchorElement && event.currentTarget.href.length > 0; |
| 226 | + // A real link already knows how to cmd/ctrl/middle-click. Leave its default |
| 227 | + // action alone so the browser (or Electron's window-open handler) opens |
| 228 | + // the host. Buttons have no href, so they still go through openExternal. |
| 229 | + if (openInBrowser && isAnchor) return false; |
239 | 230 |
|
240 | | - event.preventDefault(); |
241 | | - if (!openInBrowser && openChangeRequest(event, prUrl, targetThreadRef)) return true; |
| 231 | + event.preventDefault(); |
| 232 | + if (!openInBrowser && openChangeRequest(event, prUrl, targetThreadRef)) return true; |
242 | 233 |
|
243 | | - void openLink(prUrl, { event, threadRef: targetThreadRef }).catch((error: unknown) => { |
244 | | - console.error(error); |
245 | | - toastManager.add( |
246 | | - stackedThreadToast({ |
247 | | - type: "error", |
248 | | - title: "Unable to open pull request link", |
249 | | - description: error instanceof Error ? error.message : "An error occurred.", |
250 | | - }), |
251 | | - ); |
252 | | - }); |
253 | | - return false; |
254 | | - }, |
255 | | - [openChangeRequest, openLink], |
256 | | - ); |
| 234 | + void openLink(prUrl, { event, threadRef: targetThreadRef }).catch((error: unknown) => { |
| 235 | + console.error(error); |
| 236 | + toastManager.add( |
| 237 | + stackedThreadToast({ |
| 238 | + type: "error", |
| 239 | + title: "Unable to open pull request link", |
| 240 | + description: error instanceof Error ? error.message : "An error occurred.", |
| 241 | + }), |
| 242 | + ); |
| 243 | + }); |
| 244 | + return false; |
| 245 | + }; |
257 | 246 | } |
0 commit comments