Skip to content
Source
Sign inSign up
pingdotgg/t3code
Open History to see the last commit

openIssueLink.ts

91 lines3.9 KB

1import type {
2 EnvironmentId,
3 IssueTrackerConnectionList,
4 ScopedThreadRef,
5} from "@t3tools/contracts";
6import { resolveIssueLinkTarget } from "@t3tools/client-runtime/issue-links";
7import { useNavigate } from "@tanstack/react-router";
8import type { MouseEvent } from "react";
9​
10import { readIssueListPreferences } from "../components/issue/issueListPreferences";
11import { ISSUES_PANEL_REF, PULL_REQUESTS_PANEL_REF, useRightPanelStore } from "../rightPanelStore";
12import { useProjects, useServerConfigs } from "../state/entities";
13​
14/**
15 * Opens an issue link beside the thread, and says whether it did. Anything else — a host no
16 * project here is on, a link that merely looks like one — is left alone for the caller to handle
17 * as the ordinary link it is. Mirrors `useOpenChangeRequestLink`: a reader following a link the
18 * agent wrote is reading the thread, and should still be reading it afterwards.
19 */
20export function useOpenIssueLink(
21 threadRef?: ScopedThreadRef,
22 panelRef?: ScopedThreadRef,
23): (
24 event: Pick<
25 MouseEvent<HTMLElement>,
26 "preventDefault" | "stopPropagation" | "metaKey" | "ctrlKey"
27 > &
28 Partial<Pick<MouseEvent<HTMLElement>, "shiftKey" | "altKey">>,
29 targetUrl: string,
30 targetEnvironmentId?: EnvironmentId,
31 trackers?: IssueTrackerConnectionList,
32) => boolean {
33 const allProjects = useProjects();
34 const serverConfigs = useServerConfigs();
35 const navigate = useNavigate();
36 return (event, targetUrl, targetEnvironmentId, trackers) => {
37 // Shift and Alt keep their browser meaning (new window, download), as on commit links.
38 if (event.metaKey || event.ctrlKey || event.shiftKey || event.altKey) return false;
39 const resolvedPanelRef = panelRef ?? threadRef;
40 // A list page's panel ref is a placeholder, not an environment; the link names its own.
41 const environmentId = threadRef?.environmentId ?? targetEnvironmentId;
42 if (resolvedPanelRef === undefined || environmentId === undefined) return false;
43 if (serverConfigs.get(environmentId)?.environment.capabilities.issues !== true) return false;
44 const target = resolveIssueLinkTarget({
45 url: targetUrl,
46 projects: allProjects.filter((project) => project.environmentId === environmentId),
47 trackers,
48 });
49 if (target === null) return false;
50 event.preventDefault();
51 event.stopPropagation();
52 const surface = {
53 projectId: target.ref.projectId,
54 tracker: target.ref.tracker,
55 ...(target.ref.host === undefined ? {} : { host: target.ref.host }),
56 scope: target.ref.scope,
57 key: target.ref.key,
58 url: targetUrl,
59 };
60 // A list page's panel is the Issues page's: the pull request list's own panel shows pull
61 // requests only. Opening the tab here, not only through the URL, also brings back one that
62 // is already open.
63 if (resolvedPanelRef === PULL_REQUESTS_PANEL_REF || resolvedPanelRef === ISSUES_PANEL_REF) {
64 useRightPanelStore.getState().openIssue(ISSUES_PANEL_REF, { ...surface, environmentId });
65 const selection = {
66 selectedEnvironmentId: environmentId,
67 selectedProjectId: target.ref.projectId,
68 ...(target.ref.host === undefined ? {} : { selectedHost: target.ref.host }),
69 tracker: target.ref.tracker,
70 scope: target.ref.scope,
71 key: target.ref.key,
72 };
73 // Already on the Issues page, the list keeps the filters it is showing.
74 if (window.location.pathname === "/issues") {
75 void navigate({
76 from: "/issues",
77 to: "/issues",
78 search: (previous) => ({ ...previous, ...selection }),
79 });
80 } else {
81 void navigate({ to: "/issues", search: { ...readIssueListPreferences(), ...selection } });
82 }
83 return true;
84 }
85 useRightPanelStore.getState().openIssue(resolvedPanelRef, {
86 ...(resolvedPanelRef.environmentId === environmentId ? {} : { environmentId }),
87 ...surface,
88 });
89 return true;
90 };