// Copyright 2017-2023 @polkadot/app-settings authors & contributors // SPDX-License-Identifier: Apache-2.0 import type { ChainInfo } from '../types'; import React, { useCallback, useMemo, useRef, useState } from 'react'; import styled from 'styled-components'; import { knownExtensions } from '@polkadot/apps-config'; import { externalEmptySVG } from '@polkadot/apps-config/ui/logos/external'; import { Button, Dropdown, Spinner, Table } from '@polkadot/react-components'; import { useToggle } from '@polkadot/react-hooks'; import { useTranslation } from '../translate'; import useExtensions from '../useExtensions'; import iconOption from './iconOption'; interface Props { chainInfo: ChainInfo | null; className?: string; } function Extensions ({ chainInfo, className }: Props): React.ReactElement { const { t } = useTranslation(); const { extensions } = useExtensions(); const [selectedIndex, setSelectedIndex] = useState(0); const [isBusy, toggleBusy] = useToggle(); const options = useMemo( () => (extensions || []).map(({ extension: { name, version } }, value) => iconOption(`${name} ${version}`, value, knownExtensions[name]?.ui.logo || externalEmptySVG) ), [extensions] ); const _updateMeta = useCallback( (): void => { if (chainInfo && extensions?.[selectedIndex]) { toggleBusy(); extensions[selectedIndex] .update(chainInfo) .catch(() => false) .then(() => toggleBusy()) .catch(console.error); } }, [chainInfo, extensions, selectedIndex, toggleBusy] ); const headerRef = useRef<[React.ReactNode?, string?, number?][]>([ [t('Extensions'), 'start'] ]); return ( ('No Upgradable extensions')} header={headerRef.current} > {extensions ? options.length !== 0 && ( <> ('upgradable extensions')} onChange={setSelectedIndex} options={options} value={selectedIndex} />