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

ThreadLinkPicker.tsx

87 lines3.2 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 { Command, CommandInput, CommandItem, CommandList } from "./ui/command";
10​
11type ThreadShell = ReturnType<typeof useVisibleThreadShells>[number];
12​
13/** Search over an environment's unarchived threads, to link one to a pull request or an issue. */
14export function ThreadLinkPicker({
15 environmentId,
16 pending,
17 isLinked,
18 onSelect,
19}: {
20 environmentId: EnvironmentId;
21 pending: boolean;
22 isLinked: (thread: ThreadShell) => boolean;
23 onSelect: (threadId: ThreadId) => void;
24}) {
25 const gt = useGT();
26 const threads = useVisibleThreadShells();
27 const projects = useProjects();
28 const [query, setQuery] = useState("");
29 // Mounts older settled threads matching the query, which shells may leave out.
30 useThreadSearch([environmentId], query, { titlesOnly: true });
31 const projectNames = new Map(
32 projects
33 .filter((project) => project.environmentId === environmentId)
34 .map((project) => [project.id, project.title]),
35 );
36 const search = query.trim().toLocaleLowerCase();
37 const candidates = threads
38 .filter(
39 (thread) =>
40 thread.environmentId === environmentId &&
41 thread.archivedAt === null &&
42 `${thread.title} ${projectNames.get(thread.projectId) ?? ""}`
43 .toLocaleLowerCase()
44 .includes(search),
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 return (
59 <CommandItem
60 key={thread.id}
61 value={thread.id}
62 disabled={pending || linked}
63 onClick={() => onSelect(thread.id)}
64 >
65 <MessageSquareIcon aria-hidden className="size-4 shrink-0" />
66 <span className="flex min-w-0 flex-1 flex-col">
67 <span className="truncate">{thread.title || gt("Untitled thread")}</span>
68 <span className="truncate text-xs text-muted-foreground">
69 {projectNames.get(thread.projectId)}
70 </span>
71 </span>
72 {linked ? (
73 <>
74 <CheckIcon aria-hidden className="size-3.5" />
75 <span className="text-xs text-muted-foreground">
76 <T context="thread is linked">Linked</T>
77 </span>
78 </>
79 ) : null}
80 </CommandItem>
81 );
82 })
83 )}
84 </CommandList>
85 </Command>
86 );