-
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.
feat(landingpage): reflect elements storybook version in URL (#1380)
Closes #1338 --------- Co-authored-by: Jan-Niklas Voß <[email protected]>
- Loading branch information
1 parent
80272ca
commit 4e309ac
Showing
3 changed files
with
39 additions
and
41 deletions.
There are no files selected for viewing
33 changes: 26 additions & 7 deletions
33
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,33 +1,19 @@ | ||
import { createContext, ReactNode, useState, useEffect } from 'react'; | ||
|
||
export interface VersionContextType { | ||
selectedVersion: string | undefined; | ||
setSelectedVersion: (value: string | undefined) => void; | ||
versions: string[]; | ||
} | ||
|
||
export const VersionContext = createContext<VersionContextType | undefined>(undefined); | ||
|
||
export const VersionProvider = ({ children }: { children: ReactNode }) => { | ||
const [selectedVersion, setSelectedVersion] = useState<string | undefined>(undefined); | ||
const [versions, setVersions] = useState<string[]>([]); | ||
|
||
useEffect(() => { | ||
fetch('https://raw.githubusercontent.com/inovex/elements/pages/hosted-versions.json') | ||
.then(response => response.json()) | ||
.then(data => { | ||
const reversedVersions = data.reverse(); | ||
setVersions(reversedVersions); | ||
|
||
if (reversedVersions.length > 0) { | ||
setSelectedVersion(reversedVersions[0]); | ||
} | ||
}); | ||
.then(data => setVersions(data.reverse())); | ||
}, []); | ||
|
||
return ( | ||
<VersionContext.Provider value={{ selectedVersion, setSelectedVersion, versions }}> | ||
{children} | ||
</VersionContext.Provider> | ||
); | ||
return <VersionContext.Provider value={{ versions }}>{children}</VersionContext.Provider>; | ||
}; |
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,29 +1,22 @@ | ||
import { useEffect, useState } from 'react'; | ||
import { useEffect, useState, useMemo } 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 version = searchParams.get('version'); | ||
const storybookUrl = useMemo(() => { | ||
const version = searchParams.get('version') || 'latest'; // Default to 'latest' | ||
const element = searchParams.get('element') || WELCOME_PAGE_PLACEHOLDER; | ||
const url = process.env.NEXT_PUBLIC_STORYBOOK_URL; | ||
|
||
useEffect(() => { | ||
if (!process.env.NEXT_PUBLIC_STORYBOOK_URL) | ||
if (!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); | ||
} | ||
|
||
const newUrl = [baseStorybookUrl, '?path=/docs/', searchParams?.get('element') ?? WELCOME_PAGE_PLACEHOLDER].join( | ||
'', | ||
); | ||
|
||
setInitialStorybookUrl(newUrl); | ||
}, [selectedVersion]); | ||
return `${url.replace('latest', version)}?path=/docs/${element}`; | ||
}, [version]); | ||
|
||
return initialStorybookUrl; | ||
return storybookUrl; | ||
}; |