Skip to content

Commit 7d73fb5

Browse files
committed
add component
1 parent 40036ee commit 7d73fb5

File tree

4 files changed

+44
-3
lines changed

4 files changed

+44
-3
lines changed
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
'use client';
2+
3+
import {useEffect, useState} from 'react';
4+
import Link from 'next/link';
5+
6+
import {KEY_ACTIVE_PLATFORM} from 'sentry-docs/constants/localStorage';
7+
8+
interface Props {
9+
children: React.ReactNode;
10+
className?: string;
11+
}
12+
const DEFAULT_LINK = '/platforms';
13+
14+
export function LinkToActivePlatform({className, children}: Props) {
15+
const [href, setHref] = useState(DEFAULT_LINK);
16+
17+
useEffect(() => {
18+
if (typeof window === 'undefined' || typeof localStorage === 'undefined') return;
19+
20+
let activePlatform = localStorage.getItem(KEY_ACTIVE_PLATFORM);
21+
if (activePlatform) {
22+
const segments = activePlatform.split('.');
23+
if (segments.length === 2) {
24+
activePlatform = `${segments[0]}/guides/${segments[1]}`;
25+
} else {
26+
activePlatform = `${segments[0]}`;
27+
}
28+
setHref(`${DEFAULT_LINK}/${activePlatform}`);
29+
}
30+
}, []);
31+
32+
return (
33+
<Link href={href} className={className}>
34+
{children}
35+
</Link>
36+
);
37+
}

src/components/platformSelector/index.tsx

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ import {matchSorter} from 'match-sorter';
1515
import {usePathname, useRouter} from 'next/navigation';
1616

1717
import {PlatformIcon} from 'sentry-docs/components/platformIcon';
18+
import {KEY_ACTIVE_PLATFORM} from 'sentry-docs/constants/localStorage';
1819
import {Platform, PlatformGuide, PlatformIntegration} from 'sentry-docs/types';
1920
import {uniqByReference} from 'sentry-docs/utils';
2021

@@ -94,7 +95,7 @@ export function PlatformSelector({
9495
const onPlatformChange = (platformKey: string) => {
9596
const platform_ = platformsAndGuides.find(platform => platform.key === platformKey);
9697
if (platform_) {
97-
localStorage.setItem('active-platform', platform_.key);
98+
localStorage.setItem(KEY_ACTIVE_PLATFORM, platform_.key);
9899
router.push(platform_.url);
99100
}
100101
};
@@ -114,9 +115,9 @@ export function PlatformSelector({
114115
);
115116
useEffect(() => {
116117
if (currentPlatformKey) {
117-
localStorage.setItem('active-platform', currentPlatformKey);
118+
localStorage.setItem(KEY_ACTIVE_PLATFORM, currentPlatformKey);
118119
} else {
119-
setStoredPlatformKey(localStorage.getItem('active-platform'));
120+
setStoredPlatformKey(localStorage.getItem(KEY_ACTIVE_PLATFORM));
120121
}
121122
}, [currentPlatformKey]);
122123

src/constants/localStorage.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
export const KEY_ACTIVE_PLATFORM = 'active-platform';

src/mdxComponents.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ import {Expandable} from './components/expandable';
1616
import {GuideGrid} from './components/guideGrid';
1717
import {JsBundleList} from './components/jsBundleList';
1818
import {LambdaLayerDetail} from './components/lambdaLayerDetail';
19+
import {LinkToActivePlatform} from './components/linkToActivePlatform';
1920
import {LinkWithPlatformIcon} from './components/linkWithPlatformIcon';
2021
import {OnboardingOption, OnboardingOptionButtons} from './components/onboarding';
2122
import {OrgAuthTokenNote} from './components/orgAuthTokenNote';
@@ -66,6 +67,7 @@ export function mdxComponents(
6667
LambdaLayerDetail,
6768
Link: SmartLink,
6869
LinkWithPlatformIcon,
70+
LinkToActivePlatform,
6971
OrgAuthTokenNote,
7072
PageGrid,
7173
ParamTable,

0 commit comments

Comments
 (0)