Skip to content
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
30 changes: 28 additions & 2 deletions packages/web/app/src/components/target/explorer/filter.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import React, { ChangeEvent, useCallback, useMemo } from 'react';
import React, { ChangeEvent, useCallback, useMemo, useState, useDeferredValue } from 'react';
import { FilterIcon } from 'lucide-react';
import { useQuery } from 'urql';
import { Button } from '@/components/ui/button';
Expand Down Expand Up @@ -94,6 +94,8 @@ export function TypeFilter(props: {
};
}) {
const router = useRouter();
const [inputValue, setInputValue] = useState('');
const deferredInputValue = useDeferredValue(inputValue);
const [query] = useQuery({
query: TypeFilter_AllTypes,
variables: {
Expand All @@ -115,6 +117,29 @@ export function TypeFilter(props: {
[allNamedTypes],
);

const sortedTypes = useMemo(() => {
if (!deferredInputValue) return types;

const search = deferredInputValue.toLowerCase();
return [...types].sort((a, b) => {
const aName = a.label.toLowerCase();
const bName = b.label.toLowerCase();

// Exact match gets highest priority
const aExact = aName === search;
const bExact = bName === search;
if (aExact !== bExact) return aExact ? -1 : 1;

// Prefix match gets second priority
const aPrefix = aName.startsWith(search);
const bPrefix = bName.startsWith(search);
if (aPrefix !== bPrefix) return aPrefix ? -1 : 1;

// Alphabetical within same relevance
return aName.localeCompare(bName);
});
}, [types, deferredInputValue]);

const onChange = useCallback(
(option: SelectOption | null) => {
void router.navigate({
Expand All @@ -140,8 +165,9 @@ export function TypeFilter(props: {
className="min-w-[200px] grow cursor-text"
placeholder="Search for a type"
defaultValue={defaultValue}
options={types}
options={sortedTypes}
onChange={onChange}
onInputChange={setInputValue}
loading={query.fetching}
/>
);
Expand Down
6 changes: 6 additions & 0 deletions packages/web/app/src/components/v2/autocomplete.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,7 @@ export function Autocomplete(props: {
disabled?: boolean;
loading?: boolean;
className?: string;
onInputChange?: (value: string) => void;
}): ReactElement {
return (
<Select
Expand All @@ -125,6 +126,11 @@ export function Autocomplete(props: {
isClearable
closeMenuOnSelect
onChange={option => props.onChange(option as SelectOption)}
onInputChange={(value, { action }) => {
if (action === 'input-change') {
props.onInputChange?.(value);
}
}}
isDisabled={props.disabled}
isLoading={props.loading}
placeholder={props.placeholder}
Expand Down