Open History to see the last commit
1import { threadIssueKeyOf, type ScopedThreadRef, type ThreadIssueLink } from "@t3tools/contracts";
2import { scopedThreadKey } from "@t3tools/client-runtime/environment";
3import { useGT, useLocale, plural, part } from "gt-react";
4import { MessageSquareIcon } from "lucide-react";
5import { useState, type MouseEvent } from "react";
6
7import { cn } from "~/lib/utils";
8import { useRightPanelStore } from "~/rightPanelStore";
9import { useServerConfigs } from "~/state/entities";
10import { useLinkedIssueDetail } from "~/state/issues";
11
12import {
13 IssueGlyph,
14 issueStatePresentation,
15 issueTrackerPresentation,
16} from "../issue/issuePresentation";
17import {
18 PULL_REQUEST_ROW_CLASS,
19 PULL_REQUEST_ROW_NUMBER_CLASS,
20 PullRequestRowLines,
21} from "../pullRequest/PullRequestListRow";
22import {
23 PullRequestActorAvatar,
24 PullRequestActorLabel,
25} from "../pullRequest/pullRequestPresentation";
26import { ThreadDetailsControl, ThreadDetailsMoreButton } from "./ThreadDetailsControl";
27import { SharedTooltipTrigger } from "../ui/shared-tooltip";
28
29function LinkedIssueRow({
30 link,
31 threadRef,
32 compact,
33 mountScope,
34 onOpen,
35}: {
36 link: ThreadIssueLink;
37 threadRef: ScopedThreadRef;
38 compact: boolean;
39 mountScope: "thread-details" | "linked-items" | "sidebar-tooltip";
40 onOpen: (() => void) | undefined;
41}) {
42 const gt = useGT();
43 const locale = useLocale();
44 const configs = useServerConfigs();
45 const detail = useLinkedIssueDetail(threadRef.environmentId, link, mountScope);
46 const trackerPresentation = issueTrackerPresentation(link.tracker);
47 const state = detail === null ? null : issueStatePresentation(detail.state);
48 const Icon = state?.Icon ?? IssueGlyph.issue;
49 const title = detail?.title ?? link.title;
50 const anchorProps = {
51 href: link.url,
52 target: "_blank",
53 rel: "noopener noreferrer",
54 "aria-label": gt`Issue ${{ key: link.displayKey }}: ${{ title }}`,
55 onClick: (event: MouseEvent<HTMLElement>) => {
56 event.stopPropagation();
57 if (event.metaKey || event.ctrlKey || event.shiftKey || event.altKey) return;
58 if (configs.get(threadRef.environmentId)?.environment.capabilities.issues !== true) return;
59 event.preventDefault();
60 useRightPanelStore.getState().openIssue(threadRef, link);
61 onOpen?.();
62 },
63 };
64 const glyph = (
65 <Icon
66 aria-hidden={state === null ? true : undefined}
67 role={state === null ? undefined : "img"}
68 aria-label={state?.label}
69 className={cn("size-4 shrink-0", state?.toneClassName ?? "text-muted-foreground")}
70 />
71 );
72 const comments =
73 detail !== null && detail.commentCount > 0 ? (
74 <span
75 role="img"
76 aria-label={gt`${plural(
77 { count: detail.commentCount },
78 {
79 one: part`${{ count: detail.commentCount }} comment`,
80 other: part`${{ count: detail.commentCount }} comments`,
81 },
82 )}`}
83 className="inline-flex shrink-0 items-center gap-0.5 tabular-nums"
84 >
85 <MessageSquareIcon aria-hidden className="size-3" />
86 <span aria-hidden>{detail.commentCount}</span>
87 </span>
88 ) : null;
89 const assignedActors = detail?.assignees ?? [];
90 const [firstAssignee] = assignedActors;
91 const assigneeLabel = firstAssignee
92 ? gt`Assignees: ${{
93 names: new Intl.ListFormat(locale).format(
94 assignedActors.map((assignee) => assignee.name || assignee.login),
95 ),
96 }}`
97 : undefined;
98 const assignees = firstAssignee ? (
99 <SharedTooltipTrigger
100 render={
101 <span role="img" aria-label={assigneeLabel} className="flex shrink-0 items-center gap-1" />
102 }
103 label={assigneeLabel}
104 disabled={mountScope === "sidebar-tooltip"}
105 >
106 <span className="flex items-center -space-x-1">
107 {assignedActors.slice(0, 3).map((assignee) => (
108 <PullRequestActorAvatar
109 key={assignee.login}
110 actor={assignee}
111 className="ring-1 ring-background"
112 />
113 ))}
114 </span>
115 {assignedActors.length > 3 ? <span aria-hidden>+{assignedActors.length - 3}</span> : null}
116 </SharedTooltipTrigger>
117 ) : null;
118 const status =
119 assignees || comments ? (
120 <>
121 {assignees}
122 {comments}
123 </>
124 ) : null;
125 const trackerIcon = (
126 <trackerPresentation.Icon
127 role="img"
128 aria-label={trackerPresentation.providerName}
129 className="size-3 shrink-0"
130 />
131 );
132 const lines = (
133 <>
134 {glyph}
135 <PullRequestRowLines
136 number={<span className={PULL_REQUEST_ROW_NUMBER_CLASS}>{link.displayKey}</span>}
137 title={title}
138 status={status}
139 meta={
140 <>
141 {detail?.author ? (
142 <PullRequestActorLabel actor={detail.author} tooltip={false} />
143 ) : null}
144 {trackerIcon}
145 <span className="truncate">{link.scope}</span>
146 </>
147 }
148 updatedAt={detail?.updatedAt ?? link.linkedAt}
149 />
150 </>
151 );
152 return compact ? (
153 <div className="pb-1.5">
154 <SharedTooltipTrigger
155 render={<ThreadDetailsControl part="title" render={<a {...anchorProps} />} />}
156 label={
157 <>
158 <div className="flex min-w-0 items-center gap-2">{lines}</div>
159 </>
160 }
161 align="start"
162 variant="glass"
163 className="w-80 max-w-[calc(100vw-2rem)]"
164 >
165 {glyph}
166 <span className="min-w-0 flex-1 line-clamp-2 whitespace-normal leading-4.5">{title}</span>
167 </SharedTooltipTrigger>
168 <div className="flex h-5 min-w-0 items-center gap-1.5 ps-9 pe-2.5 text-xs text-muted-foreground">
169 <span aria-hidden className="shrink-0 tabular-nums">
170 {link.displayKey}
171 </span>
172 {trackerIcon}
173 {status}
174 </div>
175 </div>
176 ) : (
177 <a
178 {...anchorProps}
179 className={cn(
180 PULL_REQUEST_ROW_CLASS,
181 "px-2 hover:bg-accent/60 focus-visible:outline-2 focus-visible:-outline-offset-2 focus-visible:outline-ring",
182 )}
183 >
184 {lines}
185 </a>
186 );
187}
188
189export function ThreadDetailsIssueRows({
190 links,
191 threadRef,
192 compact = true,
193 mountScope = "thread-details",
194 onOpen,
195}: {
196 links: ReadonlyArray<ThreadIssueLink>;
197 threadRef: ScopedThreadRef;
198 compact?: boolean;
199 mountScope?: "thread-details" | "linked-items" | "sidebar-tooltip";
200 onOpen?: () => void;
201}) {
202 const [expandedThread, setExpandedThread] = useState<string | null>(null);
203 const threadKey = scopedThreadKey(threadRef);
204 const expanded = expandedThread === threadKey;
205 const row = (link: ThreadIssueLink) => (
206 <LinkedIssueRow
207 key={threadIssueKeyOf(link)}
208 link={link}
209 threadRef={threadRef}
210 compact={compact}
211 mountScope={mountScope}
212 onOpen={onOpen}
213 />
214 );
215 const [first, ...rest] = links;
216 if (first === undefined || !compact || rest.length === 0) return <>{links.map(row)}</>;
217 return (
218 <>
219 {row(first)}
220 {expanded ? (
221 <div data-thread-details-issue-list className="flex flex-col">
222 {rest.map(row)}
223 </div>
224 ) : null}
225 <ThreadDetailsMoreButton
226 expanded={expanded}
227 remaining={rest.length}
228 onToggle={() => setExpandedThread(expanded ? null : threadKey)}
229 />
230 </>
231 );
232}