-
Notifications
You must be signed in to change notification settings - Fork 551
feat: add same deployment link util #2546
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
625e295
feat: introduce SameDeploymentLink component for deployment-aware nav…
wassimoo 768d2c1
chore: update CODEOWNERS to include @wassimoo for code examples and g…
wassimoo 1e2c20b
refactor: simplify SameDeploymentLink component
wassimoo 9c5be3a
fix: reflect deployment model context in sidebar picker
wassimoo File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,4 +1,4 @@ | ||
| * @vinckr @aeneasr @zepatrik @piotrmsc @unatasha8 | ||
| * @vinckr @aeneasr @zepatrik @piotrmsc @unatasha8 @wassimoo | ||
| *.md @vinckr @aeneasr @unatasha8 | ||
| *.mdx @vinckr @aeneasr @unatasha8 | ||
| /code-examples/ @aeneasr @zepatrik @piotrmsc | ||
| /code-examples/ @aeneasr @zepatrik @piotrmsc @wassimoo |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,78 @@ | ||
| import React, { useEffect, type ReactNode } from "react" | ||
| import Link from "@docusaurus/Link" | ||
| import { useLocation } from "@docusaurus/router" | ||
| import { useAllDocsData } from "@docusaurus/plugin-content-docs/client" | ||
|
|
||
| type DocsDeploymentSegment = "network" | "oel" | "oss" | ||
|
|
||
| type SameDeploymentLinkProps = { | ||
| to: string | ||
| // Optional overrides for the link target based on the deployment segment | ||
| network?: string | ||
| oel?: string | ||
| oss?: string | ||
| children: ReactNode | ||
| } | ||
|
|
||
| const DEFAULT_DOCS_PLUGIN_ID = "default" | ||
|
|
||
| const DEPLOYMENT_SEGMENT_PATTERN = /\/(network|oel|oss)(?:\/|$)/ | ||
|
|
||
| const stripLeadingSlashes = (value: string) => value.replace(/^\/+/, "") | ||
| const normalizePath = (value: string) => `/${stripLeadingSlashes(value)}` | ||
|
|
||
| /** | ||
| * Internal docs link that keeps the reader on the current deployment (Network / OEL / OSS). | ||
| */ | ||
| export default function SameDeploymentLink({ | ||
| to, | ||
| network, | ||
| oel, | ||
| oss, | ||
| children, | ||
| }: SameDeploymentLinkProps): JSX.Element { | ||
| const { pathname } = useLocation() | ||
| const segment = | ||
| (pathname.match(DEPLOYMENT_SEGMENT_PATTERN)?.[1] as | ||
| | DocsDeploymentSegment | ||
| | undefined) ?? "network" | ||
|
|
||
| const rewriteToCurrentDeployment = (value: string): string => { | ||
| const path = normalizePath(value) | ||
| const match = path.match(DEPLOYMENT_SEGMENT_PATTERN) | ||
| if (match) { | ||
| // Replace explicit deployment segment with the current one. | ||
| return path.replace(DEPLOYMENT_SEGMENT_PATTERN, `/${segment}/`) | ||
| } | ||
| // Prefix when no deployment segment is present. | ||
| return `/${segment}${path}` | ||
| } | ||
|
|
||
| const overrideForCurrentDeployment = { network, oel, oss }[segment] | ||
| const href = overrideForCurrentDeployment | ||
| ? normalizePath(overrideForCurrentDeployment) | ||
| : rewriteToCurrentDeployment(to) | ||
| const allDocs = useAllDocsData() | ||
|
|
||
| useEffect(() => { | ||
| if (!DEPLOYMENT_SEGMENT_PATTERN.test(href)) return | ||
| const plugin = allDocs[DEFAULT_DOCS_PLUGIN_ID] | ||
| const version = | ||
| plugin?.versions.find((v) => v.isLast) ?? plugin?.versions[0] | ||
| if (!version?.docs?.length) return | ||
|
|
||
| const docId = stripLeadingSlashes(href) | ||
| const found = version.docs.some((d) => d.id === docId) | ||
| if (!found) { | ||
| const overrideMsg = overrideForCurrentDeployment | ||
| ? ` (override prop "${segment}" was used)` | ||
| : "" | ||
| console.warn( | ||
| `[SameDeploymentLink] No doc with id "${docId}". ` + | ||
| `Resolved href="${href}" from to="${to}" under deployment "${segment}" did not match any page in this build${overrideMsg}.`, | ||
| ) | ||
| } | ||
| }, [allDocs, href, overrideForCurrentDeployment, segment, to]) | ||
|
|
||
| return <Link to={href}>{children}</Link> | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,27 @@ | ||
| export type DocsDeploymentId = "network" | "oel" | "oss" | ||
|
|
||
| /** | ||
| * Infer the docs deployment from a pathname. | ||
| * | ||
| * Note: Some routes still use legacy `/docs/self-hosted/oel/...` paths. | ||
| */ | ||
| export function docsDeploymentFromPathname(pathname: string): DocsDeploymentId { | ||
| if (!pathname) return "network" | ||
|
|
||
| if (pathname.includes("/oel/") || pathname.includes("/self-hosted/oel/")) | ||
| return "oel" | ||
| if (pathname.includes("/oss/")) return "oss" | ||
| if (pathname.includes("/network/")) return "network" | ||
|
|
||
| return "network" | ||
| } | ||
|
|
||
| export function isExplicitDocsDeploymentPath(pathname: string): boolean { | ||
| if (!pathname) return false | ||
| return ( | ||
| pathname.includes("/network/") || | ||
| pathname.includes("/oel/") || | ||
| pathname.includes("/oss/") || | ||
| pathname.includes("/self-hosted/oel/") | ||
| ) | ||
| } |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.