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

ThreadLinkPicker.tsx

90 lines3.4 KB

1import type { EnvironmentId, ThreadId } from "@t3tools/contracts";
2import { CheckIcon, MessageSquareIcon } from "lucide-react";
3import { T, useGT } from "gt-react";
4import { useState } from "react";
5​
6import { useProjects, useVisibleThreadShells } from "~/state/entities";
7import { useThreadSearch } from "~/state/queries";
8​
9import { threadLinkMatcher } from "./ThreadLinkPicker.logic";
10import { Command, CommandInput, CommandItem, CommandList } from "./ui/command";
11import { PickerRowContent } from "./ui/picker-row-content";
12​
13type ThreadShell = ReturnType<typeof useVisibleThreadShells>[number];
14​
15/** Search over an environment's unarchived threads, to link one to a pull request or an issue. */
16export function ThreadLinkPicker({
17 environmentId,
18 pending,
19 isLinked,
20 onSelect,
21}: {
22 environmentId: EnvironmentId;
23 pending: boolean;
24 isLinked: (thread: ThreadShell) => boolean;
25 onSelect: (threadId: ThreadId) => void;
26}) {
27 const gt = useGT();
28 const threads = useVisibleThreadShells();
29 const projects = useProjects();
30 const [query, setQuery] = useState("");
31 // Mounts older settled threads matching the query, which shells may leave out.
32 useThreadSearch([environmentId], query, { titlesOnly: true });
33 const projectNames = new Map(
34 projects
35 .filter((project) => project.environmentId === environmentId)
36 .map((project) => [project.id, project.title]),
37 );
38 const matches = threadLinkMatcher(query);
39 const candidates = threads
40 .filter(
41 (thread) =>
42 thread.environmentId === environmentId &&
43 thread.archivedAt === null &&
44 matches(`${thread.title} ${projectNames.get(thread.projectId) ?? ""}`),
45 )
46 .toSorted((a, b) => b.updatedAt.localeCompare(a.updatedAt));
47 return (
48 <Command mode="none" value={query} onValueChange={setQuery} aria-label={gt`Choose a thread`}>
49 <CommandInput placeholder={gt`Search threads or projects...`} disabled={pending} />
50 <CommandList className="max-h-80 overflow-y-auto">
51 {candidates.length === 0 ? (
52 <div className="px-3 py-6 text-center text-sm text-muted-foreground">
53 <T>No active threads found.</T>
54 </div>
55 ) : (
56 candidates.map((thread) => {
57 const linked = isLinked(thread);
58 const projectName = projectNames.get(thread.projectId);
59 return (
60 <CommandItem
61 key={thread.id}
62 value={thread.id}
63 disabled={pending || linked}
64 onClick={() => onSelect(thread.id)}
65 >
66 <PickerRowContent
67 icon={<MessageSquareIcon aria-hidden className="size-4 shrink-0" />}
68 title={<span className="truncate">{thread.title || gt`Untitled thread`}</span>}
69 description={
70 projectName ? (
71 <span className="truncate text-xs text-muted-foreground">{projectName}</span>
72 ) : null
73 }
74 />
75 {linked ? (
76 <>
77 <CheckIcon aria-hidden className="size-3.5" />
78 <span className="text-xs text-muted-foreground">
79 <T context="thread is linked">Linked</T>
80 </span>
81 </>
82 ) : null}
83 </CommandItem>
84 );
85 })
86 )}
87 </CommandList>
88 </Command>
89 );