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