Skip to content
This repository has been archived by the owner on Jan 8, 2025. It is now read-only.

Commit

Permalink
Allow link hrefs with a scope query param
Browse files Browse the repository at this point in the history
Closes #35
Closes #33
See gravitational/teleport#11383

Previously, the Link component would always set the "scope" query
parameter in its href to the current value of DocsContext.scope.
I wanted to make it possible to link to other scopes, allowing readers
who are interested in another edition of Teleport to read content
intended for that edition.

We could add explicit links to different
scopes in, for example, warnings at the top of scope-irrelevant
pages or in introduction pages to sections that are only helpful for
a specific Teleport edition (e.g., the Enterprise section).

This change edits useNormalizedHref to add a "scope" value to the
href's query if the input href has a "scope" key and the value is
a valid scope name ("oss", "cloud", or "enterprise").
  • Loading branch information
ptgott committed May 10, 2022
1 parent 6adc47a commit 73f7d10
Showing 1 changed file with 16 additions and 1 deletion.
17 changes: 16 additions & 1 deletion components/Link/hooks.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { resolve } from "url";
import { useRouter } from "next/router";
import { useContext } from "react";
import { ScopeType } from "layouts/DocsPage/types";
import {
normalizePath,
splitPath,
Expand Down Expand Up @@ -31,7 +32,21 @@ export const useNormalizedHref = (href: string) => {
? href.substring(basePath.length)
: href;

const { scope } = useContext(DocsContext);
const scopes = {
oss: true,
cloud: true,
enterprise: true,
};

let scope: ScopeType = useContext(DocsContext).scope;

const { query } = splitPath(href);

// If a valid scope is provided via query parameter, adjust the
// link to navigate to that scope.
if (query.hasOwnProperty("scope") && scopes[query.scope] == true) {
scope = query["scope"] as ScopeType;
}

if (
isHash(noBaseHref) ||
Expand Down

0 comments on commit 73f7d10

Please sign in to comment.