123456789101112131415161718192021222324252627282930313233343536373839404142434445464748 |
- import {useCallback, useMemo, useRef} from 'react';
- import type {SelectKey, SelectOption} from 'sentry/components/compactSelect';
- type Option = SelectOption<SelectKey>;
- type OptionCache = Map<SelectKey, Option>;
- export function useCompactSelectOptionsCache(options: Option[]): {
- clear: () => void;
- options: Option[];
- } {
- const cache = useRef<OptionCache>(new Map());
- const clearCache = useCallback(() => {
- cache.current.clear();
- }, []);
- const outgoingOptions = useMemo(() => {
- options.forEach(option => {
- cache.current.set(option.value, option);
- });
- return Array.from(cache.current.values()).sort(alphabeticalCompare);
- }, [options]);
- return {options: outgoingOptions, clear: clearCache};
- }
- type OptionComparator = (a: Option, b: Option) => number;
- const alphabeticalCompare: OptionComparator = (a, b) => {
- return a.value.toString().localeCompare(b.value.toString());
- };
|