Open History to see the last commit
1import { scopeProjectRef, scopeThreadRef } from "@t3tools/client-runtime/environment";
2import type { EnvironmentId, ThreadId } from "@t3tools/contracts";
3import {
4 ChevronDownIcon,
5 FolderGit2Icon,
6 FolderGitIcon,
7 FolderIcon,
8 ScaleIcon,
9} from "lucide-react";
10import {
11 type Ref,
12 memo,
13 useImperativeHandle,
14 useCallback,
15 useEffect,
16 useLayoutEffect,
17 useMemo,
18 useRef,
19 useState,
20} from "react";
21
22import { useComposerDraftStore, type DraftId } from "../composerDraftStore";
23import { EnvironmentMachineIcon } from "./EnvironmentMachineIcon";
24import { useProject, useThreadShell, useThreadShellsForProjectRefs } from "../state/entities";
25import {
26 type EnvMode,
27 type EnvironmentOption,
28 resolveContextStripLabelsCompact,
29 resolveCurrentWorkspaceLabel,
30 resolveEnvModeLabel,
31 resolveEffectiveEnvMode,
32 resolveLockedWorkspaceLabel,
33 resolvePreviousWorktreeLabel,
34 resolvePreviousWorktreeSeed,
35 shouldShowEnvironmentIndicator,
36} from "./BranchToolbar.logic";
37import {
38 BranchToolbarBranchSelector,
39 type BranchToolbarBranchSelectorHandle,
40} from "./BranchToolbarBranchSelector";
41import { BranchToolbarEnvironmentSelector } from "./BranchToolbarEnvironmentSelector";
42import { BranchToolbarEnvModeSelector } from "./BranchToolbarEnvModeSelector";
43import { PreviousWorktreeItemContent } from "./PreviousWorktreeItemContent";
44import { ComposerControl } from "./chat/ComposerControl";
45import {
46 Menu,
47 MenuGroup,
48 MenuGroupLabel,
49 MenuPopup,
50 MenuRadioGroup,
51 MenuRadioItem,
52 MenuSeparator,
53 MenuTrigger,
54} from "./ui/menu";
55import { Separator } from "./ui/separator";
56import { Tooltip, TooltipPopup, TooltipTrigger } from "./ui/tooltip";
57import { MiddleTruncate } from "./ui/middle-truncate";
58import { ComposerSurface } from "./chat/ComposerSurface";
59import { useComposerMenuProps } from "./chat/composerEventScope";
60import { measureRestingComposerControls } from "./chat/restingComposerControlsMeasurement";
61import { resolveRestingComposerControlsNaturalWidth } from "./composerFooterLayout";
62import { cn } from "~/lib/utils";
63
64export interface BranchToolbarHandle {
65 openBranchPicker: () => void;
66 usePreviousWorktree: () => void;
67}
68
69interface BranchToolbarProps {
70 forceNewWorktree?: boolean;
71 ref?: Ref<BranchToolbarHandle>;
72 environmentId: EnvironmentId;
73 threadId: ThreadId;
74 showGitControls: boolean;
75 draftId?: DraftId;
76 onEnvModeChange: (mode: EnvMode) => void;
77 effectiveEnvModeOverride?: EnvMode;
78 activeThreadBranchOverride?: string | null;
79 onActiveThreadBranchOverrideChange?: (branch: string | null) => void;
80 startFromOrigin: boolean;
81 onStartFromOriginChange: (startFromOrigin: boolean) => void;
82 autoEnvironmentLabel?: string | undefined;
83 onAutoEnvironment?: (() => void) | undefined;
84 envLocked: boolean;
85 onCheckoutPullRequestRequest?: (reference: string) => void;
86 onComposerFocusRequest?: () => void;
87 availableEnvironments?: readonly EnvironmentOption[];
88 onEnvironmentChange?: (environmentId: EnvironmentId) => void;
89 composerControlsHostRef?: (element: HTMLDivElement | null) => void;
90 contextStripVisible?: boolean;
91}
92
93interface MobileRunContextSelectorProps {
94 forceNewWorktree: boolean;
95 autoEnvironmentLabel?: string | undefined;
96 onAutoEnvironment?: (() => void) | undefined;
97 envLocked: boolean;
98 envModeLocked: boolean;
99 environmentId: EnvironmentId;
100 availableEnvironments: readonly EnvironmentOption[] | undefined;
101 showEnvironmentPicker: boolean;
102 showEnvironmentIndicator: boolean;
103 onEnvironmentChange: ((environmentId: EnvironmentId) => void) | undefined;
104 effectiveEnvMode: EnvMode;
105 activeWorktreePath: string | null;
106 onEnvModeChange: (mode: EnvMode) => void;
107 previousWorktreeLabel: string | null;
108 previousWorktreeBranch: string | null;
109 onUsePreviousWorktree: () => void;
110}
111
112const MobileRunContextSelector = memo(function MobileRunContextSelector({
113 forceNewWorktree,
114 autoEnvironmentLabel,
115 onAutoEnvironment,
116 envLocked,
117 envModeLocked,
118 environmentId,
119 availableEnvironments,
120 showEnvironmentPicker,
121 showEnvironmentIndicator,
122 onEnvironmentChange,
123 effectiveEnvMode,
124 activeWorktreePath,
125 onEnvModeChange,
126 previousWorktreeLabel,
127 previousWorktreeBranch,
128 onUsePreviousWorktree,
129}: MobileRunContextSelectorProps) {
130 const composerFloatingLayerProps = useComposerMenuProps();
131 const activeEnvironment = useMemo(
132 () => availableEnvironments?.find((env) => env.environmentId === environmentId) ?? null,
133 [availableEnvironments, environmentId],
134 );
135 const WorkspaceIcon =
136 effectiveEnvMode === "worktree"
137 ? FolderGit2Icon
138 : activeWorktreePath
139 ? FolderGitIcon
140 : FolderIcon;
141 const workspaceLabel = forceNewWorktree
142 ? resolveEnvModeLabel("worktree")
143 : envModeLocked
144 ? resolveLockedWorkspaceLabel(activeWorktreePath)
145 : effectiveEnvMode === "worktree"
146 ? resolveEnvModeLabel("worktree")
147 : resolveCurrentWorkspaceLabel(activeWorktreePath);
148 const isLocked = envLocked || envModeLocked;
149 const workspaceIcon = (
150 <Tooltip>
151 <TooltipTrigger render={<span className="inline-flex shrink-0" />}>
152 <WorkspaceIcon className={cn("size-3 shrink-0", showEnvironmentIndicator && "mx-0!")} />
153 </TooltipTrigger>
154 <TooltipPopup>{workspaceLabel}</TooltipPopup>
155 </Tooltip>
156 );
157 const icon = showEnvironmentIndicator ? (
158 // Button's base styles apply `-mx-0.5` to descendant SVGs, which eats 4px
159 // out of whatever gap we set. mx-0! cancels that so gap-0.5 reads as 2px.
160 <span className="inline-flex shrink-0 items-center gap-0.5">
161 <Tooltip>
162 <TooltipTrigger render={<span className="inline-flex shrink-0" />}>
163 {autoEnvironmentLabel ? (
164 <ScaleIcon className="size-3 shrink-0 mx-0!" aria-hidden="true" />
165 ) : (
166 <EnvironmentMachineIcon
167 kind={activeEnvironment?.machine ?? "server"}
168 className="size-3 shrink-0 mx-0!"
169 />
170 )}
171 </TooltipTrigger>
172 <TooltipPopup>{autoEnvironmentLabel ?? activeEnvironment?.label ?? "Run on"}</TooltipPopup>
173 </Tooltip>
174 {workspaceIcon}
175 </span>
176 ) : (
177 workspaceIcon
178 );
179 const triggerContent = (
180 <>
181 {icon}
182 <span
183 data-composer-label
184 className="min-w-0 max-w-[240px] group-data-[compact]/composer-context:max-w-0"
185 >
186 <span
187 data-composer-label-motion
188 className="block w-full min-w-0 max-w-[240px] truncate transition-opacity duration-180 ease-drawer group-data-[compact]/composer-context:opacity-0 motion-reduce:transition-none"
189 >
190 {autoEnvironmentLabel ??
191 (showEnvironmentIndicator ? (activeEnvironment?.label ?? "Run on") : workspaceLabel)}
192 </span>
193 </span>
194 </>
195 );
196
197 if (isLocked) {
198 return (
199 <span
200 className="inline-flex h-7 min-w-0 max-w-[48%] flex-initial items-center justify-start gap-1 rounded-md border border-transparent px-1.75 font-normal text-muted-foreground/70 text-xs sm:h-6"
201 data-composer-context-control
202 >
203 {triggerContent}
204 </span>
205 );
206 }
207
208 return (
209 <Menu>
210 <MenuTrigger
211 render={<ComposerControl size="xs" />}
212 className="min-w-0 max-w-[48%] flex-initial justify-start"
213 data-composer-context-control
214 data-composer-shortcut={[
215 showEnvironmentPicker && !envLocked ? "composer.host" : "",
216 !envModeLocked ? "composer.workspace" : "",
217 ].join(" ")}
218 >
219 {triggerContent}
220 <ChevronDownIcon className="size-3 shrink-0 opacity-50" />
221 </MenuTrigger>
222 <MenuPopup
223 align="start"
224 side="top"
225 className={previousWorktreeLabel ? "w-[min(21rem,calc(100vw-2rem))]" : undefined}
226 {...composerFloatingLayerProps}
227 >
228 {showEnvironmentPicker && availableEnvironments && onEnvironmentChange ? (
229 <>
230 <MenuGroup>
231 <MenuGroupLabel>Run on</MenuGroupLabel>
232 <MenuRadioGroup
233 value={autoEnvironmentLabel ? "auto" : environmentId}
234 onValueChange={(value) =>
235 value === "auto"
236 ? onAutoEnvironment?.()
237 : onEnvironmentChange(value as EnvironmentId)
238 }
239 >
240 {onAutoEnvironment && (
241 <MenuRadioItem
242 value="auto"
243 disabled={envLocked}
244 closeOnClick
245 onClick={() => {
246 if (autoEnvironmentLabel) onAutoEnvironment?.();
247 }}
248 >
249 <span className="flex min-w-0 items-center gap-1.5">
250 <ScaleIcon className="size-3" aria-hidden="true" />
251 <span className="min-w-0 truncate">
252 {autoEnvironmentLabel ?? "Auto balance"}
253 </span>
254 </span>
255 </MenuRadioItem>
256 )}
257 {availableEnvironments.map((env) => (
258 <MenuRadioItem
259 key={env.environmentId}
260 disabled={envLocked}
261 value={env.environmentId}
262 closeOnClick
263 >
264 <span className="flex min-w-0 items-center gap-1.5">
265 <EnvironmentMachineIcon kind={env.machine} className="size-3" />
266 <span className="min-w-0 truncate">{env.label}</span>
267 </span>
268 </MenuRadioItem>
269 ))}
270 </MenuRadioGroup>
271 </MenuGroup>
272 <MenuSeparator />
273 </>
274 ) : null}
275 <MenuGroup>
276 <MenuGroupLabel>Workspace</MenuGroupLabel>
277 <MenuRadioGroup
278 value={effectiveEnvMode}
279 onValueChange={(value) => {
280 if (value === "previous-worktree") {
281 onUsePreviousWorktree();
282 return;
283 }
284 onEnvModeChange(value as EnvMode);
285 }}
286 >
287 <MenuRadioItem disabled={envModeLocked || forceNewWorktree} value="local" closeOnClick>
288 <span className="flex min-w-0 items-center gap-1.5">
289 {activeWorktreePath ? (
290 <FolderGitIcon className="size-3" />
291 ) : (
292 <FolderIcon className="size-3" />
293 )}
294 <MiddleTruncate value={resolveCurrentWorkspaceLabel(activeWorktreePath)} />
295 </span>
296 </MenuRadioItem>
297 <MenuRadioItem disabled={envModeLocked} value="worktree" closeOnClick>
298 <span className="flex min-w-0 items-center gap-1.5">
299 <FolderGit2Icon className="size-3" />
300 <span className="min-w-0 truncate">{resolveEnvModeLabel("worktree")}</span>
301 </span>
302 </MenuRadioItem>
303 {previousWorktreeLabel ? (
304 <MenuRadioItem disabled={envModeLocked} value="previous-worktree" closeOnClick>
305 <PreviousWorktreeItemContent branch={previousWorktreeBranch} />
306 </MenuRadioItem>
307 ) : null}
308 </MenuRadioGroup>
309 </MenuGroup>
310 </MenuPopup>
311 </Menu>
312 );
313});
314
315/**
316 * Collapse the strip's labels to icons only when the text no longer fits.
317 *
318 * Hidden labels stay measurable because their inner text keeps its natural
319 * width while the outer layout box collapses. This lets every pass recompute
320 * the expanded width without remembered values that could go stale or latch
321 * the strip compact. A small hysteresis keeps the boundary from flapping.
322 */
323const COMPOSER_CONTEXT_MOTION_DURATION_MS = 180;
324const COMPOSER_CONTEXT_MOTION_EASING = "cubic-bezier(0.32, 0.72, 0, 1)";
325const COMPOSER_CONTEXT_LABEL_SELECTOR = "[data-composer-label]";
326
327function useLabelsOverflow(element: HTMLDivElement | null): boolean {
328 const [overflows, setOverflows] = useState(false);
329 const pendingLabelRectsRef = useRef<Map<HTMLElement, DOMRect> | null>(null);
330 const labelAnimationsRef = useRef(new Map<HTMLElement, Animation>());
331 // A render-synced mirror instead of useEffectEvent: the compiler memoizes
332 // the event callback, which left observers reading the first render's null
333 // element forever.
334 const stateRef = useRef({ element, overflows });
335 stateRef.current = { element, overflows };
336
337 const measure = useCallback(() => {
338 const { element: current, overflows: compact } = stateRef.current;
339 if (!current) return;
340 const available = current.clientWidth;
341 if (available === 0) return;
342 // flex-1 stretches the groups to fill the strip, so their own boxes always
343 // measure "full". Sum the laid-out content instead, skipping hidden form
344 // artifacts and other out-of-flow nodes.
345 const contentWidth = (parent: Element): number => {
346 const gap = Number.parseFloat(getComputedStyle(parent).columnGap) || 0;
347 let width = 0;
348 let counted = 0;
349 for (const child of parent.children) {
350 if (!(child instanceof HTMLElement)) continue;
351 if (child.offsetWidth === 0) continue;
352 const style = getComputedStyle(child);
353 const position = style.position;
354 if (position === "absolute" || position === "fixed") continue;
355 width +=
356 child.offsetWidth +
357 (Number.parseFloat(style.marginInlineStart) || 0) +
358 (Number.parseFloat(style.marginInlineEnd) || 0);
359 counted += 1;
360 }
361 return width + gap * Math.max(0, counted - 1);
362 };
363 const stripGap = Number.parseFloat(getComputedStyle(current).columnGap) || 0;
364 let needed = 0;
365 let groups = 0;
366 for (const child of current.children) {
367 if (!(child instanceof HTMLElement)) continue;
368 // The host itself flexes into all remaining room. Reserve the natural
369 // width of the controls inside it, blocks in overflow included, so Git
370 // labels compact before squeezing out the model picker. Reserving only
371 // the visible controls would let the labels expand into room the
372 // composer just freed, shrink the host, and hide the controls again.
373 const hostedControls = child.matches('[data-chat-resting-composer-controls-host="true"]')
374 ? child.querySelector<HTMLElement>('[data-chat-composer-resting-controls="true"]')
375 : null;
376 const hostedMeasurement = hostedControls
377 ? measureRestingComposerControls(hostedControls)
378 : null;
379 const width = hostedMeasurement
380 ? resolveRestingComposerControlsNaturalWidth(hostedMeasurement)
381 : contentWidth(hostedControls ?? child);
382 if (width <= 1) continue;
383 groups += 1;
384 needed += width;
385 }
386 needed += stripGap * Math.max(0, groups - 1);
387 for (const label of current.querySelectorAll<HTMLElement>("[data-composer-label]")) {
388 // The clipping can happen below the marker (SelectValue truncates
389 // internally), where the outer span's scrollWidth matches its clipped
390 // box. The text's real width is the largest scrollWidth in the subtree.
391 let textWidth = label.scrollWidth;
392 for (const inner of label.querySelectorAll<HTMLElement>("*")) {
393 textWidth = Math.max(textWidth, inner.scrollWidth);
394 }
395 // Subtract the visible width even during an animation. The content
396 // sum already includes it; only the hidden text needs reserving.
397 needed += Math.max(0, textWidth - label.getBoundingClientRect().width);
398 }
399 const nextOverflows = resolveContextStripLabelsCompact({
400 compact,
401 neededWidth: needed,
402 availableWidth: available,
403 });
404 if (nextOverflows !== compact) {
405 pendingLabelRectsRef.current = new Map(
406 Array.from(current.querySelectorAll<HTMLElement>(COMPOSER_CONTEXT_LABEL_SELECTOR)).map(
407 (label) => [label, label.getBoundingClientRect()],
408 ),
409 );
410 }
411 setOverflows(nextOverflows);
412 }, []);
413
414 useLayoutEffect(() => {
415 const previousRects = pendingLabelRectsRef.current;
416 if (!previousRects) return;
417 pendingLabelRectsRef.current = null;
418
419 for (const animation of labelAnimationsRef.current.values()) {
420 animation.cancel();
421 }
422 labelAnimationsRef.current.clear();
423
424 if (window.matchMedia("(prefers-reduced-motion: reduce)").matches) return;
425
426 for (const [label, previousRect] of previousRects) {
427 if (!label.isConnected) continue;
428 const nextWidth = label.getBoundingClientRect().width;
429 if (Math.abs(previousRect.width - nextWidth) < 0.5) continue;
430
431 // Animate the space occupied by each label so flex layout keeps the
432 // trailing controls anchored. Translating the whole group after its
433 // width snaps sends expanded text beyond the strip's right edge.
434 const animation = label.animate(
435 [
436 { width: `${previousRect.width}px`, maxWidth: `${previousRect.width}px` },
437 { width: `${nextWidth}px`, maxWidth: `${nextWidth}px` },
438 ],
439 {
440 duration: COMPOSER_CONTEXT_MOTION_DURATION_MS,
441 easing: COMPOSER_CONTEXT_MOTION_EASING,
442 fill: "backwards",
443 },
444 );
445 labelAnimationsRef.current.set(label, animation);
446 animation.addEventListener(
447 "finish",
448 () => {
449 if (labelAnimationsRef.current.get(label) === animation) {
450 labelAnimationsRef.current.delete(label);
451 }
452 },
453 { once: true },
454 );
455 }
456 }, [overflows]);
457
458 useEffect(
459 () => () => {
460 for (const animation of labelAnimationsRef.current.values()) {
461 animation.cancel();
462 }
463 },
464 [],
465 );
466
467 // Label widths can change without the strip box moving (font family or
468 // size preferences), so re-measure on every render as well as on resize
469 // and font loads.
470 useLayoutEffect(() => {
471 measure();
472 });
473
474 useEffect(() => {
475 if (!element) return;
476 const observer = new ResizeObserver(measure);
477 observer.observe(element);
478 document.fonts.addEventListener("loadingdone", measure);
479 return () => {
480 observer.disconnect();
481 document.fonts.removeEventListener("loadingdone", measure);
482 };
483 }, [element, measure]);
484
485 return overflows;
486}
487
488export const BranchToolbar = memo(function BranchToolbar({
489 forceNewWorktree = false,
490 ref,
491 environmentId,
492 threadId,
493 showGitControls,
494 draftId,
495 onEnvModeChange,
496 effectiveEnvModeOverride,
497 activeThreadBranchOverride,
498 onActiveThreadBranchOverrideChange,
499 startFromOrigin,
500 onStartFromOriginChange,
501 autoEnvironmentLabel,
502 onAutoEnvironment,
503 envLocked,
504 onCheckoutPullRequestRequest,
505 onComposerFocusRequest,
506 availableEnvironments,
507 onEnvironmentChange,
508 composerControlsHostRef,
509 contextStripVisible = true,
510}: BranchToolbarProps) {
511 const branchSelectorRef = useRef<BranchToolbarBranchSelectorHandle>(null);
512 const threadRef = useMemo(
513 () => scopeThreadRef(environmentId, threadId),
514 [environmentId, threadId],
515 );
516 const draftThread = useComposerDraftStore((store) =>
517 draftId ? store.getDraftSession(draftId) : store.getDraftThreadByRef(threadRef),
518 );
519 const serverThread = useThreadShell(threadRef);
520 const setDraftThreadContext = useComposerDraftStore((store) => store.setDraftThreadContext);
521 const activeProjectRef = serverThread
522 ? scopeProjectRef(serverThread.environmentId, serverThread.projectId)
523 : draftThread
524 ? scopeProjectRef(draftThread.environmentId, draftThread.projectId)
525 : null;
526 const activeProject = useProject(activeProjectRef);
527 const hasActiveThread = serverThread !== null || draftThread !== null;
528 const activeWorktreePath = forceNewWorktree
529 ? null
530 : (serverThread?.worktreePath ?? draftThread?.worktreePath ?? null);
531 const effectiveEnvMode =
532 (forceNewWorktree ? "worktree" : effectiveEnvModeOverride) ??
533 resolveEffectiveEnvMode({
534 activeWorktreePath,
535 hasServerThread: serverThread !== null,
536 draftThreadEnvMode: draftThread?.envMode,
537 });
538 const envModeLocked = envLocked || (serverThread !== null && activeWorktreePath !== null);
539
540 // "Previous worktree" hops a draft into the most recently active worktree
541 // of this project — the "keep going where I just was" follow-up flow. Only
542 // drafts can hop; started server threads have their workspace pinned.
543 const canUsePreviousWorktree =
544 draftThread !== null && serverThread === null && !envModeLocked && !forceNewWorktree;
545 const projectRefsForWorktreeLookup = useMemo(
546 () => (canUsePreviousWorktree && activeProjectRef ? [activeProjectRef] : []),
547 [canUsePreviousWorktree, activeProjectRef],
548 );
549 const projectThreads = useThreadShellsForProjectRefs(projectRefsForWorktreeLookup);
550 const previousWorktreeSeed = useMemo(
551 () =>
552 canUsePreviousWorktree
553 ? resolvePreviousWorktreeSeed({
554 threads: projectThreads,
555 currentWorktreePath: activeWorktreePath,
556 })
557 : null,
558 [activeWorktreePath, canUsePreviousWorktree, projectThreads],
559 );
560 const previousWorktreeLabel = previousWorktreeSeed
561 ? resolvePreviousWorktreeLabel(previousWorktreeSeed)
562 : null;
563 const onUsePreviousWorktree = useCallback(() => {
564 if (!previousWorktreeSeed || !activeProjectRef) return;
565 // Same shape the branch selector writes when picking a branch that
566 // already lives in a worktree: point the draft at the existing tree.
567 setDraftThreadContext(draftId ?? threadRef, {
568 branch: previousWorktreeSeed.branch,
569 worktreePath: previousWorktreeSeed.worktreePath,
570 envMode: "worktree",
571 projectRef: activeProjectRef,
572 });
573 }, [activeProjectRef, draftId, previousWorktreeSeed, setDraftThreadContext, threadRef]);
574
575 useImperativeHandle(
576 ref,
577 () => ({
578 openBranchPicker: () => branchSelectorRef.current?.open(),
579 usePreviousWorktree: () => {
580 if (!showGitControls || !canUsePreviousWorktree || !previousWorktreeSeed) return;
581 onUsePreviousWorktree();
582 onComposerFocusRequest?.();
583 },
584 }),
585 [
586 canUsePreviousWorktree,
587 onComposerFocusRequest,
588 onUsePreviousWorktree,
589 previousWorktreeSeed,
590 showGitControls,
591 ],
592 );
593
594 const showEnvironmentPicker = Boolean(
595 availableEnvironments && availableEnvironments.length > 1 && onEnvironmentChange,
596 );
597 const activeEnvironmentOption =
598 availableEnvironments?.find((env) => env.environmentId === environmentId) ?? null;
599 const showEnvironmentIndicator = shouldShowEnvironmentIndicator({
600 activeEnvironment: activeEnvironmentOption,
601 canPickEnvironment: showEnvironmentPicker,
602 });
603 const [stripElement, setStripElement] = useState<HTMLDivElement | null>(null);
604 const labelsOverflow = useLabelsOverflow(stripElement);
605
606 if (!hasActiveThread || !activeProject) return null;
607
608 return (
609 <ComposerSurface.ContextStrip
610 ref={setStripElement}
611 data-compact={labelsOverflow ? "" : undefined}
612 className={cn(
613 "gap-1 text-xs font-normal text-muted-foreground/70",
614 // A non-Git strip with no visible composer controls should occupy no
615 // space, but its host must retain a prospective width so controls can
616 // become visible again when the chat view grows.
617 !contextStripVisible && "pointer-events-none invisible absolute inset-x-0 top-full",
618 )}
619 >
620 {showGitControls ? (
621 <div className="contents @3xl/composer-surface:hidden">
622 <MobileRunContextSelector
623 forceNewWorktree={forceNewWorktree}
624 autoEnvironmentLabel={autoEnvironmentLabel}
625 onAutoEnvironment={onAutoEnvironment}
626 envLocked={envLocked}
627 envModeLocked={envModeLocked}
628 environmentId={environmentId}
629 availableEnvironments={availableEnvironments}
630 showEnvironmentPicker={showEnvironmentPicker}
631 showEnvironmentIndicator={showEnvironmentIndicator}
632 onEnvironmentChange={onEnvironmentChange}
633 effectiveEnvMode={effectiveEnvMode}
634 activeWorktreePath={activeWorktreePath}
635 onEnvModeChange={onEnvModeChange}
636 previousWorktreeLabel={previousWorktreeLabel}
637 previousWorktreeBranch={previousWorktreeSeed?.branch ?? null}
638 onUsePreviousWorktree={onUsePreviousWorktree}
639 />
640 </div>
641 ) : null}
642 {showGitControls || showEnvironmentIndicator ? (
643 <div
644 className={cn(
645 "min-h-7 min-w-10 items-center gap-1 sm:min-h-6",
646 showGitControls ? "hidden @3xl/composer-surface:flex" : "flex",
647 composerControlsHostRef ? "shrink" : "flex-1",
648 )}
649 >
650 {showEnvironmentIndicator && availableEnvironments && (
651 <>
652 <BranchToolbarEnvironmentSelector
653 autoEnvironmentLabel={autoEnvironmentLabel}
654 onAutoEnvironment={onAutoEnvironment}
655 envLocked={envLocked}
656 environmentId={environmentId}
657 availableEnvironments={availableEnvironments}
658 {...(showEnvironmentPicker && onEnvironmentChange ? { onEnvironmentChange } : {})}
659 />
660 {showGitControls ? (
661 <Separator
662 orientation="vertical"
663 className="mx-0.5 h-3.5!"
664 data-composer-context-control
665 />
666 ) : null}
667 </>
668 )}
669 {showGitControls ? (
670 <BranchToolbarEnvModeSelector
671 forceNewWorktree={forceNewWorktree}
672 envLocked={envModeLocked}
673 effectiveEnvMode={effectiveEnvMode}
674 activeWorktreePath={activeWorktreePath}
675 onEnvModeChange={onEnvModeChange}
676 previousWorktreeLabel={previousWorktreeLabel}
677 previousWorktreeBranch={previousWorktreeSeed?.branch ?? null}
678 onUsePreviousWorktree={onUsePreviousWorktree}
679 />
680 ) : null}
681 </div>
682 ) : null}
683
684 {composerControlsHostRef ? (
685 // The host takes whatever the workspace and branch controls leave
686 // over, in both strip layouts, so a collapsed composer can show its
687 // model and mode controls wherever they fit.
688 <div
689 ref={composerControlsHostRef}
690 data-composer-context-control
691 data-chat-resting-composer-controls-host="true"
692 className="flex min-w-0 flex-1 items-center justify-start overflow-x-clip overflow-y-visible"
693 />
694 ) : null}
695
696 {showGitControls ? (
697 <BranchToolbarBranchSelector
698 forceNewWorktree={forceNewWorktree}
699 ref={branchSelectorRef}
700 className="min-w-0 flex-initial justify-end @3xl/composer-surface:ml-auto"
701 environmentId={environmentId}
702 threadId={threadId}
703 {...(draftId ? { draftId } : {})}
704 envLocked={envLocked}
705 {...(forceNewWorktree
706 ? { effectiveEnvModeOverride: "worktree" }
707 : effectiveEnvModeOverride
708 ? { effectiveEnvModeOverride }
709 : {})}
710 {...(activeThreadBranchOverride !== undefined ? { activeThreadBranchOverride } : {})}
711 {...(onActiveThreadBranchOverrideChange ? { onActiveThreadBranchOverrideChange } : {})}
712 startFromOrigin={startFromOrigin}
713 onStartFromOriginChange={onStartFromOriginChange}
714 {...(onCheckoutPullRequestRequest ? { onCheckoutPullRequestRequest } : {})}
715 {...(onComposerFocusRequest ? { onComposerFocusRequest } : {})}
716 />
717 ) : null}
718 </ComposerSurface.ContextStrip>
719 );
720});