summaryrefslogtreecommitdiffhomepage
path: root/packages/frontend/src/lib/components/TabBar.svelte
blob: 4fbe3b1c5fa69c46f609db1f2b4b101a99554bc7 (plain)
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
<script lang="ts">
import { tick } from "svelte";
import { tabStore } from "../tabs.svelte.js";

function statusColor(status: string): string {
	if (status === "running") return "bg-warning";
	if (status === "error") return "bg-error";
	return "bg-success";
}

const userTabs = $derived(tabStore.tabs.filter((t) => t.parentTabId === null));
const subagentTabs = $derived(
	tabStore.tabs.filter((t) => t.parentTabId !== null && t.parentTabId === activeUserTabId),
);
const hasSubagentTabs = $derived(subagentTabs.length > 0);

// When a subagent tab is active, its parent user tab should still appear selected
const activeTab = $derived(tabStore.tabs.find((t) => t.id === tabStore.activeTabId));
const activeUserTabId = $derived(
	activeTab?.parentTabId !== null && activeTab?.parentTabId !== undefined
		? activeTab.parentTabId
		: tabStore.activeTabId,
);

// ── Drag-and-drop reorder (user tabs only) ──
// Mirrors the native HTML5 DnD pattern used in AgentBuilder.svelte.
let dragIndex = $state<number | null>(null);
let dragOverIndex = $state<number | null>(null);

function dropReorder(targetIndex: number): void {
	if (dragIndex !== null && dragIndex !== targetIndex) {
		const ids = userTabs.map((t) => t.id);
		const moved = ids.splice(dragIndex, 1)[0];
		if (moved) {
			ids.splice(targetIndex, 0, moved);
			tabStore.reorderTabs(ids);
		}
	}
	dragIndex = null;
	dragOverIndex = null;
}

// ── Double-click rename (user tabs only) ──
let editingTabId = $state<string | null>(null);
let editValue = $state("");
let editInputEl = $state<HTMLInputElement | undefined>(undefined);

async function startRename(tab: { id: string; title: string }): Promise<void> {
	editingTabId = tab.id;
	editValue = tab.title;
	await tick();
	editInputEl?.focus();
	editInputEl?.select();
}

function commitRename(): void {
	if (editingTabId === null) return;
	const id = editingTabId;
	editingTabId = null;
	const next = editValue.trim();
	if (next) tabStore.renameTab(id, next);
}

function cancelRename(): void {
	editingTabId = null;
}

function handleRenameKeydown(e: KeyboardEvent): void {
	if (e.key === "Enter") {
		e.preventDefault();
		commitRename();
	} else if (e.key === "Escape") {
		e.preventDefault();
		cancelRename();
	}
}
</script>

<!-- Top row: user tabs -->
<!-- svelte-ignore a11y_no_static_element_interactions -->
<div
	class="overflow-x-auto bg-base-200 flex-shrink-0 {hasSubagentTabs ? '' : 'rounded-br-lg'}"
	ondblclick={(e) => { if (e.target === e.currentTarget) tabStore.createNewTab(); }}
>
	<!-- svelte-ignore a11y_no_static_element_interactions -->
	<div
		role="tablist"
		tabindex="0"
		class="tabs tabs-lift min-w-max"
		ondblclick={(e) => { if (e.target === e.currentTarget) tabStore.createNewTab(); }}
	>
		<!-- New tab button — always first -->
		<button
			type="button"
			class="tab"
			onclick={() => tabStore.createNewTab()}
			aria-label="New tab"
		>
			+
		</button>

		{#each userTabs as tab, i (tab.id)}
			<!-- svelte-ignore a11y_no_static_element_interactions -->
			<div
				role="tab"
				class="tab !flex items-stretch gap-1.5 {tab.id === activeUserTabId ? 'tab-active' : ''} {dragOverIndex === i ? 'bg-primary/10' : ''} {dragIndex === i ? 'opacity-50' : ''}"
				draggable={editingTabId === tab.id ? "false" : "true"}
				onclick={() => tabStore.switchTab(tab.id)}
				onkeydown={(e) => { if (e.key === 'Enter' || e.key === ' ') tabStore.switchTab(tab.id); }}
				ondragstart={(e) => {
					dragIndex = i;
					if (e.dataTransfer) e.dataTransfer.effectAllowed = "move";
				}}
				ondragover={(e) => {
					e.preventDefault();
					if (e.dataTransfer) e.dataTransfer.dropEffect = "move";
					dragOverIndex = i;
				}}
				ondragleave={() => { if (dragOverIndex === i) dragOverIndex = null; }}
				ondrop={(e) => { e.preventDefault(); dropReorder(i); }}
				ondragend={() => { dragIndex = null; dragOverIndex = null; }}
				tabindex="0"
			>
				<span class="flex items-center gap-1.5">
					<span class="w-1.5 h-1.5 rounded-full shrink-0 {statusColor(tab.agentStatus)}"></span>
					<span class="font-mono text-[10px] px-1 py-0.5 rounded bg-base-300 text-base-content/60 shrink-0" title="Tab ID — agents address this tab by this handle">{tabStore.shortHandleFor(tab.id)}</span>
					{#if editingTabId === tab.id}
						<input
							bind:this={editInputEl}
							bind:value={editValue}
							class="max-w-32 text-xs bg-base-100 rounded px-1 outline-none ring-1 ring-primary/40"
							onclick={(e) => e.stopPropagation()}
							ondblclick={(e) => e.stopPropagation()}
							onkeydown={handleRenameKeydown}
							onblur={commitRename}
						/>
					{:else}
						<span
							class="max-w-32 truncate text-xs"
							ondblclick={(e) => { e.stopPropagation(); startRename(tab); }}
							title="Double-click to rename"
						>{tab.title}</span>
					{/if}
				</span>
				<button
					type="button"
					class="flex items-center justify-center px-3 my-1 leading-none text-base-content/30 hover:text-error hover:bg-base-300 rounded transition-colors text-xs"
					onclick={(e) => { e.stopPropagation(); tabStore.closeTab(tab.id); }}
					aria-label="Close tab"
				>
					&#x2715;
				</button>
			</div>
		{/each}
	</div>
</div>

<!-- Bottom row: subagent tabs (hidden when empty) -->
{#if hasSubagentTabs}
	<div class="overflow-x-auto bg-base-200 flex-shrink-0 border-t border-base-300 rounded-br-lg">
		<div
			role="tablist"
			class="tabs tabs-lift tabs-xs min-w-max"
		>
			{#each subagentTabs as tab (tab.id)}
				<!-- svelte-ignore a11y_no_static_element_interactions -->
				<div
					role="tab"
					class="tab !flex items-stretch gap-1 {tab.id === tabStore.activeTabId ? 'tab-active' : ''} {!tab.persistent ? 'opacity-70 italic' : ''}"
					onclick={() => tab.persistent ? tabStore.switchTab(tab.id) : tabStore.promoteTab(tab.id)}
					onkeydown={(e) => { if (e.key === 'Enter' || e.key === ' ') tab.persistent ? tabStore.switchTab(tab.id) : tabStore.promoteTab(tab.id); }}
					tabindex="0"
				>
					<span class="flex items-center gap-1">
						<span class="w-1 h-1 rounded-full shrink-0 {statusColor(tab.agentStatus)}"></span>
						<span class="font-mono text-[10px] px-1 rounded bg-base-300 text-base-content/60 shrink-0" title="Tab ID — agents address this tab by this handle">{tabStore.shortHandleFor(tab.id)}</span>
						<span class="max-w-28 truncate text-xs">{tab.title}</span>
					</span>
					{#if tab.persistent}
						<button
							type="button"
							class="flex items-center justify-center px-2 my-0.5 leading-none text-base-content/30 hover:text-error hover:bg-base-300 rounded transition-colors text-xs"
							onclick={(e) => { e.stopPropagation(); tabStore.closeTab(tab.id); }}
							aria-label="Close tab"
						>
							&#x2715;
						</button>
					{/if}
				</div>
			{/each}
		</div>
	</div>
{/if}