-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBaseboard.tsx
More file actions
196 lines (170 loc) · 6.93 KB
/
Baseboard.tsx
File metadata and controls
196 lines (170 loc) · 6.93 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
import { useRef, useState, useMemo, useLayoutEffect, useContext, useSyncExternalStore } from 'react';
import { CaretLeftIcon, CaretRightIcon } from '@phosphor-icons/react';
import { Door } from './Door';
import { DoorElementsContext, type DetachedItem } from './Pond';
import { DEFAULT_SESSION_UI_STATE, getSessionStateSnapshot, subscribeToSessionStateChanges } from '../lib/terminal-registry';
export interface BaseboardProps {
items: DetachedItem[];
activeId: string | null;
onReattach: (item: DetachedItem) => void;
}
export function Baseboard({ items, activeId, onReattach }: BaseboardProps) {
const { elements: doorElements, bumpVersion } = useContext(DoorElementsContext);
const sessionStates = useSyncExternalStore(subscribeToSessionStateChanges, getSessionStateSnapshot);
const containerRef = useRef<HTMLDivElement>(null);
const [containerWidth, setContainerWidth] = useState(0);
const [startIndex, setStartIndex] = useState(0);
const doorWidthsRef = useRef<number[]>([]);
const arrowMeasureEl = useRef<HTMLButtonElement>(null);
const layoutMetrics = useRef({ doorGap: 0, arrowWidth: 0 });
useLayoutEffect(() => {
const el = containerRef.current;
if (!el) return;
const ro = new ResizeObserver(([entry]) => {
setContainerWidth(entry.contentRect.width);
});
ro.observe(el);
return () => ro.disconnect();
}, []);
// Measure door widths from hidden elements — re-measures when items change
const measureEl = useRef<HTMLDivElement>(null);
useLayoutEffect(() => {
const el = measureEl.current;
if (!el) return;
const widths: number[] = [];
for (let i = 0; i < el.children.length; i++) {
widths.push((el.children[i] as HTMLElement).offsetWidth);
}
doorWidthsRef.current = widths;
// Measure layout metrics from DOM to stay in sync with CSS classes
const container = containerRef.current;
if (container) {
layoutMetrics.current.doorGap = parseFloat(getComputedStyle(container).gap) || 0;
}
if (arrowMeasureEl.current) {
layoutMetrics.current.arrowWidth = arrowMeasureEl.current.offsetWidth;
}
}, [items, sessionStates]);
// Reset startIndex when the set of door items changes (not just count)
const itemKey = useMemo(() => items.map(i => i.id).join('\0'), [items]);
useLayoutEffect(() => {
setStartIndex(0);
}, [itemKey]);
// Keyboard shortcut hint — only show when there's enough space and no doors
const shortcutHint = 'LCmd → RCmd to enter command mode';
const showHint = items.length === 0 && containerWidth > 350;
// Calculate which doors fit
// contentRect.width already excludes container padding
const availableWidth = containerWidth;
let visibleCount = 0;
let usedWidth = 0;
if (items.length > 0) {
const widths = doorWidthsRef.current;
const { doorGap, arrowWidth } = layoutMetrics.current;
const hasLeftOverflow = startIndex > 0;
let budget = availableWidth - (hasLeftOverflow ? arrowWidth : 0);
for (let i = startIndex; i < items.length; i++) {
const doorW = (widths[i] || 100) + (visibleCount > 0 ? doorGap : 0);
// Reserve space for right arrow if there are more items after this one
const needsRightArrow = i + 1 < items.length;
const rightReserve = needsRightArrow ? arrowWidth : 0;
if (usedWidth + doorW + rightReserve > budget) break;
usedWidth += doorW;
visibleCount++;
}
// Ensure at least one door is visible
if (visibleCount === 0 && items.length > 0) visibleCount = 1;
}
const endIndex = startIndex + visibleCount;
const hiddenLeft = startIndex;
const hiddenRight = items.length - endIndex;
useLayoutEffect(() => {
const container = containerRef.current;
if (!container) return;
const visibleDoors = new Map<string, HTMLElement>();
for (const item of items.slice(startIndex, endIndex)) {
const el = container.querySelector<HTMLElement>(`[data-door-id="${item.id}"]`);
if (el) visibleDoors.set(item.id, el);
}
let changed = false;
if (doorElements.size !== visibleDoors.size) {
changed = true;
} else {
for (const [id, el] of visibleDoors) {
if (doorElements.get(id) !== el) {
changed = true;
break;
}
}
}
if (!changed) return;
doorElements.clear();
for (const [id, el] of visibleDoors) {
doorElements.set(id, el);
}
bumpVersion();
}, [items, startIndex, endIndex, doorElements, bumpVersion]);
const scrollLeft = () => setStartIndex(Math.max(0, startIndex - 1));
const scrollRight = () => setStartIndex(Math.min(items.length - 1, startIndex + 1));
return (
<div
ref={containerRef}
className="flex h-8 shrink-0 items-end gap-1.5 border-t border-border bg-surface-alt px-2.5 pt-1"
>
{/* Hidden measurement pass — doors + overflow arrow */}
<div ref={measureEl} className="absolute -left-[9999px] flex gap-1.5" aria-hidden>
{items.map(item => {
const sessionState = sessionStates.get(item.id) ?? DEFAULT_SESSION_UI_STATE;
return (
<Door
key={item.id}
title={item.title}
status={sessionState.status}
todo={sessionState.todo}
/>
);
})}
</div>
<button ref={arrowMeasureEl} className="absolute -left-[9999px] flex h-5 shrink-0 items-center gap-1 rounded px-1.5 pb-px text-[9px] font-medium font-mono tracking-[0.06em] text-muted" aria-hidden tabIndex={-1}>
9 more <CaretRightIcon size={10} weight="bold" />
</button>
{items.length === 0 && showHint && (
<span className="truncate pb-1 text-[9px] font-mono tracking-[0.06em] text-muted">
{shortcutHint}
</span>
)}
{hiddenLeft > 0 && (
<button
className="flex h-5 shrink-0 items-center gap-1 rounded px-1.5 pb-px text-[9px] font-medium font-mono tracking-[0.06em] text-muted transition-colors hover:bg-surface-raised hover:text-foreground"
onClick={scrollLeft}
>
<CaretLeftIcon size={10} weight="bold" />
{hiddenLeft} more
</button>
)}
{items.slice(startIndex, endIndex).map(item => {
const sessionState = sessionStates.get(item.id) ?? DEFAULT_SESSION_UI_STATE;
return (
<Door
key={item.id}
doorId={item.id}
title={item.title}
isActive={activeId === item.id}
status={sessionState.status}
todo={sessionState.todo}
onClick={() => onReattach(item)}
/>
);
})}
{hiddenRight > 0 && (
<button
className="ml-auto flex h-5 shrink-0 items-center gap-1 rounded px-1.5 pb-px text-[9px] font-medium font-mono tracking-[0.06em] text-muted transition-colors hover:bg-surface-raised hover:text-foreground"
onClick={scrollRight}
>
{hiddenRight} more
<CaretRightIcon size={10} weight="bold" />
</button>
)}
</div>
);
}