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

Add a ScopeLink component #35

Closed

Conversation

ptgott
Copy link
Contributor

@ptgott ptgott commented Apr 7, 2022

In the docs, there are various guides that are entirely irrelevant for
a user of a particular scope. In these cases, we usually include a
warning at the top of each guide that indicates that the guide is only
relevant for Cloud, Open Source, or Enterprise users. Rather than force
the user to guess that they need to change the value of the scope
picker, the ScopeLink component enables us to add an inline link that
changes the scope of the page. For example:

<Notice
  type="warning"
  scope={["oss"]}
>

  This guide requires Teleport Cloud or Teleport Enterprise.

  View this guide as a <ScopeLink scope="cloud">Teleport Cloud
  user</ScopeLink> or <ScopeLink scope="enterprise">Teleport
  Enterprise user</ScopeLink>.

</Notice>

In the docs, there are various guides that are entirely irrelevant for
a user of a particular scope. In these cases, we usually include a
warning at the top of each guide that indicates that the guide is only
relevant for Cloud, Open Source, or Enterprise users. Rather than force
the user to guess that they need to change the value of the scope
picker, the ScopeLink component enables us to add an inline link that
changes the scope of the page. For example:

```html
<Notice
  type="warning"
  scope={["oss"]}
>

  This guide requires Teleport Cloud or Teleport Enterprise.

  View this guide as a <ScopeLink scope="cloud">Teleport Cloud
  user</ScopeLink> or <ScopeLink scope="enterprise">Teleport
  Enterprise user</ScopeLink>.

</Notice>
```
@vercel
Copy link

vercel bot commented Apr 7, 2022

@ptgott is attempting to deploy a commit to the gravitational Team on Vercel.

A member of the Team first needs to authorize it.

@ptgott
Copy link
Contributor Author

ptgott commented Apr 7, 2022

Demo video:

scopelink.mp4

@ptgott ptgott mentioned this pull request Apr 7, 2022
@ptgott
Copy link
Contributor Author

ptgott commented Apr 20, 2022

Hi @iAdramelk @Alqanar @C-STYR, I was wondering if you had any thoughts on this draft PR or #33 for handling situations where a user with one scope may need to switch scopes in order to see a particular page.

I was working on using the hideInScopes field in config.json as well as the the ScopedBlock component to hide links in the docs navigation sidebar and menu pages when those links were irrelevant to a reader's scope. There were some edge cases where a user might want to read a page that is relevant outside of their scope, e.g., they're an oss user but want to read the Enterprise getting started guide.

Are there other solutions that would work better here? Thanks!

@iAdramelk
Copy link
Contributor

@ptgott hi Paul,sorry for the late answer.

I have two suggestions here:

  1. Right now ScopeLink will only work with current URL. My guess will be, that we may need to link to different pages in the correct scope too.
  2. onClick will only work on client and will be broke with SSR. Why not write actual href in it? E. g. ${hrefscope=${scope}. This way it will work in SSR too.

How I would approach it: I would update normal Link component to include optional scope prop - it it is set - always use provided scope, if now set current scope dynamically.

E. g. <Link href="/" scope="oss">Got to Introduction page</Link>.

@ptgott
Copy link
Contributor Author

ptgott commented May 4, 2022

Hi @iAdramelk, thanks for the feedback. I did some more digging, and it looks like it might be enough to make the following change to useNormalizedHref, which extracts the scope query parameter from a link href and, if it's oss, enterprise, or cloud, uses that instead of useContext(DocsContext) to obtain the scope.

This way, we can add a link [like this](../getting-started.mdx?scope=cloud) and it will link to that scope.

Currently, useNormalizedHref only uses the href argument to define noBaseHref, which means that any query parameters added to a link href get ignored.

diff --git a/components/Link/hooks.ts b/components/Link/hooks.ts
index eb231f0..894ed38 100644
--- a/components/Link/hooks.ts
+++ b/components/Link/hooks.ts
@@ -25,13 +25,28 @@ export const useCurrentHref = () => {
  */
 
 export const useNormalizedHref = (href: string) => {
+
   const { asPath, basePath } = useRouter();
 
   const noBaseHref = href.startsWith(basePath)
     ? href.substring(basePath.length)
     : href;
 
-  const { scope } = useContext(DocsContext);
+    const scopes = {
+      "oss":true,
+      "cloud":true,
+      "enterprise": true
+    }
+
+    let scope: "oss"|"cloud"|"enterprise" = "oss"
+
+    const { query } = splitPath(href)
+    if(query.hasOwnProperty("scope") && scopes[query.scope] == true ){
+      scope = query["scope"] as "oss"|"cloud"|"enterprise"
+    } else {
+      scope = useContext(DocsContext).scope;
+    }
+
 
   if (
     isHash(noBaseHref) ||

I've tested this out with a file called docs/pages/sample.mdx:

---
title: sample page
---

[Cloud](../sample?scope=cloud)
[Enterprise](../sample?scope=enterprise)

@iAdramelk
Copy link
Contributor

@ptgott Good idea! This will be more universal.

ptgott added a commit to ptgott/docs that referenced this pull request May 9, 2022
Closes gravitational#35
Closes gravitational#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").
ptgott added a commit to ptgott/docs that referenced this pull request May 10, 2022
Closes gravitational#35
Closes gravitational#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").
ptgott added a commit to ptgott/docs that referenced this pull request May 10, 2022
Closes gravitational#35
Closes gravitational#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").
@ptgott ptgott closed this May 10, 2022
ptgott added a commit to ptgott/docs that referenced this pull request May 10, 2022
Closes gravitational#35
Closes gravitational#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").
C-STYR pushed a commit that referenced this pull request May 12, 2022
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").
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants