The best way to control Claude Code, Codex and more from any computer or phone
| 1 | import type { DraftId } from "~/composerDraftStore"; |
| 2 | import { composerDraftHasUserContent, useComposerDraftStore } from "~/composerDraftStore"; |
| 3 | import { resolveEnvironmentMachineKind, type ScopedProjectRef } from "@t3tools/contracts"; |
| 4 | import { scopedProjectKey, scopeProjectRef } from "@t3tools/client-runtime/environment"; |
| 5 | import { isScratchProject } from "@t3tools/client-runtime/state/projects"; |
| 6 | import { T, useGT, Var } from "gt-react"; |
| 7 | import { FolderPlusIcon } from "lucide-react"; |
| 8 | import { useAtomValue } from "@effect/atom-react"; |
| 9 | import { useRef, useState } from "react"; |
| 10 | |
| 11 | import { openCommandPalette } from "~/commandPaletteBus"; |
| 12 | import { shortcutLabelForCommand } from "~/keybindings"; |
| 13 | import { primaryServerKeybindingsAtom } from "~/state/server"; |
| 14 | import { useNewThreadHandler } from "~/hooks/useHandleNewThread"; |
| 15 | import { useOrderWhileOpen } from "~/hooks/useOrderWhileOpen"; |
| 16 | import { useProjectPin } from "~/hooks/useProjectGroupUpdate"; |
| 17 | import { useScratchProject } from "~/hooks/useScratchProject"; |
| 18 | import { isTriagePrompt } from "@t3tools/shared/triagePrompt"; |
| 19 | import { useClientSettings } from "~/hooks/useSettings"; |
| 20 | import { seedDraftModelSelection } from "~/lib/seedDraftModelSelection"; |
| 21 | import { |
| 22 | deriveLogicalProjectKeyFromSettings, |
| 23 | pickableProjectGroups, |
| 24 | selectProjectGroupingSettings, |
| 25 | } from "@t3tools/client-runtime/state/project-grouping"; |
| 26 | import { |
| 27 | buildSidebarProjectPickerEntries, |
| 28 | projectGroupsSpanEnvironments, |
| 29 | } from "~/sidebarProjectGrouping"; |
| 30 | import { useProjects } from "~/state/entities"; |
| 31 | import { useEnvironments, usePrimaryEnvironmentId } from "~/state/environments"; |
| 32 | import { ProjectEnvironmentBadge } from "../ProjectEnvironmentBadge"; |
| 33 | import { ProjectFavicon } from "../ProjectFavicon"; |
| 34 | import { isProjectPinShortcut, ProjectPinButton } from "../ProjectPinButton"; |
| 35 | import { useSortedProjectGroups } from "../CommandPaletteLookups"; |
| 36 | import { |
| 37 | Combobox, |
| 38 | ComboboxEmpty, |
| 39 | ComboboxItem, |
| 40 | ComboboxList, |
| 41 | ComboboxPopup, |
| 42 | ComboboxSearchInput, |
| 43 | ComboboxTrigger, |
| 44 | useComboboxFilter, |
| 45 | } from "../ui/combobox"; |
| 46 | import { Tooltip, TooltipPopup, TooltipTrigger } from "../ui/tooltip"; |
| 47 | import { InlineButton } from "../ui/button"; |
| 48 | import { DraftProjectName } from "./DraftProjectName"; |
| 49 | import { DraftHeroProjectHeading } from "./DraftHeroProjectHeading"; |
| 50 | import { TriageDraftHero } from "./TriageDraftHero"; |
| 51 | |
| 52 | const ADD_PROJECT_VALUE = "add-project"; |
| 53 | |
| 54 | interface PickerItem { |
| 55 | readonly value: string; |
| 56 | readonly label: string; |
| 57 | } |
| 58 | |
| 59 | interface DraftHeroHeadlineProps { |
| 60 | readonly draftId: DraftId | null; |
| 61 | readonly activeProjectRef: ScopedProjectRef | null; |
| 62 | readonly activeProjectTitle: string | null; |
| 63 | } |
| 64 | |
| 65 | export function DraftHeroHeadline({ |
| 66 | draftId, |
| 67 | activeProjectRef, |
| 68 | activeProjectTitle, |
| 69 | }: DraftHeroHeadlineProps) { |
| 70 | const gt = useGT(); |
| 71 | const projects = useProjects(); |
| 72 | const { environments } = useEnvironments(); |
| 73 | const primaryEnvironmentId = usePrimaryEnvironmentId(); |
| 74 | const projectGroupingSettings = useClientSettings(selectProjectGroupingSettings); |
| 75 | const setLogicalProjectDraftThreadId = useComposerDraftStore( |
| 76 | (store) => store.setLogicalProjectDraftThreadId, |
| 77 | ); |
| 78 | const getComposerDraft = useComposerDraftStore((store) => store.getComposerDraft); |
| 79 | // A boolean, so typing in a draft does not re-render the hero. |
| 80 | const isTriageDraftPrompt = useComposerDraftStore((store) => |
| 81 | draftId ? isTriagePrompt(store.getComposerDraft(draftId)?.prompt ?? "") : false, |
| 82 | ); |
| 83 | const openAddProject = () => openCommandPalette({ open: "add-project" }); |
| 84 | const { scratchEnvironmentId, scratchWorkspaceRootFor, openScratchProject } = useScratchProject(); |
| 85 | const openProjectDraft = useNewThreadHandler(); |
| 86 | const keybindings = useAtomValue(primaryServerKeybindingsAtom); |
| 87 | const [pickerOpen, setPickerOpen] = useState(false); |
| 88 | const [pickerQuery, setPickerQuery] = useState(""); |
| 89 | const pickerFilter = useComboboxFilter(); |
| 90 | const highlightedPickerValueRef = useRef<string | null>(null); |
| 91 | const { canPinProjectGroup, toggleProjectGroupPin } = useProjectPin(); |
| 92 | |
| 93 | const environmentLabelById = new Map( |
| 94 | environments.map((environment) => [environment.environmentId, environment.label] as const), |
| 95 | ); |
| 96 | const projectGroups = useSortedProjectGroups(environmentLabelById); |
| 97 | const showProjectEnvironments = projectGroupsSpanEnvironments(projectGroups); |
| 98 | const environmentMachineById = new Map( |
| 99 | environments.map( |
| 100 | (environment) => |
| 101 | [ |
| 102 | environment.environmentId, |
| 103 | resolveEnvironmentMachineKind(environment.serverConfig), |
| 104 | ] as const, |
| 105 | ), |
| 106 | ); |
| 107 | const projectPickerEntries = buildSidebarProjectPickerEntries({ |
| 108 | groups: pickableProjectGroups(projectGroups), |
| 109 | preferredProjectRef: activeProjectRef, |
| 110 | }); |
| 111 | const projectEntryByKey = new Map( |
| 112 | projectPickerEntries.map((entry) => [entry.group.projectKey, entry] as const), |
| 113 | ); |
| 114 | const activeProjectGroup = |
| 115 | activeProjectRef === null |
| 116 | ? null |
| 117 | : (projectGroups.find((group) => |
| 118 | group.memberProjectRefs.some( |
| 119 | (projectRef) => scopedProjectKey(projectRef) === scopedProjectKey(activeProjectRef), |
| 120 | ), |
| 121 | ) ?? null); |
| 122 | const activeProjectKey = activeProjectGroup?.projectKey ?? ""; |
| 123 | const hasResolvedProject = activeProjectTitle !== null; |
| 124 | const canChooseProject = projectPickerEntries.length > 0; |
| 125 | const shouldShowProjectMenu = canChooseProject; |
| 126 | const menuEntries = projectPickerEntries.filter( |
| 127 | ({ targetProject }) => |
| 128 | !isScratchProject(targetProject, scratchWorkspaceRootFor(targetProject.environmentId)), |
| 129 | ); |
| 130 | const activeProject = |
| 131 | activeProjectRef === null |
| 132 | ? null |
| 133 | : (projects.find( |
| 134 | (project) => |
| 135 | project.environmentId === activeProjectRef.environmentId && |
| 136 | project.id === activeProjectRef.projectId, |
| 137 | ) ?? null); |
| 138 | const scratchTargetEnvironmentId = scratchEnvironmentId(activeProjectRef?.environmentId ?? null); |
| 139 | const scratchWorkspaceRoot = scratchWorkspaceRootFor(scratchTargetEnvironmentId); |
| 140 | const isScratchDraft = |
| 141 | activeProject !== null && |
| 142 | isScratchProject(activeProject, scratchWorkspaceRootFor(activeProject.environmentId)); |
| 143 | |
| 144 | // Pinning from a row keeps rows in place while the picker is open. |
| 145 | const pickerItems: readonly PickerItem[] = useOrderWhileOpen( |
| 146 | [ |
| 147 | ...menuEntries.map(({ group }) => ({ value: group.projectKey, label: group.displayName })), |
| 148 | { value: ADD_PROJECT_VALUE, label: gt("Add project") }, |
| 149 | ], |
| 150 | (item) => item.value, |
| 151 | pickerOpen, |
| 152 | ); |
| 153 | const pickerSearch = pickerQuery.trim(); |
| 154 | const filteredPickerItems = |
| 155 | pickerSearch.length === 0 |
| 156 | ? pickerItems |
| 157 | : pickerItems.filter( |
| 158 | (item) => |
| 159 | item.value !== ADD_PROJECT_VALUE && |
| 160 | pickerFilter.contains(item, pickerSearch, (candidate) => candidate.label), |
| 161 | ); |
| 162 | const selectedPickerItem = pickerItems.find((item) => item.value === activeProjectKey) ?? null; |
| 163 | |
| 164 | // The picker can change the draft's target while the no-project home is |
| 165 | // still being opened; a stale continuation must not retarget it again. Picking |
| 166 | // a project clears the pending request, and the draft session is compared |
| 167 | // against the store when the scratch project arrives. |
| 168 | const pendingScratchRef = useRef<symbol | null>(null); |
| 169 | const draftTargetKey = () => { |
| 170 | const session = draftId ? useComposerDraftStore.getState().getDraftSession(draftId) : null; |
| 171 | return session |
| 172 | ? `${session.environmentId}:${session.projectId}:${session.logicalProjectKey}` |
| 173 | : null; |
| 174 | }; |
| 175 | // Retargeting an empty draft would overwrite the project's draft and strand browser tabs. |
| 176 | const selectProject = (project: (typeof projects)[number], logicalProjectKey: string) => { |
| 177 | if (!draftId) { |
| 178 | return; |
| 179 | } |
| 180 | pendingScratchRef.current = null; |
| 181 | const projectRef = scopeProjectRef(project.environmentId, project.id); |
| 182 | if (!composerDraftHasUserContent(getComposerDraft(draftId))) { |
| 183 | void openProjectDraft(projectRef); |
| 184 | return; |
| 185 | } |
| 186 | setLogicalProjectDraftThreadId(logicalProjectKey, projectRef, draftId); |
| 187 | seedDraftModelSelection(draftId, projectRef); |
| 188 | }; |
| 189 | const startScratch = async (): Promise<boolean> => { |
| 190 | if (scratchTargetEnvironmentId === null || isScratchDraft) { |
| 191 | return false; |
| 192 | } |
| 193 | const request = Symbol("scratch"); |
| 194 | pendingScratchRef.current = request; |
| 195 | const requestedTarget = draftTargetKey(); |
| 196 | const project = await openScratchProject(scratchTargetEnvironmentId); |
| 197 | if (!project || pendingScratchRef.current !== request || draftTargetKey() !== requestedTarget) { |
| 198 | return false; |
| 199 | } |
| 200 | pendingScratchRef.current = null; |
| 201 | selectProject(project, deriveLogicalProjectKeyFromSettings(project, projectGroupingSettings)); |
| 202 | return true; |
| 203 | }; |
| 204 | |
| 205 | const projectSelector = shouldShowProjectMenu ? ( |
| 206 | <Combobox |
| 207 | items={pickerItems} |
| 208 | filteredItems={filteredPickerItems} |
| 209 | autoHighlight |
| 210 | itemToStringLabel={(item) => item.label} |
| 211 | isItemEqualToValue={(a, b) => a.value === b.value} |
| 212 | open={pickerOpen} |
| 213 | onItemHighlighted={(item) => { |
| 214 | highlightedPickerValueRef.current = item?.value ?? null; |
| 215 | }} |
| 216 | onOpenChange={(open) => { |
| 217 | setPickerOpen(open); |
| 218 | setPickerQuery(""); |
| 219 | }} |
| 220 | value={selectedPickerItem} |
| 221 | onValueChange={(item) => { |
| 222 | if (!item) return; |
| 223 | setPickerOpen(false); |
| 224 | if (item.value === ADD_PROJECT_VALUE) { |
| 225 | openAddProject(); |
| 226 | return; |
| 227 | } |
| 228 | const entry = projectEntryByKey.get(item.value); |
| 229 | if (!entry || item.value === activeProjectKey) { |
| 230 | return; |
| 231 | } |
| 232 | selectProject(entry.targetProject, entry.group.projectKey); |
| 233 | }} |
| 234 | > |
| 235 | <Tooltip> |
| 236 | <TooltipTrigger |
| 237 | render={ |
| 238 | <ComboboxTrigger |
| 239 | render={<InlineButton tone="picker" />} |
| 240 | data-draft-project-trigger="" |
| 241 | className="pointer-events-auto min-w-0 max-w-full align-baseline" |
| 242 | /> |
| 243 | } |
| 244 | > |
| 245 | <DraftProjectName |
| 246 | value={ |
| 247 | isScratchDraft ? gt("No project") : (activeProjectTitle ?? gt("Choose a project")) |
| 248 | } |
| 249 | singleLine |
| 250 | /> |
| 251 | </TooltipTrigger> |
| 252 | {activeProjectTitle && !isScratchDraft ? ( |
| 253 | <TooltipPopup side="top">{activeProjectTitle}</TooltipPopup> |
| 254 | ) : null} |
| 255 | </Tooltip> |
| 256 | <ComboboxPopup align="center" className="w-72 overflow-hidden"> |
| 257 | <ComboboxSearchInput |
| 258 | aria-label={gt("Search projects")} |
| 259 | placeholder={gt("Search projects...")} |
| 260 | value={pickerQuery} |
| 261 | onChange={(event) => setPickerQuery(event.target.value)} |
| 262 | onKeyDown={(event) => { |
| 263 | const highlighted = highlightedPickerValueRef.current; |
| 264 | const group = highlighted ? projectEntryByKey.get(highlighted)?.group : undefined; |
| 265 | if (!group || !canPinProjectGroup(group) || !isProjectPinShortcut(event, keybindings)) { |
| 266 | return; |
| 267 | } |
| 268 | event.preventDefault(); |
| 269 | event.stopPropagation(); |
| 270 | toggleProjectGroupPin(group); |
| 271 | }} |
| 272 | /> |
| 273 | <ComboboxEmpty> |
| 274 | <T>No matching projects.</T> |
| 275 | </ComboboxEmpty> |
| 276 | <ComboboxList> |
| 277 | {(item: PickerItem) => { |
| 278 | const entry = projectEntryByKey.get(item.value); |
| 279 | return ( |
| 280 | <ComboboxItem key={item.value} hideIndicator value={item}> |
| 281 | {item.value === ADD_PROJECT_VALUE ? ( |
| 282 | <FolderPlusIcon className="size-4 shrink-0" /> |
| 283 | ) : entry ? ( |
| 284 | <ProjectFavicon project={entry.group} className="size-4 shrink-0" /> |
| 285 | ) : null} |
| 286 | <Tooltip> |
| 287 | <TooltipTrigger render={<span className="min-w-0 flex-1 text-sm" />}> |
| 288 | <DraftProjectName value={item.label} /> |
| 289 | </TooltipTrigger> |
| 290 | <TooltipPopup side="top">{item.label}</TooltipPopup> |
| 291 | </Tooltip> |
| 292 | {entry && canPinProjectGroup(entry.group) ? ( |
| 293 | <ProjectPinButton |
| 294 | name={entry.group.displayName} |
| 295 | pinned={entry.group.pinned} |
| 296 | shortcutLabel={shortcutLabelForCommand(keybindings, "thread.pin")} |
| 297 | onToggle={() => toggleProjectGroupPin(entry.group)} |
| 298 | /> |
| 299 | ) : null} |
| 300 | {entry && showProjectEnvironments ? ( |
| 301 | <ProjectEnvironmentBadge |
| 302 | group={entry.group} |
| 303 | primaryEnvironmentId={primaryEnvironmentId} |
| 304 | machineByEnvironmentId={environmentMachineById} |
| 305 | /> |
| 306 | ) : null} |
| 307 | </ComboboxItem> |
| 308 | ); |
| 309 | }} |
| 310 | </ComboboxList> |
| 311 | </ComboboxPopup> |
| 312 | </Combobox> |
| 313 | ) : ( |
| 314 | <button |
| 315 | type="button" |
| 316 | onClick={openAddProject} |
| 317 | className="pointer-events-auto inline min-w-0 max-w-full cursor-pointer border-muted-foreground/35 border-b border-dotted text-muted-foreground/60 transition-colors hover:border-muted-foreground/60 hover:text-muted-foreground/80 focus-visible:rounded-sm focus-visible:outline-hidden focus-visible:ring-2 focus-visible:ring-inset focus-visible:ring-ring" |
| 318 | > |
| 319 | <DraftProjectName value={activeProjectTitle ?? gt("Add a project")} singleLine /> |
| 320 | </button> |
| 321 | ); |
| 322 | |
| 323 | const noProjectShortcut = shortcutLabelForCommand(keybindings, "chat.newWithoutProject"); |
| 324 | const orStartWithoutProject = |
| 325 | scratchWorkspaceRoot !== null && !isScratchDraft && (hasResolvedProject || canChooseProject) ? ( |
| 326 | <Tooltip> |
| 327 | <TooltipTrigger |
| 328 | render={ |
| 329 | <InlineButton |
| 330 | tone="muted" |
| 331 | className="pointer-events-auto" |
| 332 | onClick={() => |
| 333 | void startScratch().then((started) => { |
| 334 | if (started) { |
| 335 | document.querySelector<HTMLElement>("[data-draft-project-trigger]")?.focus(); |
| 336 | } |
| 337 | }) |
| 338 | } |
| 339 | /> |
| 340 | } |
| 341 | > |
| 342 | <T>or start without a project</T> |
| 343 | </TooltipTrigger> |
| 344 | {noProjectShortcut ? <TooltipPopup side="bottom">{noProjectShortcut}</TooltipPopup> : null} |
| 345 | </Tooltip> |
| 346 | ) : null; |
| 347 | |
| 348 | if (isScratchDraft && isTriageDraftPrompt) { |
| 349 | const environment = environments.find( |
| 350 | (entry) => entry.environmentId === activeProject.environmentId, |
| 351 | ); |
| 352 | return ( |
| 353 | <TriageDraftHero |
| 354 | machineLabel={environment?.label ?? null} |
| 355 | serverVersion={environment?.serverConfig?.environment.serverVersion ?? null} |
| 356 | /> |
| 357 | ); |
| 358 | } |
| 359 | |
| 360 | return ( |
| 361 | <div className="mx-auto flex w-full max-w-5xl flex-col items-center"> |
| 362 | {hasResolvedProject && !isScratchDraft ? ( |
| 363 | <DraftHeroProjectHeading projectName={activeProjectTitle ?? gt("Choose a project")}> |
| 364 | {projectSelector} |
| 365 | </DraftHeroProjectHeading> |
| 366 | ) : ( |
| 367 | <h1 className="w-full text-center font-normal text-2xl text-foreground tracking-tight sm:text-3xl"> |
| 368 | {isScratchDraft ? ( |
| 369 | <T>What should we work on?</T> |
| 370 | ) : canChooseProject ? ( |
| 371 | <T> |
| 372 | <Var>{projectSelector}</Var> to start |
| 373 | </T> |
| 374 | ) : ( |
| 375 | <T>Add a project to start</T> |
| 376 | )} |
| 377 | </h1> |
| 378 | )} |
| 379 | {scratchWorkspaceRoot === null ? null : ( |
| 380 | <p className="mt-2 flex h-6 w-full items-center justify-center text-sm"> |
| 381 | {isScratchDraft ? projectSelector : orStartWithoutProject} |
| 382 | </p> |
| 383 | )} |
| 384 | </div> |
| 385 | ); |
| 386 | } |