The best way to control Claude Code, Codex and more from any computer or phone
| 1 | import type { |
| 2 | EnvironmentId, |
| 3 | IssueTrackerConnectionList, |
| 4 | ScopedThreadRef, |
| 5 | } from "@t3tools/contracts"; |
| 6 | import { resolveIssueLinkTarget } from "@t3tools/client-runtime/issue-links"; |
| 7 | import { useNavigate } from "@tanstack/react-router"; |
| 8 | import type { MouseEvent } from "react"; |
| 9 | |
| 10 | import { readIssueListPreferences } from "../components/issue/issueListPreferences"; |
| 11 | import { ISSUES_PANEL_REF, PULL_REQUESTS_PANEL_REF, useRightPanelStore } from "../rightPanelStore"; |
| 12 | import { 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 | */ |
| 20 | export function useOpenIssueLink( |
| 21 | threadRef?: ScopedThreadRef, |
| 22 | panelRef?: ScopedThreadRef, |
| 23 | ): ( |
| 24 | event: Pick< |
| 25 | MouseEvent<HTMLElement>, |
| 26 | "preventDefault" | "stopPropagation" | "metaKey" | "ctrlKey" |
| 27 | >, |
| 28 | targetUrl: string, |
| 29 | targetEnvironmentId?: EnvironmentId, |
| 30 | trackers?: IssueTrackerConnectionList, |
| 31 | ) => boolean { |
| 32 | const allProjects = useProjects(); |
| 33 | const serverConfigs = useServerConfigs(); |
| 34 | const navigate = useNavigate(); |
| 35 | return (event, targetUrl, targetEnvironmentId, trackers) => { |
| 36 | if (event.metaKey || event.ctrlKey) return false; |
| 37 | const resolvedPanelRef = panelRef ?? threadRef; |
| 38 | // A list page's panel ref is a placeholder, not an environment; the link names its own. |
| 39 | const environmentId = threadRef?.environmentId ?? targetEnvironmentId; |
| 40 | if (resolvedPanelRef === undefined || environmentId === undefined) return false; |
| 41 | if (serverConfigs.get(environmentId)?.environment.capabilities.issues !== true) return false; |
| 42 | const target = resolveIssueLinkTarget({ |
| 43 | url: targetUrl, |
| 44 | projects: allProjects.filter((project) => project.environmentId === environmentId), |
| 45 | trackers, |
| 46 | }); |
| 47 | if (target === null) return false; |
| 48 | event.preventDefault(); |
| 49 | event.stopPropagation(); |
| 50 | const surface = { |
| 51 | projectId: target.ref.projectId, |
| 52 | tracker: target.ref.tracker, |
| 53 | ...(target.ref.host === undefined ? {} : { host: target.ref.host }), |
| 54 | scope: target.ref.scope, |
| 55 | key: target.ref.key, |
| 56 | url: targetUrl, |
| 57 | }; |
| 58 | // A list page's panel is the Issues page's: its pull request tabs render this markdown too, |
| 59 | // and the pull request list's own panel shows pull requests only. Opening the tab here, not |
| 60 | // only through the URL, also brings back one that is already open. |
| 61 | if (resolvedPanelRef === PULL_REQUESTS_PANEL_REF) { |
| 62 | useRightPanelStore.getState().openIssue(ISSUES_PANEL_REF, { ...surface, environmentId }); |
| 63 | const selection = { |
| 64 | selectedEnvironmentId: environmentId, |
| 65 | selectedProjectId: target.ref.projectId, |
| 66 | ...(target.ref.host === undefined ? {} : { selectedHost: target.ref.host }), |
| 67 | tracker: target.ref.tracker, |
| 68 | scope: target.ref.scope, |
| 69 | key: target.ref.key, |
| 70 | }; |
| 71 | // Already on the Issues page, the list keeps the filters it is showing. |
| 72 | if (window.location.pathname === "/issues") { |
| 73 | void navigate({ |
| 74 | from: "/issues", |
| 75 | to: "/issues", |
| 76 | search: (previous) => ({ ...previous, ...selection }), |
| 77 | }); |
| 78 | } else { |
| 79 | void navigate({ to: "/issues", search: { ...readIssueListPreferences(), ...selection } }); |
| 80 | } |
| 81 | return true; |
| 82 | } |
| 83 | useRightPanelStore.getState().openIssue(resolvedPanelRef, { |
| 84 | ...(resolvedPanelRef.environmentId === environmentId ? {} : { environmentId }), |
| 85 | ...surface, |
| 86 | }); |
| 87 | return true; |
| 88 | }; |
| 89 | } |