Skip to content

Commit ae3fc1d

Browse files
Refactor: Improve platform selector search and virtual guide stability
Co-authored-by: shannon.anahata <[email protected]>
1 parent b824aad commit ae3fc1d

File tree

2 files changed

+28
-9
lines changed

2 files changed

+28
-9
lines changed

src/components/platformSelector/index.tsx

Lines changed: 24 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
'use client';
2-
import {Fragment, Ref, useEffect, useMemo, useRef, useState} from 'react';
2+
import {Fragment, Ref, startTransition, useCallback, useEffect, useMemo, useRef, useState} from 'react';
33
import {Combobox, ComboboxItem, ComboboxList, ComboboxProvider} from '@ariakit/react';
44
import {CaretRightIcon, CaretSortIcon, MagnifyingGlassIcon} from '@radix-ui/react-icons';
55
import * as RadixSelect from '@radix-ui/react-select';
@@ -63,25 +63,40 @@ export function PlatformSelector({
6363
const currentPlatformKey = currentPlatform?.key;
6464
const [open, setOpen] = useState(false);
6565
const [searchValue, setSearchValue] = useState('');
66+
67+
// Controlled search handler for virtual guide stability
68+
const handleSearchValueChange = useCallback((value: string) => {
69+
// Use startTransition for virtual guides to prevent focus issues
70+
if (currentPlatformKey === 'javascript-platform') {
71+
startTransition(() => setSearchValue(value));
72+
} else {
73+
setSearchValue(value);
74+
}
75+
}, [currentPlatformKey]);
6676

6777
const matches = useMemo(() => {
6878
if (!searchValue) {
6979
return platformsAndGuides;
7080
}
81+
82+
// Find the currently selected platform/guide first
83+
const selectedPlatform = platformsAndGuides.find(
84+
lang => lang.key === currentPlatformKey
85+
);
86+
7187
// any of these fields can be used to match the search value
7288
const keys = ['title', 'name', 'aliases', 'sdk', 'keywords'];
73-
const matches_ = matchSorter(platformsAndGuides, searchValue, {
89+
let matches_ = matchSorter(platformsAndGuides, searchValue, {
7490
keys,
7591
threshold: matchSorter.rankings.ACRONYM,
7692
});
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-
);
93+
94+
// For virtual guides (like javascript-platform), ensure they're always included
95+
// when they're the current selection, to prevent focus/display issues
8296
if (selectedPlatform && !matches_.includes(selectedPlatform)) {
83-
matches_.push(selectedPlatform);
97+
matches_ = [selectedPlatform, ...matches_];
8498
}
99+
85100
return matches_;
86101
}, [searchValue, currentPlatformKey, platformsAndGuides]);
87102

@@ -161,7 +176,7 @@ export function PlatformSelector({
161176
open={open}
162177
setOpen={setOpen}
163178
includesBaseElement={false}
164-
setValue={setSearchValue}
179+
setValue={handleSearchValueChange}
165180
>
166181
<RadixSelect.Trigger aria-label="Platform" className={styles.select}>
167182
<RadixSelect.Value placeholder="Choose your SDK" />

src/docTree.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -377,6 +377,10 @@ function extractGuides(platformNode: DocNode): PlatformGuide[] {
377377
? {
378378
...nodeToGuide(platformNode.slug, platformNode),
379379
key: `${platformNode.slug}-platform`,
380+
// Enhance searchable properties for virtual guides
381+
title: platformNode.frontmatter.title || platformNode.frontmatter.platformTitle,
382+
aliases: [...(platformNode.frontmatter.aliases || []), platformNode.slug],
383+
keywords: [...(platformNode.frontmatter.keywords || []), 'browser', 'client'],
380384
}
381385
: undefined;
382386

0 commit comments

Comments
 (0)