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

openIssueLink.ts

83 lines3.6 KB

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