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

ThreadLinkPicker.tsx

116 lines4.7 KB

1import { scopedProjectKey, scopeProjectRef } from "@t3tools/client-runtime/environment";
2import type { EnvironmentId, ThreadId } from "@t3tools/contracts";
3import { CheckIcon, MessageSquareIcon } from "lucide-react";
4import { T, useGT, Var } from "gt-react";
5import { useState } from "react";
6​
7import { useProjects, useVisibleThreadShells } from "~/state/entities";
8import { useConnectedEnvironmentIds, useEnvironment } from "~/state/environments";
9import { useThreadSearch } from "~/state/queries";
10​
11import { threadLinkCandidates, threadLinkMatcher } from "./ThreadLinkPicker.logic";
12import { Command, CommandInput, CommandItem, CommandList } from "./ui/command";
13import { PickerRowContent } from "./ui/picker-row-content";
14​
15type ThreadShell = ReturnType<typeof useVisibleThreadShells>[number];
16​
17/** Search over an environment's unarchived threads, to link one to a pull request or an issue. */
18export function ThreadLinkPicker({
19 environmentId,
20 pending,
21 isLinked,
22 onSelect,
23}: {
24 environmentId: EnvironmentId;
25 pending: boolean;
26 isLinked: (thread: ThreadShell) => boolean;
27 onSelect: (threadId: ThreadId) => void;
28}) {
29 const gt = useGT();
30 const threads = useVisibleThreadShells();
31 const projects = useProjects();
32 const otherEnvironmentIds = useConnectedEnvironmentIds().filter((id) => id !== environmentId);
33 const environmentLabel = useEnvironment(environmentId)?.label ?? gt`this environment`;
34 const [query, setQuery] = useState("");
35 // Mounts older settled threads matching the query, which shells may leave out.
36 const localSearch = useThreadSearch([environmentId], query, { titlesOnly: true });
37 const otherSearch = useThreadSearch(otherEnvironmentIds, query, { titlesOnly: true });
38 const projectNames = new Map(
39 projects.map((project) => [
40 scopedProjectKey(scopeProjectRef(project.environmentId, project.id)),
41 project.title,
42 ]),
43 );
44 const projectNameFor = (thread: ThreadShell) =>
45 projectNames.get(scopedProjectKey(scopeProjectRef(thread.environmentId, thread.projectId)));
46 const matches = threadLinkMatcher(query);
47 const { candidates, matchesElsewhere } = threadLinkCandidates(threads, environmentId, (thread) =>
48 matches(`${thread.title} ${projectNameFor(thread) ?? ""}`),
49 );
50 // An older match elsewhere only reaches the shells once its environment's search answers.
51 const searchStatus = localSearch.isPending
52 ? gt`Searching…`
53 : localSearch.failed
54 ? gt`Search failed`
55 : otherSearch.isPending && !matchesElsewhere
56 ? gt`Searching…`
57 : null;
58 return (
59 <Command mode="none" value={query} onValueChange={setQuery} aria-label={gt`Choose a thread`}>
60 <CommandInput placeholder={gt`Search threads or projects...`} disabled={pending} />
61 <CommandList className="max-h-80 overflow-y-auto">
62 {candidates.length === 0 ? (
63 <div role="status" className="px-3 py-6 text-center text-sm text-muted-foreground">
64 {searchStatus ??
65 (matchesElsewhere ? (
66 query.trim() === "" ? (
67 <T>
68 No active threads on <Var>{environmentLabel}</Var>. Threads on other
69 environments can't be linked here.
70 </T>
71 ) : (
72 <T>
73 No matching active threads on <Var>{environmentLabel}</Var>. Threads on other
74 environments can't be linked here.
75 </T>
76 )
77 ) : (
78 <T>No active threads found.</T>
79 ))}
80 </div>
81 ) : (
82 candidates.map((thread) => {
83 const linked = isLinked(thread);
84 const projectName = projectNameFor(thread);
85 return (
86 <CommandItem
87 key={thread.id}
88 value={thread.id}
89 disabled={pending || linked}
90 onClick={() => onSelect(thread.id)}
91 >
92 <PickerRowContent
93 icon={<MessageSquareIcon aria-hidden className="size-4 shrink-0" />}
94 title={<span className="truncate">{thread.title || gt`Untitled thread`}</span>}
95 description={
96 projectName ? (
97 <span className="truncate text-xs text-muted-foreground">{projectName}</span>
98 ) : null
99 }
100 />
101 {linked ? (
102 <>
103 <CheckIcon aria-hidden className="size-3.5" />
104 <span className="text-xs text-muted-foreground">
105 <T context="thread is linked">Linked</T>
106 </span>
107 </>
108 ) : null}
109 </CommandItem>
110 );
111 })
112 )}
113 </CommandList>
114 </Command>
115 );