Skip to content
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

feat(platform): Add LinkToActivePlatform component #13020

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 37 additions & 0 deletions src/components/linkToActivePlatform.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
'use client';

import {useEffect, useState} from 'react';
import Link from 'next/link';

import {KEY_ACTIVE_PLATFORM} from 'sentry-docs/constants/localStorage';

interface Props {
children: React.ReactNode;
className?: string;
}
const DEFAULT_LINK = '/platforms';

export function LinkToActivePlatform({className, children}: Props) {
const [href, setHref] = useState(DEFAULT_LINK);

useEffect(() => {
if (typeof window === 'undefined' || typeof localStorage === 'undefined') return;

let activePlatform = localStorage.getItem(KEY_ACTIVE_PLATFORM);
if (activePlatform) {
const segments = activePlatform.split('.');
if (segments.length === 2) {
activePlatform = `${segments[0]}/guides/${segments[1]}`;
} else {
activePlatform = `${segments[0]}`;
}
setHref(`${DEFAULT_LINK}/${activePlatform}`);
}
}, []);

return (
<Link href={href} className={className}>
{children}
</Link>
);
}
7 changes: 4 additions & 3 deletions src/components/platformSelector/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import {matchSorter} from 'match-sorter';
import {usePathname, useRouter} from 'next/navigation';

import {PlatformIcon} from 'sentry-docs/components/platformIcon';
import {KEY_ACTIVE_PLATFORM} from 'sentry-docs/constants/localStorage';
import {Platform, PlatformGuide, PlatformIntegration} from 'sentry-docs/types';
import {uniqByReference} from 'sentry-docs/utils';

Expand Down Expand Up @@ -94,7 +95,7 @@ export function PlatformSelector({
const onPlatformChange = (platformKey: string) => {
const platform_ = platformsAndGuides.find(platform => platform.key === platformKey);
if (platform_) {
localStorage.setItem('active-platform', platform_.key);
localStorage.setItem(KEY_ACTIVE_PLATFORM, platform_.key);
router.push(platform_.url);
}
};
Expand All @@ -114,9 +115,9 @@ export function PlatformSelector({
);
useEffect(() => {
if (currentPlatformKey) {
localStorage.setItem('active-platform', currentPlatformKey);
localStorage.setItem(KEY_ACTIVE_PLATFORM, currentPlatformKey);
} else {
setStoredPlatformKey(localStorage.getItem('active-platform'));
setStoredPlatformKey(localStorage.getItem(KEY_ACTIVE_PLATFORM));
}
}, [currentPlatformKey]);

Expand Down
1 change: 1 addition & 0 deletions src/constants/localStorage.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export const KEY_ACTIVE_PLATFORM = 'active-platform';
2 changes: 2 additions & 0 deletions src/mdxComponents.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import {Expandable} from './components/expandable';
import {GuideGrid} from './components/guideGrid';
import {JsBundleList} from './components/jsBundleList';
import {LambdaLayerDetail} from './components/lambdaLayerDetail';
import {LinkToActivePlatform} from './components/linkToActivePlatform';
import {LinkWithPlatformIcon} from './components/linkWithPlatformIcon';
import {OnboardingOption, OnboardingOptionButtons} from './components/onboarding';
import {OrgAuthTokenNote} from './components/orgAuthTokenNote';
Expand Down Expand Up @@ -66,6 +67,7 @@ export function mdxComponents(
LambdaLayerDetail,
Link: SmartLink,
LinkWithPlatformIcon,
LinkToActivePlatform,
OrgAuthTokenNote,
PageGrid,
ParamTable,
Expand Down
Loading