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, EnvironmentMachineKind } from "@t3tools/contracts"; |
| 2 | import { T, useGT } from "gt-react"; |
| 3 | |
| 4 | import { m, type MessageResolver } from "../i18n/messages"; |
| 5 | import type { SidebarProjectSnapshot } from "../sidebarProjectGrouping"; |
| 6 | import type { Project } from "../types"; |
| 7 | import { |
| 8 | buildCommandPaletteProjectMetadata, |
| 9 | buildProjectActionItems, |
| 10 | type CommandPaletteItemPin, |
| 11 | type CommandPaletteProject, |
| 12 | ITEM_ICON_CLASS, |
| 13 | } from "./CommandPalette.logic"; |
| 14 | import { EnvironmentMachineIcon } from "./EnvironmentMachineIcon"; |
| 15 | import { ProjectFavicon } from "./ProjectFavicon"; |
| 16 | import { COMMAND_PALETTE_META_ICON_CLASS, CommandPaletteMetaDot } from "./ThreadCommandSubtitle"; |
| 17 | |
| 18 | export interface CommandPaletteProjectLocation { |
| 19 | readonly kind: "local" | "remote"; |
| 20 | readonly label: string; |
| 21 | readonly machine: EnvironmentMachineKind; |
| 22 | } |
| 23 | |
| 24 | interface ProjectItemsInput { |
| 25 | readonly projects: ReadonlyArray<CommandPaletteProject>; |
| 26 | readonly projectGroupByTargetKey: ReadonlyMap<string, SidebarProjectSnapshot>; |
| 27 | readonly locationByEnvironmentId: ReadonlyMap<EnvironmentId, CommandPaletteProjectLocation>; |
| 28 | readonly runProject: (project: CommandPaletteProject) => Promise<void>; |
| 29 | readonly pin?: (project: CommandPaletteProject) => CommandPaletteItemPin | null; |
| 30 | } |
| 31 | |
| 32 | function projectFaviconIcon(project: Project) { |
| 33 | return <ProjectFavicon project={project} className={ITEM_ICON_CLASS} />; |
| 34 | } |
| 35 | |
| 36 | /** Projects as root search results; a grouped project matches on every member. */ |
| 37 | export function buildProjectSearchItems(input: ProjectItemsInput, resolve: MessageResolver = m) { |
| 38 | const membersOf = (project: CommandPaletteProject) => |
| 39 | input.projectGroupByTargetKey.get(`${project.environmentId}:${project.id}`)?.memberProjects ?? [ |
| 40 | project, |
| 41 | ]; |
| 42 | return buildProjectActionItems({ |
| 43 | projects: input.projects, |
| 44 | valuePrefix: "project", |
| 45 | searchTerms: (project) => |
| 46 | buildCommandPaletteProjectMetadata( |
| 47 | { |
| 48 | projects: membersOf(project), |
| 49 | locationByEnvironmentId: input.locationByEnvironmentId, |
| 50 | }, |
| 51 | resolve, |
| 52 | ).searchTerms, |
| 53 | renderDescription: (project) => { |
| 54 | const members = membersOf(project); |
| 55 | const metadata = buildCommandPaletteProjectMetadata( |
| 56 | { |
| 57 | projects: members, |
| 58 | locationByEnvironmentId: input.locationByEnvironmentId, |
| 59 | }, |
| 60 | resolve, |
| 61 | ); |
| 62 | return members.length > 1 ? ( |
| 63 | <span className="truncate">{metadata.environmentLabels.join(" · ")}</span> |
| 64 | ) : ( |
| 65 | <ProjectLocationDescription |
| 66 | location={input.locationByEnvironmentId.get(project.environmentId)} |
| 67 | workspaceRoot={project.workspaceRoot} |
| 68 | /> |
| 69 | ); |
| 70 | }, |
| 71 | icon: projectFaviconIcon, |
| 72 | runProject: input.runProject, |
| 73 | ...(input.pin ? { pin: input.pin } : {}), |
| 74 | }); |
| 75 | } |
| 76 | |
| 77 | /** Projects as targets of the "New thread in..." submenu. */ |
| 78 | export function buildNewThreadInProjectItems( |
| 79 | input: ProjectItemsInput & { readonly currentProjectKey: string | null }, |
| 80 | ) { |
| 81 | return buildProjectActionItems({ |
| 82 | projects: input.projects, |
| 83 | valuePrefix: "new-thread-in", |
| 84 | searchTerms: (project) => { |
| 85 | const group = input.projectGroupByTargetKey.get(`${project.environmentId}:${project.id}`); |
| 86 | const location = input.locationByEnvironmentId.get(project.environmentId); |
| 87 | return [ |
| 88 | ...(group?.memberProjects.flatMap((member) => [member.title, member.workspaceRoot]) ?? []), |
| 89 | ...(location ? [location.label] : []), |
| 90 | ]; |
| 91 | }, |
| 92 | renderDescription: (project) => ( |
| 93 | <ProjectLocationDescription |
| 94 | location={input.locationByEnvironmentId.get(project.environmentId)} |
| 95 | workspaceRoot={project.workspaceRoot} |
| 96 | current={input.currentProjectKey === `${project.environmentId}:${project.id}`} |
| 97 | /> |
| 98 | ), |
| 99 | icon: projectFaviconIcon, |
| 100 | runProject: input.runProject, |
| 101 | ...(input.pin ? { pin: input.pin } : {}), |
| 102 | }); |
| 103 | } |
| 104 | |
| 105 | function ProjectLocationDescription(props: { |
| 106 | readonly location: CommandPaletteProjectLocation | undefined; |
| 107 | readonly workspaceRoot: string; |
| 108 | readonly current?: boolean; |
| 109 | }) { |
| 110 | const gt = useGT(); |
| 111 | const location: CommandPaletteProjectLocation = props.location ?? { |
| 112 | kind: "remote", |
| 113 | label: gt("Remote", { $context: "machine location" }), |
| 114 | machine: "server", |
| 115 | }; |
| 116 | return ( |
| 117 | <span className="flex min-w-0 items-center gap-1"> |
| 118 | {props.current ? ( |
| 119 | <> |
| 120 | <span className="shrink-0"> |
| 121 | <T context="this project is the current one">Current</T> |
| 122 | </span> |
| 123 | <CommandPaletteMetaDot /> |
| 124 | </> |
| 125 | ) : null} |
| 126 | <span className="inline-flex min-w-0 items-center gap-1"> |
| 127 | {location.kind === "remote" ? ( |
| 128 | <EnvironmentMachineIcon |
| 129 | aria-hidden |
| 130 | kind={location.machine} |
| 131 | className={COMMAND_PALETTE_META_ICON_CLASS} |
| 132 | /> |
| 133 | ) : null} |
| 134 | <span className="truncate">{location.label}</span> |
| 135 | </span> |
| 136 | <CommandPaletteMetaDot /> |
| 137 | <span className="min-w-0 flex-1 truncate">{props.workspaceRoot}</span> |
| 138 | </span> |
| 139 | ); |
| 140 | } |