import { Check, CheckBoxOutlineBlankOutlined, CheckBoxOutlined, IndeterminateCheckBoxOutlined, } from '@mui/icons-material' import { Avatar, IconButton, ListItemAvatar, Tooltip, debounce, useTheme, } from '@mui/material' import type { ToolsResponse } from '@openocean.finance/widget-sdk' import type { MouseEventHandler } from 'react' import { type ChangeEventHandler, useEffect, useMemo, useState } from 'react' import { useTranslation } from 'react-i18next' import { useNavigate } from 'react-router-dom' import { shallow } from 'zustand/shallow' import { FullPageContainer } from '../components/FullPageContainer.js' import { ListItemText } from '../components/ListItemText.js' import { StickySearchInput } from '../components/Search/SearchInput.js' import { SearchList } from '../components/Search/SearchInput.style.js' import { SearchNotFound } from '../components/Search/SearchNotFound.js' import { SettingsListItemButton } from '../components/SettingsListItemButton.js' import { useDefaultElementId } from '../hooks/useDefaultElementId.js' import { useHeader } from '../hooks/useHeader.js' import { useScrollableContainer } from '../hooks/useScrollableContainer.js' import { useTools } from '../hooks/useTools.js' import { useWidgetConfig } from '../providers/WidgetProvider/WidgetProvider.js' import { useSettingsActions } from '../stores/settings/useSettingsActions.js' import { useSettingsStore } from '../stores/settings/useSettingsStore.js' import { navigationRoutes } from '../utils/navigationRoutes.js' interface SelectAllCheckboxProps { allCheckboxesSelected: boolean onClick: MouseEventHandler anyCheckboxesSelected: boolean noCheckboxesAvailable: boolean } const SelectAllCheckbox: React.FC = ({ allCheckboxesSelected, anyCheckboxesSelected, noCheckboxesAvailable, onClick, }) => { const { t } = useTranslation() const theme = useTheme() const tooltipTitle = noCheckboxesAvailable ? undefined : allCheckboxesSelected ? t('tooltip.deselectAll') : t('tooltip.selectAll') return ( {allCheckboxesSelected ? ( ) : anyCheckboxesSelected ? ( ) : ( )} ) } type ToolCollectionTypes = ToolsResponse['exchanges'] | ToolsResponse['bridges'] export const SelectEnabledToolsPage: React.FC<{ type: 'Bridges' | 'Exchanges' }> = ({ type }) => { const typeKey = type.toLowerCase() as 'bridges' | 'exchanges' const { bridges } = useWidgetConfig() const navigate = useNavigate() const { tools } = useTools() const { setToolValue, toggleToolKeys } = useSettingsActions() const [enabledTools, disabledTools] = useSettingsStore( (state) => [state[`_enabled${type}`], state[`disabled${type}`]], shallow ) const { t } = useTranslation() const elementId = useDefaultElementId() const scrollableContainer = useScrollableContainer(elementId) const [filteredTools, setFilteredTools] = useState( tools?.[typeKey] ?? [] ) useEffect(() => { if (type === 'Bridges' && bridges?.showSettings === false) { navigate(`/${navigationRoutes.settings}`) } }, [bridges?.showSettings, navigate, type]) const headerAction = useMemo( () => ( !disabledTools.includes(tool.key)) } anyCheckboxesSelected={ !!filteredTools.length && filteredTools.some((tool) => disabledTools.includes(tool.key)) } noCheckboxesAvailable={!filteredTools.length} onClick={() => toggleToolKeys( type, filteredTools.map((tool) => tool.key) ) } /> ), [disabledTools, toggleToolKeys, type, filteredTools] ) useHeader(t(`settings.enabled${type}`), headerAction) const handleClick = (key: string) => { setToolValue(type, key, !enabledTools[key]) } const handleSearchInputChange: ChangeEventHandler< HTMLInputElement | HTMLTextAreaElement > = (e) => { const value = e.target.value if (!value) { setFilteredTools(tools?.[typeKey] ?? []) } else { setFilteredTools( (tools?.[typeKey] ? tools[typeKey].filter((tool) => tool.name.toLowerCase().includes(value.toLowerCase()) ) : []) as ToolCollectionTypes ) } if (scrollableContainer) { scrollableContainer.scrollTop = 0 } } const debouncedSearchInputChange = debounce(handleSearchInputChange, 250) return ( {filteredTools.length ? ( {filteredTools.map((tool) => ( handleClick(tool.key)} > {tool.name[0]} {enabledTools[tool.key] && ( )} ))} ) : ( )} ) }