mirror of
https://github.com/pezkuwichain/pezkuwi-apps.git
synced 2026-11-13 02:07:06 +00:00
50 lines
1.4 KiB
TypeScript
50 lines
1.4 KiB
TypeScript
// Copyright 2017-2026 @pezkuwi/app-addresses authors & contributors
|
|
// SPDX-License-Identifier: Apache-2.0
|
|
|
|
import type { SortedAddress } from './types.js';
|
|
|
|
import FileSaver from 'file-saver';
|
|
import React, { useCallback } from 'react';
|
|
|
|
import { Button } from '@pezkuwi/react-components';
|
|
import { keyring } from '@pezkuwi/ui-keyring';
|
|
|
|
import { useTranslation } from '../translate.js';
|
|
|
|
interface Props {
|
|
sortedAddresses?: SortedAddress[]
|
|
}
|
|
|
|
function Export ({ sortedAddresses }: Props): React.ReactElement<Props> {
|
|
const { t } = useTranslation();
|
|
|
|
const onExport = useCallback(() => {
|
|
const accounts = sortedAddresses?.map(({ address, isFavorite }) => {
|
|
const account = keyring.getAddress(address); // get account info
|
|
|
|
return { address, isFavorite, name: account?.meta.name || address };
|
|
});
|
|
|
|
/** **************** Export accounts as JSON ******************/
|
|
|
|
const blob = new Blob([JSON.stringify(accounts, null, 2)], { type: 'application/json; charset=utf-8' });
|
|
|
|
// eslint-disable-next-line deprecation/deprecation
|
|
FileSaver.saveAs(blob, `batch_exported_address_book_${new Date().getTime()}.json`);
|
|
|
|
/** ********************* ************** ************************/
|
|
}, [sortedAddresses]);
|
|
|
|
return sortedAddresses?.length
|
|
? (
|
|
<Button
|
|
icon='file-export'
|
|
label={t('Export')}
|
|
onClick={onExport}
|
|
/>
|
|
)
|
|
: <></>;
|
|
}
|
|
|
|
export default React.memo(Export);
|