-
Notifications
You must be signed in to change notification settings - Fork 9
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
6978c7d
commit 5c87ddd
Showing
3 changed files
with
43 additions
and
66 deletions.
There are no files selected for viewing
51 changes: 30 additions & 21 deletions
51
packages/landingpage/app/[lang]/_components/layout/header/versionSelect.tsx
This file contains 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 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 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,35 +1,25 @@ | ||
import { useEffect, useState } from 'react'; | ||
import { useSearchParams } from 'next/navigation'; | ||
import { useVersion } from '@hooks/useVersion'; | ||
|
||
export const WELCOME_PAGE_PLACEHOLDER = 'docs-welcome--docs'; | ||
|
||
export const useStorybookUrl = () => { | ||
const { selectedVersion } = useVersion(); | ||
const searchParams = useSearchParams(); | ||
const [initialStorybookUrl, setInitialStorybookUrl] = useState<string | null>( | ||
null, | ||
); | ||
const [initialStorybookUrl, setInitialStorybookUrl] = useState<string | null>(null); | ||
|
||
useEffect(() => { | ||
if (!process.env.NEXT_PUBLIC_STORYBOOK_URL) | ||
throw new Error( | ||
'NEXT_PUBLIC_STORYBOOK_URL not found in environment variables.', | ||
); | ||
|
||
let baseStorybookUrl = process.env.NEXT_PUBLIC_STORYBOOK_URL; | ||
|
||
if (selectedVersion) { | ||
baseStorybookUrl = baseStorybookUrl.replace('latest', selectedVersion); | ||
if (!process.env.NEXT_PUBLIC_STORYBOOK_URL) { | ||
throw new Error('NEXT_PUBLIC_STORYBOOK_URL not found in environment variables.'); | ||
} | ||
|
||
const newUrl = [ | ||
baseStorybookUrl, | ||
'?path=/docs/', | ||
searchParams?.get('element') ?? WELCOME_PAGE_PLACEHOLDER, | ||
].join(''); | ||
const version = searchParams.get('version') || 'latest'; // Use 'latest' if no version is specified | ||
const element = searchParams.get('element') || WELCOME_PAGE_PLACEHOLDER; | ||
|
||
// Construct the URL by directly inserting the version and element | ||
const newUrl = `${process.env.NEXT_PUBLIC_STORYBOOK_URL.replace('latest', version)}?path=/docs/${element}`; | ||
|
||
setInitialStorybookUrl(newUrl); | ||
}, [selectedVersion]); | ||
}, [searchParams]); | ||
|
||
return initialStorybookUrl; | ||
}; |