mirror of
https://github.com/luxfi/explore.git
synced 2026-09-24 00:28:18 +00:00
93 lines
3.0 KiB
TypeScript
93 lines
3.0 KiB
TypeScript
import React from 'react';
|
|
|
|
import type { ExternalChain } from 'types/externalChains';
|
|
|
|
import useIsInitialLoading from 'lib/hooks/useIsInitialLoading';
|
|
import useIsMobile from 'lib/hooks/useIsMobile';
|
|
import type { SelectOption, SelectProps, ViewMode } from '@luxfi/ui/select';
|
|
import { createListCollection, Select } from '@luxfi/ui/select';
|
|
import { FilterInput } from 'toolkit/components/filters/FilterInput';
|
|
import { Separator } from '@luxfi/ui/separator';
|
|
import IconSvg from 'ui/shared/IconSvg';
|
|
|
|
import ChainIcon from './ChainIcon';
|
|
|
|
const ALL_OPTION: SelectOption = {
|
|
value: 'all',
|
|
label: 'All chains',
|
|
icon: <IconSvg name="pie_chart" className="size-5"/>,
|
|
afterElement: <Separator orientation="horizontal"/>,
|
|
};
|
|
|
|
export interface Props extends Omit<SelectProps, 'collection' | 'placeholder'> {
|
|
loading?: boolean;
|
|
mode?: ViewMode;
|
|
chainsConfig: Array<Omit<ExternalChain, 'explorer_url'>>;
|
|
chainIds?: Array<string>;
|
|
withAllOption?: boolean;
|
|
}
|
|
|
|
const ChainSelect = ({ loading, mode, chainsConfig, chainIds, withAllOption, ...props }: Props) => {
|
|
|
|
const [ inputValue, setInputValue ] = React.useState('');
|
|
|
|
const isInitialLoading = useIsInitialLoading(loading);
|
|
const isMobile = useIsMobile();
|
|
|
|
const allItems = React.useMemo(() => {
|
|
const chainItems = chainsConfig
|
|
.filter((chain) => !chainIds || isInitialLoading || chainIds.includes(chain.id))
|
|
.map((chain) => ({
|
|
value: chain.id,
|
|
label: chain.name || `Chain ${ chain.id }`,
|
|
icon: <ChainIcon data={ chain } alt={ `${ chain.name } logo` } borderRadius="none" noTooltip/>,
|
|
})) || [];
|
|
|
|
return [ withAllOption ? ALL_OPTION : undefined, ...chainItems ].filter(Boolean);
|
|
}, [ chainsConfig, chainIds, withAllOption, isInitialLoading ]);
|
|
|
|
const collection = React.useMemo(() => {
|
|
return createListCollection<SelectOption>({ items: allItems });
|
|
}, [ allItems ]);
|
|
|
|
const handleFilterChange = React.useCallback((value: string) => {
|
|
setInputValue(value);
|
|
}, [ ]);
|
|
|
|
const itemFilter = React.useCallback((item: SelectOption) => {
|
|
return item.label.toLowerCase().includes(inputValue.toLowerCase());
|
|
}, [ inputValue ]);
|
|
|
|
const contentHeader = allItems.length > 10 ? (
|
|
<div className="px-4 pt-4 pb-2 bg-[var(--color-popover-bg)] sticky top-0 z-[1]">
|
|
<FilterInput
|
|
placeholder="Find chain"
|
|
initialValue={ inputValue }
|
|
onChange={ handleFilterChange }
|
|
/>
|
|
</div>
|
|
) : null;
|
|
|
|
if (allItems.length === 0) {
|
|
return null;
|
|
}
|
|
|
|
return (
|
|
<Select
|
|
collection={ collection }
|
|
defaultValue={ allItems.length > 0 ? [ allItems[0].value ] : undefined }
|
|
placeholder="Select chain"
|
|
loading={ isInitialLoading }
|
|
mode={ isMobile && !mode ? 'compact' : mode }
|
|
w="fit-content"
|
|
className="shrink-0"
|
|
contentHeader={ contentHeader }
|
|
contentProps={ contentHeader ? { style: { paddingTop: 0 } } : undefined }
|
|
itemFilter={ itemFilter }
|
|
{ ...props }
|
|
/>
|
|
);
|
|
};
|
|
|
|
export default React.memo(ChainSelect);
|