diff --git a/packages/hoppscotch-common/src/helpers/rest/__tests__/tabRenderCache.spec.ts b/packages/hoppscotch-common/src/helpers/rest/__tests__/tabRenderCache.spec.ts new file mode 100644 index 00000000..efdf8744 --- /dev/null +++ b/packages/hoppscotch-common/src/helpers/rest/__tests__/tabRenderCache.spec.ts @@ -0,0 +1,51 @@ +import { describe, expect, test } from "vitest" +import { updateRenderedRESTTabIDs } from "../tabRenderCache" + +describe("REST tab render cache", () => { + test("keeps the active tab and the 4 most recent rendered tabs", () => { + let renderedTabIDs: string[] = [] + + for (const activeTabID of ["tab-1", "tab-2", "tab-3", "tab-4", "tab-5"]) { + renderedTabIDs = updateRenderedRESTTabIDs({ + renderedTabIDs, + activeTabID, + activeTabIDs: ["tab-1", "tab-2", "tab-3", "tab-4", "tab-5"], + maxRenderedTabs: 5, + }) + } + + expect(renderedTabIDs).toEqual([ + "tab-5", + "tab-4", + "tab-3", + "tab-2", + "tab-1", + ]) + + renderedTabIDs = updateRenderedRESTTabIDs({ + renderedTabIDs, + activeTabID: "tab-6", + activeTabIDs: ["tab-1", "tab-2", "tab-3", "tab-4", "tab-5", "tab-6"], + maxRenderedTabs: 5, + }) + + expect(renderedTabIDs).toEqual([ + "tab-6", + "tab-5", + "tab-4", + "tab-3", + "tab-2", + ]) + }) + + test("removes closed tabs from the rendered cache", () => { + const renderedTabIDs = updateRenderedRESTTabIDs({ + renderedTabIDs: ["tab-5", "tab-4", "tab-3", "tab-2", "tab-1"], + activeTabID: "tab-5", + activeTabIDs: ["tab-1", "tab-3", "tab-5"], + maxRenderedTabs: 5, + }) + + expect(renderedTabIDs).toEqual(["tab-5", "tab-3", "tab-1"]) + }) +}) diff --git a/packages/hoppscotch-common/src/helpers/rest/tabRenderCache.ts b/packages/hoppscotch-common/src/helpers/rest/tabRenderCache.ts new file mode 100644 index 00000000..c973229d --- /dev/null +++ b/packages/hoppscotch-common/src/helpers/rest/tabRenderCache.ts @@ -0,0 +1,22 @@ +type UpdateRenderedRESTTabIDsOptions = { + renderedTabIDs: string[] + activeTabID: string + activeTabIDs: string[] + maxRenderedTabs: number +} + +export function updateRenderedRESTTabIDs({ + renderedTabIDs, + activeTabID, + activeTabIDs, + maxRenderedTabs, +}: UpdateRenderedRESTTabIDsOptions) { + const activeTabIDSet = new Set(activeTabIDs) + + return [ + activeTabID, + ...renderedTabIDs.filter((tabID) => tabID !== activeTabID), + ] + .filter((tabID) => activeTabIDSet.has(tabID)) + .slice(0, maxRenderedTabs) +} diff --git a/packages/hoppscotch-common/src/pages/index.vue b/packages/hoppscotch-common/src/pages/index.vue index bfcb216a..e6268870 100644 --- a/packages/hoppscotch-common/src/pages/index.vue +++ b/packages/hoppscotch-common/src/pages/index.vue @@ -6,6 +6,7 @@ v-if="currentTabID" :id="'rest_windows'" v-model="currentTabID" + render-inactive-tabs @remove-tab="removeTab" @add-tab="addNewTab" @sort="sortTabs" @@ -44,24 +45,26 @@ - - - - - - +