Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -114,3 +114,23 @@ describe("slimTabsForStorage", () => {
expect(s.body.formData[0].fileContentBase64).toBe("")
})
})

describe("moveTab", () => {
const { moveTab } = require("../context/tabs-context")
const ids = (tabs: { id: string }[]) => tabs.map((t) => t.id).join("")
const abc = [{ id: "a", v: 1 }, { id: "b", v: 2 }, { id: "c", v: 3 }]

it("moves forward and backward into the target's slot", () => {
expect(ids(moveTab(abc, "a", "c"))).toBe("bca")
expect(ids(moveTab(abc, "c", "a"))).toBe("cab")
})

it("keeps the live tab objects (no stale snapshot)", () => {
expect(moveTab(abc, "a", "b")[1]).toBe(abc[0])
})

it("returns the same array for unknown or identical ids", () => {
expect(moveTab(abc, "x", "a")).toBe(abc)
expect(moveTab(abc, "a", "a")).toBe(abc)
})
})
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,11 @@ interface CollectionsSidebarProps {
onLoadRequest: (request: CollectionRequest) => void
}

export function CollectionsSidebar({ onLoadRequest: loadRequest }: CollectionsSidebarProps) {
// Memoized: the API client root re-renders on every keystroke, and this
// sidebar's only prop (`onLoadRequest`) is stable across them.
export const CollectionsSidebar = React.memo(CollectionsSidebarImpl)

function CollectionsSidebarImpl({ onLoadRequest: loadRequest }: CollectionsSidebarProps) {
const panel = useToolSidebarPanel()
// While the panel floats (mobile sheet, or the desktop flyout from the
// collapsed rail), loading a request has to dismiss it or the loaded
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,17 @@ function persistTabs(tabs: ApiRequestState[]) {
}
}

/** Move the tab `fromId` into `toId`'s slot; returns `tabs` unchanged when either id is missing. */
export function moveTab<T extends { id: string }>(tabs: T[], fromId: string, toId: string): T[] {
const from = tabs.findIndex((t) => t.id === fromId)
const to = tabs.findIndex((t) => t.id === toId)
if (from === -1 || to === -1 || from === to) return tabs
const next = [...tabs]
const [moved] = next.splice(from, 1)
next.splice(to, 0, moved)
return next
}

export const createNewTab = (kind: "rest" | "websocket" | "grpc" = "rest"): ApiRequestState => ({
id: crypto.randomUUID(),
name: kind === "websocket" ? "New WebSocket" : kind === "grpc" ? "New gRPC" : API_CLIENT_DEFAULT_TAB_NAME,
Expand Down Expand Up @@ -77,7 +88,8 @@ type TabsActions = {
closeTab(id: string): void
duplicateTab(id: string): void
renameTab(id: string, name: string): void
reorderTabs(next: ApiRequestState[]): void
/** Move `fromId` to `toId`'s slot. Id-based so a memoized caller holding a stale tabs array can't overwrite edits. */
reorderTabs(fromId: string, toId: string): void
setActiveTabId(id: string): void
updateActiveTab(updates: Partial<ApiRequestState> | ((tab: ApiRequestState) => Partial<ApiRequestState>)): void
}
Expand Down Expand Up @@ -225,8 +237,8 @@ export function TabsProvider({ children }: { children: React.ReactNode }) {
setTabs((prev) => prev.map((tab) => tab.id === id ? { ...tab, name } : tab))
},

reorderTabs(next: ApiRequestState[]) {
setTabs(next)
reorderTabs(fromId: string, toId: string) {
setTabs((prev) => moveTab(prev, fromId, toId))
},

setActiveTabId(id: string) {
Expand Down
31 changes: 21 additions & 10 deletions apps/desktop-ui/src/components/api-client/tab-bar.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ interface TabBarProps {
onTabClose: (id: string) => void
onTabAdd: (kind?: "rest" | "websocket" | "grpc") => void
onTabRename?: (id: string, name: string) => void
onTabReorder?: (reordered: ApiRequestState[]) => void
onTabReorder?: (fromId: string, toId: string) => void
onTabDuplicate?: (id: string) => void
}

Expand All @@ -38,7 +38,25 @@ const getMethodColor = (method: string) => {
}
}

export function TabBar({
/** True when two tab lists render the same chips (the only tab fields the bar reads). */
function sameChips(a: ApiRequestState[], b: ApiRequestState[]): boolean {
return a.length === b.length && a.every((t, i) =>
t.id === b[i].id && t.kind === b[i].kind && t.method === b[i].method && t.name === b[i].name)
}

/**
* `tabs` is a new array on every keystroke in any request field; skip the
* re-render unless a chip actually changed. Safe because the bar never hands
* tab objects back up (reorder is id-based).
*/
export const TabBar = React.memo(TabBarImpl, (prev, next) => {
for (const key of Object.keys(next) as (keyof TabBarProps)[]) {
if (key !== "tabs" && prev[key] !== next[key]) return false
}
return sameChips(prev.tabs, next.tabs)
})

function TabBarImpl({
tabs,
activeTabId,
onTabChange,
Expand Down Expand Up @@ -112,14 +130,7 @@ export function TabBar({
e.preventDefault()
if (!draggedId || draggedId === targetId || !onTabReorder) return

const from = tabs.findIndex((t) => t.id === draggedId)
const to = tabs.findIndex((t) => t.id === targetId)
if (from === -1 || to === -1) return

const reordered = [...tabs]
const [moved] = reordered.splice(from, 1)
reordered.splice(to, 0, moved)
onTabReorder(reordered)
onTabReorder(draggedId, targetId)
setDraggedId(null)
setDragOverId(null)
}
Expand Down
Loading