Skip to content
Source
Sign inSign up
pingdotgg/t3code
Open History to see the last commit

BranchToolbar.tsx

677 lines26.2 KB

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