Database governance built for humans and agents — controlling changes and access across every major database.
Go cicddata-governancedata-maskingdata-securitydatabase-accessdatabase-governancedevopsdevsecopsflywaygitopsliquibasemongodbmysqloraclepostgresqlschema-migrationssecuritysql-clientsqlservertidb
import {
closestCenter,
DndContext,
type DragEndEvent,
PointerSensor,
useSensor,
useSensors,
} from "@dnd-kit/core";
import {
horizontalListSortingStrategy,
SortableContext,
} from "@dnd-kit/sortable";
import { Plus } from "lucide-react";
import {
useCallback,
useEffect,
useLayoutEffect,
useRef,
useState,
} from "react";
import { useTranslation } from "react-i18next";
import scrollIntoView from "scroll-into-view-if-needed";
import {
AlertDialog,
AlertDialogContent,
AlertDialogDescription,
AlertDialogFooter,
AlertDialogTitle,
} from "@/components/ui/alert-dialog";
import { Button } from "@/components/ui/button";
import { cn } from "@/lib/utils";
import { tabListEvents } from "@/modules/sql-editor/model/TabList/events";
import {
getSQLEditorTabsState,
useOpenTabList,
useSQLEditorTabState,
} from "@/modules/sql-editor/store/tab";
import type { SQLEditorTab } from "@/types/sqlEditor/tab";
import { TabContextMenu, type TabContextMenuHandle } from "./TabContextMenu";
import { TabItem } from "./TabItem/TabItem";
type CloseTabAction =
| "CLOSE"
| "CLOSE_OTHERS"
| "CLOSE_TO_THE_RIGHT"
| "CLOSE_SAVED"
| "CLOSE_ALL";
type PendingClose = {
tab: SQLEditorTab;
resolve: (confirmed: boolean) => void;
};
/**
* Horizontal tab bar at the top of the SQL editor. Drag-reorder via
* @dnd-kit, overflow-x scroll, "+" button to add a new saved query, and
* right-click context menu delegated to TabContextMenu.
*/
export function TabList() {
const { t } = useTranslation();
// Zustand's selector subscribes to in-place tab mutations because
// `updateTab` reassigns / triggers an immer produce on `tabsById`,
// bumping the slice identity.
const tabs = useOpenTabList();
const currentTabId = useSQLEditorTabState((s) => s.currentTabId);
const scrollRef = useRef<HTMLDivElement>(null);
const contextMenuRef = useRef<TabContextMenuHandle>(null);
const [loading, setLoading] = useState(false);
const [scrollState, setScrollState] = useState({
moreLeft: false,
moreRight: false,
});
const [pendingClose, setPendingClose] = useState<PendingClose | null>(null);
const recalculateScrollState = useCallback(() => {
contextMenuRef.current?.hide();
const el = scrollRef.current;
if (!el) return;
const { scrollWidth, offsetWidth, scrollLeft } = el;
setScrollState({
moreLeft: scrollLeft > 0,
moreRight:
scrollWidth > offsetWidth
? scrollLeft + offsetWidth < scrollWidth
: false,
});
}, []);
// Initial measure + resize observer.
useLayoutEffect(() => {
recalculateScrollState();
const el = scrollRef.current;
if (!el) return;
const ro = new ResizeObserver(() => recalculateScrollState());
ro.observe(el);
return () => ro.disconnect();
}, [recalculateScrollState]);
// Prevent the trackpad / horizontal wheel from triggering browser
// back/forward navigation when the tab list can't scroll further.
useEffect(() => {
const el = scrollRef.current;
if (!el) return;
const handler = (e: WheelEvent) => {
const maxX = el.scrollWidth - el.clientWidth;
const target = el.scrollLeft + e.deltaX;
if (target < 0 || target > maxX) {
e.preventDefault();
}
};
el.addEventListener("wheel", handler, { passive: false });
return () => el.removeEventListener("wheel", handler);
}, []);
// Scroll the selected tab into view whenever the active tab changes.
useEffect(() => {
if (!currentTabId) return;
requestAnimationFrame(() => {
const el = document.querySelector(
`[data-tab-id="${currentTabId}"]`
) as HTMLElement | null;
if (el) {
scrollIntoView(el, { scrollMode: "if-needed" });
}
});
}, [currentTabId]);
const confirmCloseUnsaved = useCallback(
(tab: SQLEditorTab) =>
new Promise<boolean>((resolve) => {
setPendingClose({ tab, resolve });
}),
[]
);
const removeTab = useCallback(
async (tab: SQLEditorTab, focusWhenConfirm = false) => {
const tabsState = getSQLEditorTabsState();
if (tab.mode === "SAVED_QUERY" && tab.status !== "CLEAN") {
if (focusWhenConfirm) {
tabsState.setCurrentTabId(tab.id);
}
const confirmed = await confirmCloseUnsaved(tab);
if (!confirmed) return false;
}
tabsState.closeTab(tab.id);
requestAnimationFrame(recalculateScrollState);
return true;
},
[confirmCloseUnsaved, recalculateScrollState]
);
const handleAddTab = () => {
if (loading) return;
setLoading(true);
try {
// A blank tab stays local until auto-save sees SQL content.
getSQLEditorTabsState().addTab();
requestAnimationFrame(() => {
const el = scrollRef.current;
if (el) el.scrollTo(el.scrollWidth, 0);
requestAnimationFrame(recalculateScrollState);
});
} finally {
setLoading(false);
}
};
const handleSelect = (tab: SQLEditorTab) => {
getSQLEditorTabsState().setCurrentTabId(tab.id);
};
const handleClose = (tab: SQLEditorTab) => {
void removeTab(tab);
};
const handleContextMenu = (
tab: SQLEditorTab,
index: number,
e: React.MouseEvent
) => {
contextMenuRef.current?.show(tab, index, e);
};
// Drag-reorder.
const sensors = useSensors(
useSensor(PointerSensor, { activationConstraint: { distance: 4 } })
);
const handleDragEnd = (event: DragEndEvent) => {
const { active, over } = event;
if (!over || active.id === over.id) return;
const oldIndex = tabs.findIndex((t) => t.id === active.id);
const newIndex = tabs.findIndex((t) => t.id === over.id);
if (oldIndex < 0 || newIndex < 0) return;
const next = [...tabs];
const [moved] = next.splice(oldIndex, 1);
next.splice(newIndex, 0, moved);
// Rewrite the persisted tab order without touching individual tabs.
getSQLEditorTabsState().setOpenTabListOrder(next.map((tab) => tab.id));
};
// Listen for close-tab events from the context menu (batch actions).
useEffect(() => {
const unsubscribe = tabListEvents.on(
"close-tab",
async ({ data: { tab, index, action } }) => {
const tabsState = getSQLEditorTabsState();
const snapshot = tabsState.openTmpTabList
.map((persisted) => tabsState.tabsById.get(persisted.id))
.filter((t): t is SQLEditorTab => !!t);
const max = snapshot.length - 1;
const remove = async (t: SQLEditorTab) => {
await removeTab(t, true);
await new Promise((r) => requestAnimationFrame(r));
};
switch (action as CloseTabAction) {
case "CLOSE":
await remove(tab);
return;
case "CLOSE_OTHERS": {
for (let i = max; i > index; i--) await remove(snapshot[i]);
for (let i = index - 1; i >= 0; i--) await remove(snapshot[i]);
return;
}
case "CLOSE_TO_THE_RIGHT": {
for (let i = max; i > index; i--) await remove(snapshot[i]);
return;
}
case "CLOSE_SAVED": {
for (let i = max; i >= 0; i--) {
const t = snapshot[i];
if (t.status === "CLEAN") await remove(t);
}
return;
}
case "CLOSE_ALL": {
for (let i = max; i >= 0; i--) await remove(snapshot[i]);
return;
}
}
}
);
return () => {
unsubscribe();
};
}, [removeTab]);
return (
<div
className={cn(
"bb-sql-editor-tab-list flex justify-between items-center",
// pl-2 keeps the first tab off the vertical resize bar to its left.
"box-border text-control-light text-sm border-b pl-2 pr-2 gap-1"
)}
>
<div
ref={scrollRef}
onScroll={recalculateScrollState}
className={cn(
"flex-1 overflow-x-auto overflow-y-hidden scrollbar-thin",
scrollState.moreLeft && "more-left",
scrollState.moreRight && "more-right"
)}
>
<DndContext
sensors={sensors}
collisionDetection={closestCenter}
onDragEnd={handleDragEnd}
>
<SortableContext
items={tabs.map((tab) => tab.id)}
strategy={horizontalListSortingStrategy}
>
<div className="relative flex flex-nowrap pt-0.5">
{tabs.map((tab, index) => (
<TabItem
key={tab.id}
tab={tab}
index={index}
onSelect={handleSelect}
onClose={handleClose}
onContextMenu={handleContextMenu}
/>
))}
<div className="shrink-0 sticky right-0 bg-background flex items-stretch justify-end">
<Button
appearance="secondary"
size="md"
type="button"
className={cn(
"bg-control-bg/20 hover:bg-accent/10",
"border-t border-x rounded-t-sm hover:border-accent disabled:opacity-50"
)}
disabled={loading}
onClick={handleAddTab}
aria-label={t("common.add")}
>
<Plus className="size-4" strokeWidth={2.5} />
</Button>
</div>
</div>
</SortableContext>
</DndContext>
</div>
<TabContextMenu ref={contextMenuRef} />
<AlertDialog
open={pendingClose !== null}
// The user MUST click Cancel or "Close tab". Cancel Base UI's close
// when the reason is Esc / outside-click so the dialog stays open
// and forces an explicit choice.
onOpenChange={(
open: boolean,
eventDetails?: { reason?: string; cancel?: () => void }
) => {
if (
!open &&
(eventDetails?.reason === "escape-key" ||
eventDetails?.reason === "outside-press")
) {
eventDetails.cancel?.();
return;
}
if (!open && pendingClose) {
pendingClose.resolve(false);
setPendingClose(null);
}
}}
>
<AlertDialogContent className="max-w-md p-6">
<AlertDialogTitle>
{t("sql-editor.hint-tips.confirm-to-close-unsaved-sheet.title")}
</AlertDialogTitle>
<AlertDialogDescription>
{t("sql-editor.hint-tips.confirm-to-close-unsaved-sheet.content")}
</AlertDialogDescription>
<AlertDialogFooter className="mt-4 flex justify-end gap-x-2">
<Button
appearance="outline"
onClick={() => {
pendingClose?.resolve(false);
setPendingClose(null);
}}
>
{t("common.cancel")}
</Button>
<Button
variant="default"
onClick={() => {
pendingClose?.resolve(true);
setPendingClose(null);
}}
>
{t("sql-editor.close-sheet")}
</Button>
</AlertDialogFooter>
</AlertDialogContent>
</AlertDialog>
</div>
);
}