Skip to content

Commit ee1c1d7

Browse files
Refactor platform selector logic for clarity and correctness
Co-authored-by: shannon.anahata <[email protected]>
1 parent d829e32 commit ee1c1d7

File tree

1 file changed

+11
-33
lines changed

1 file changed

+11
-33
lines changed

src/components/platformSelector/index.tsx

Lines changed: 11 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -68,42 +68,28 @@ export function PlatformSelector({
6868
if (!searchValue) {
6969
return platformsAndGuides;
7070
}
71-
72-
// Find the currently selected platform/guide first
73-
const selectedPlatform = platformsAndGuides.find(
74-
lang => lang.key === currentPlatformKey
75-
);
76-
7771
// any of these fields can be used to match the search value
7872
const keys = ['title', 'name', 'aliases', 'sdk', 'keywords'];
79-
let matches_ = matchSorter(platformsAndGuides, searchValue, {
73+
const matches_ = matchSorter(platformsAndGuides, searchValue, {
8074
keys,
8175
threshold: matchSorter.rankings.ACRONYM,
8276
});
83-
84-
// For virtual guides (like javascript-platform), ensure they're always included
85-
// when they're the current selection, to prevent focus/display issues
77+
// Radix Select does not work if we don't render the selected item, so we
78+
// make sure to include it in the list of matches.
79+
const selectedPlatform = platformsAndGuides.find(
80+
lang => lang.key === currentPlatformKey
81+
);
8682
if (selectedPlatform && !matches_.includes(selectedPlatform)) {
87-
matches_ = [selectedPlatform, ...matches_];
83+
matches_.push(selectedPlatform);
8884
}
89-
9085
return matches_;
9186
}, [searchValue, currentPlatformKey, platformsAndGuides]);
9287

9388
const router = useRouter();
9489
const onPlatformChange = (platformKey: string) => {
9590
const cleanKey = platformKey.replace('-redirect', '');
96-
let targetPlatform = platformsAndGuides.find(platform => platform.key === cleanKey);
97-
98-
// Special handling for JavaScript: when platform "javascript" is selected,
99-
// redirect to the virtual guide "javascript.browser" instead
100-
if (cleanKey === 'javascript' && targetPlatform?.type === 'platform') {
101-
const virtualGuide = platformsAndGuides.find(p => p.key === 'javascript.browser');
102-
if (virtualGuide) {
103-
targetPlatform = virtualGuide;
104-
}
105-
}
106-
91+
const targetPlatform = platformsAndGuides.find(platform => platform.key === cleanKey);
92+
10793
if (targetPlatform) {
10894
localStorage.setItem('active-platform', targetPlatform.key);
10995
router.push(targetPlatform.url);
@@ -120,18 +106,10 @@ export function PlatformSelector({
120106
}, [open]);
121107

122108
const [storedPlatformKey, setStoredPlatformKey] = useState<string | null>(null);
123-
let storedPlatform = platformsAndGuides.find(
109+
const storedPlatform = platformsAndGuides.find(
124110
platform => platform.key === storedPlatformKey
125111
);
126112

127-
// Handle stored JavaScript platform: redirect to virtual guide
128-
if (storedPlatformKey === 'javascript' && storedPlatform?.type === 'platform') {
129-
const virtualGuide = platformsAndGuides.find(p => p.key === 'javascript.browser');
130-
if (virtualGuide) {
131-
storedPlatform = virtualGuide;
132-
}
133-
}
134-
135113
useEffect(() => {
136114
if (currentPlatformKey) {
137115
localStorage.setItem('active-platform', currentPlatformKey);
@@ -157,7 +135,7 @@ export function PlatformSelector({
157135
<div>
158136
<RadixSelect.Root
159137
defaultValue={currentPlatformKey}
160-
value={showStoredPlatform ? storedPlatform?.key : undefined}
138+
value={showStoredPlatform ? storedPlatformKey : undefined}
161139
onValueChange={onPlatformChange}
162140
open={open}
163141
onOpenChange={setOpen}

0 commit comments

Comments
 (0)