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