Skip to content

Commit 87ed033

Browse files
Refactor JavaScript platform redirect and improve stored platform handling
Co-authored-by: shannon.anahata <[email protected]>
1 parent fc3723e commit 87ed033

File tree

1 file changed

+27
-9
lines changed

1 file changed

+27
-9
lines changed

src/components/platformSelector/index.tsx

Lines changed: 27 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -87,12 +87,21 @@ export function PlatformSelector({
8787

8888
const router = useRouter();
8989
const onPlatformChange = (platformKey: string) => {
90-
const platform_ = platformsAndGuides.find(
91-
platform => platform.key === platformKey.replace('-redirect', '')
92-
);
93-
if (platform_) {
94-
localStorage.setItem('active-platform', platform_.key);
95-
router.push(platform_.url);
90+
const cleanKey = platformKey.replace('-redirect', '');
91+
let targetPlatform = platformsAndGuides.find(platform => platform.key === cleanKey);
92+
93+
// Special handling for JavaScript: when platform "javascript" is selected,
94+
// redirect to the virtual guide "javascript-platform" instead
95+
if (cleanKey === 'javascript' && targetPlatform?.type === 'platform') {
96+
const virtualGuide = platformsAndGuides.find(p => p.key === 'javascript-platform');
97+
if (virtualGuide) {
98+
targetPlatform = virtualGuide;
99+
}
100+
}
101+
102+
if (targetPlatform) {
103+
localStorage.setItem('active-platform', targetPlatform.key);
104+
router.push(targetPlatform.url);
96105
}
97106
};
98107

@@ -106,9 +115,18 @@ export function PlatformSelector({
106115
}, [open]);
107116

108117
const [storedPlatformKey, setStoredPlatformKey] = useState<string | null>(null);
109-
const storedPlatform = platformsAndGuides.find(
118+
let storedPlatform = platformsAndGuides.find(
110119
platform => platform.key === storedPlatformKey
111120
);
121+
122+
// Handle stored JavaScript platform: redirect to virtual guide
123+
if (storedPlatformKey === 'javascript' && storedPlatform?.type === 'platform') {
124+
const virtualGuide = platformsAndGuides.find(p => p.key === 'javascript-platform');
125+
if (virtualGuide) {
126+
storedPlatform = virtualGuide;
127+
}
128+
}
129+
112130
useEffect(() => {
113131
if (currentPlatformKey) {
114132
localStorage.setItem('active-platform', currentPlatformKey);
@@ -134,7 +152,7 @@ export function PlatformSelector({
134152
<div>
135153
<RadixSelect.Root
136154
defaultValue={currentPlatformKey}
137-
value={showStoredPlatform ? storedPlatformKey : undefined}
155+
value={showStoredPlatform ? storedPlatform?.key : undefined}
138156
onValueChange={onPlatformChange}
139157
open={open}
140158
onOpenChange={setOpen}
@@ -234,7 +252,7 @@ export function PlatformSelector({
234252
</RadixSelect.Content>
235253
</ComboboxProvider>
236254
</RadixSelect.Root>
237-
{showStoredPlatform && (
255+
{showStoredPlatform && storedPlatform && (
238256
<div className="mt-3">
239257
<SidebarLink
240258
href={storedPlatform.url}

0 commit comments

Comments
 (0)