Skip to content

Commit

Permalink
fix: version index is off by 1
Browse files Browse the repository at this point in the history
  • Loading branch information
abvthecity committed Mar 26, 2024
1 parent 936168f commit 4bad116
Show file tree
Hide file tree
Showing 7 changed files with 97 additions and 27 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -230,3 +230,47 @@ exports[`getNavigationRoot > primer > gets navigation root for /docs/api/introdu
},
]
`;

exports[`getNavigationRoot > primer > gets navigation root for /docs/api/v2.1/api-reference/client-session-api/retrieve-client-side-token 1`] = `
[
{
"availability": "Stable",
"id": "v2.3",
"index": 0,
"slug": [
"docs",
"api",
],
},
{
"availability": null,
"id": "v2.2",
"index": 1,
"slug": [
"docs",
"api",
"v2.2",
],
},
{
"availability": null,
"id": "v2.1",
"index": 2,
"slug": [
"docs",
"api",
"v2.1",
],
},
{
"availability": null,
"id": "v2",
"index": 3,
"slug": [
"docs",
"api",
"v2",
],
},
]
`;
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,7 @@ exports[`getNavigationRoot > yes-version-no-tabs > gets navigation root for /doc
"urlSlug": "page-5",
},
"currentTabIndex": -1,
"currentVersionIndex": 1,
"currentVersionIndex": 0,
},
"type": "found",
}
Expand Down Expand Up @@ -172,7 +172,7 @@ exports[`getNavigationRoot > yes-version-no-tabs > gets navigation root for /doc
"urlSlug": "page-6",
},
"currentTabIndex": -1,
"currentVersionIndex": 1,
"currentVersionIndex": 0,
},
"type": "found",
}
Expand Down Expand Up @@ -207,7 +207,7 @@ exports[`getNavigationRoot > yes-version-no-tabs > gets navigation root for /doc
"urlSlug": "page-9",
},
"currentTabIndex": -1,
"currentVersionIndex": 2,
"currentVersionIndex": 1,
},
"type": "found",
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,7 @@ exports[`getNavigationRoot > yes-version-yes-tabs > gets navigation root for /do
"urlSlug": "page-3",
},
"currentTabIndex": 0,
"currentVersionIndex": 1,
"currentVersionIndex": 0,
},
"type": "found",
}
Expand Down
19 changes: 19 additions & 0 deletions packages/commons/fdr-utils/src/__test__/primer.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,25 @@ describe("getNavigationRoot", () => {
);

expect(urls?.type === "found" ? urls.found.versions : []).toMatchSnapshot();
expect(urls?.type === "found" ? urls.found.currentVersionIndex : undefined).equal(0);
});

it("gets navigation root for /docs/api/v2.1/api-reference/client-session-api/retrieve-client-side-token", async () => {
const fixturePath = path.join(__dirname, "fixtures", "primer.json");

const content = fs.readFileSync(fixturePath, "utf-8");

const fixture = JSON.parse(content) as DocsV2Read.LoadDocsForUrlResponse;

const urls = getNavigationRoot(
"docs/api/v2.1/api-reference/client-session-api/retrieve-client-side-token".split("/"),
fixture.baseUrl.basePath,
fixture.definition.config.navigation,
fixture.definition.apis,
);

expect(urls?.type === "found" ? urls.found.versions : []).toMatchSnapshot();
expect(urls?.type === "found" ? urls.found.currentVersionIndex : undefined).equal(2);
});
});
});
49 changes: 27 additions & 22 deletions packages/commons/fdr-utils/src/getNavigationRoot.ts
Original file line number Diff line number Diff line change
Expand Up @@ -73,34 +73,39 @@ export function getNavigationRoot(

const sidebarNodes = findSiblings<SidebarNodeRaw>(parents, isSidebarNodeRaw).items;

const tabs = findSiblings<SidebarNodeRaw.TabGroup>(
const { items: tabItems, node: currentTab } = findSiblings<SidebarNodeRaw.TabGroup>(
parents,
(n): n is SidebarNodeRaw.TabGroup => n.type === "tabGroup",
);

const versions = findSiblings<SidebarNodeRaw.VersionGroup>(
const tabs = tabItems.map((tab) => ({
title: tab.title,
icon: tab.icon,
slug: tab.slug,
index: tab.index,
}));

const { items: versionItems, node: currentVerion } = findSiblings<SidebarNodeRaw.VersionGroup>(
parents,
(n): n is SidebarNodeRaw.VersionGroup => n.type === "versionGroup",
);

const versions = versionItems
.map((version) => ({
id: version.id,
slug: version.slug,
index: version.index,
availability: version.availability,
}))
// get rid of duplicate version
.filter((version, idx) => version.index > 0 || version.index === idx);

// const sidebarNodes = await Promise.all(rawSidebarNodes.map((node) => serializeSidebarNodeDescriptionMdx(node)));
const found: SidebarNavigationRaw = {
currentVersionIndex: versions.index,
versions: versions.items
.map((version) => ({
id: version.id,
slug: version.slug,
index: version.index,
availability: version.availability,
}))
// get rid of duplicate version
.filter((version, idx) => version.index > 0 || version.index === idx),
currentTabIndex: tabs.index,
tabs: tabs.items.map((tab) => ({
title: tab.title,
icon: tab.icon,
slug: tab.slug,
})),
currentVersionIndex: currentVerion?.index ?? -1,
versions,
currentTabIndex: currentTab?.index ?? -1,
tabs,
sidebarNodes,
currentNode: node,
};
Expand Down Expand Up @@ -141,13 +146,13 @@ function resolveRedirect(node: SidebarNodeRaw.VisitableNode | undefined, from: s
function findSiblings<T extends SidebarNodeRaw.ParentNode>(
parents: readonly SidebarNodeRaw.ParentNode[],
match: (node: SidebarNodeRaw.ParentNode) => node is T,
): { items: readonly T[]; index: number } {
): { items: readonly T[]; node: T | undefined } {
const navigationDepth = parents.findIndex(match);

const parent = parents[navigationDepth - 1] ?? parents[parents.length - 1];

if (parent == null) {
return { items: [], index: -1 };
return { items: [], node: undefined };
}

if (
Expand All @@ -156,8 +161,8 @@ function findSiblings<T extends SidebarNodeRaw.ParentNode>(
parent.type !== "versionGroup" &&
parent.type !== "section"
) {
return { items: [], index: -1 };
return { items: [], node: undefined };
}

return { items: parent.items as T[], index: parent.items.findIndex((node) => node === parents[navigationDepth]) };
return { items: parent.items as T[], node: parents[navigationDepth] as T | undefined };
}
3 changes: 2 additions & 1 deletion packages/commons/fdr-utils/src/resolver.ts
Original file line number Diff line number Diff line change
Expand Up @@ -205,13 +205,14 @@ function resolveSidebarNodesVersionItems(
parentSlugs: readonly string[],
): SidebarNodeRaw.VersionGroup["items"] {
if (isUnversionedTabbedNavigationConfig(nav)) {
return nav.tabs.map((tab): SidebarNodeRaw.TabGroup => {
return nav.tabs.map((tab, index): SidebarNodeRaw.TabGroup => {
const tabSlug = [...parentSlugs, ...tab.urlSlug.split("/")];
return {
type: "tabGroup",
title: tab.title,
icon: tab.icon,
slug: tabSlug,
index,
items: resolveSidebarNodes(tab.items, apis, tabSlug, parentSlugs),
};
});
Expand Down
1 change: 1 addition & 0 deletions packages/commons/fdr-utils/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ export interface SidebarVersionInfo {
export interface SidebarTab {
title: string;
icon: string;
index: number;
slug: readonly string[];
}

Expand Down

0 comments on commit 4bad116

Please sign in to comment.