Import and export of addresses to the My Accounts Tab (#11507)
* feat: Add ExportAll modal for batch exporting accounts * feat: Add ImportAll modal for batch importing accounts * feat: Update ExportAll modal to support checkbox selection for account types and improve validation in ImportAll modal
This commit is contained in:
parent
8e4e43b8dc
commit
9e01b5a3a8
@ -17,7 +17,9 @@ import { settings } from '@polkadot/ui-settings';
|
||||
import { BN_ZERO, isFunction } from '@polkadot/util';
|
||||
|
||||
import CreateModal from '../modals/Create.js';
|
||||
import ExportAll from '../modals/ExportAll.js';
|
||||
import ImportModal from '../modals/Import.js';
|
||||
import ImportAll from '../modals/ImportAll.js';
|
||||
import Ledger from '../modals/Ledger.js';
|
||||
import Local from '../modals/LocalAdd.js';
|
||||
import Multisig from '../modals/MultisigCreate.js';
|
||||
@ -104,6 +106,8 @@ function Overview ({ className = '', onStatusChange }: Props): React.ReactElemen
|
||||
const [isProxyOpen, toggleProxy] = useToggle();
|
||||
const [isLocalOpen, toggleLocal] = useToggle();
|
||||
const [isQrOpen, toggleQr] = useToggle();
|
||||
const [isExportAll, toggleExportAll] = useToggle();
|
||||
const [isImportAll, toggleImportAll] = useToggle();
|
||||
const [favorites, toggleFavorite] = useFavorites(STORE_FAVS);
|
||||
const [balances, setBalances] = useState<Balances>({ accounts: {} });
|
||||
const [filterOn, setFilter] = useState<string>('');
|
||||
@ -318,6 +322,19 @@ function Overview ({ className = '', onStatusChange }: Props): React.ReactElemen
|
||||
onStatusChange={onStatusChange}
|
||||
/>
|
||||
)}
|
||||
{isExportAll && (
|
||||
<ExportAll
|
||||
accountsByGroup={grouped}
|
||||
onClose={toggleExportAll}
|
||||
onStatusChange={onStatusChange}
|
||||
/>
|
||||
)}
|
||||
{isImportAll && (
|
||||
<ImportAll
|
||||
onClose={toggleImportAll}
|
||||
onStatusChange={onStatusChange}
|
||||
/>
|
||||
)}
|
||||
<BannerExtension />
|
||||
<BannerClaims />
|
||||
<Summary balance={balances.summary} />
|
||||
@ -360,6 +377,16 @@ function Overview ({ className = '', onStatusChange }: Props): React.ReactElemen
|
||||
/>
|
||||
</>
|
||||
)}
|
||||
<Button
|
||||
icon='file-import'
|
||||
label={t('Import')}
|
||||
onClick={toggleImportAll}
|
||||
/>
|
||||
<Button
|
||||
icon='file-export'
|
||||
label={t('Export')}
|
||||
onClick={toggleExportAll}
|
||||
/>
|
||||
<Button
|
||||
icon='qrcode'
|
||||
label={t('From Qr')}
|
||||
|
||||
138
packages/page-accounts/src/modals/ExportAll.tsx
Normal file
138
packages/page-accounts/src/modals/ExportAll.tsx
Normal file
@ -0,0 +1,138 @@
|
||||
// Copyright 2017-2025 @polkadot/app-accounts authors & contributors
|
||||
// SPDX-License-Identifier: Apache-2.0
|
||||
|
||||
import type { ActionStatus } from '@polkadot/react-components/Status/types';
|
||||
import type { ModalProps } from '../types.js';
|
||||
|
||||
import FileSaver from 'file-saver';
|
||||
import React, { useCallback, useState } from 'react';
|
||||
|
||||
import { Button, Checkbox, MarkWarning, Modal, styled } from '@polkadot/react-components';
|
||||
import { keyring } from '@polkadot/ui-keyring';
|
||||
import { nextTick } from '@polkadot/util';
|
||||
|
||||
import { useTranslation } from '../translate.js';
|
||||
|
||||
interface Props extends ModalProps {
|
||||
className?: string;
|
||||
onClose: () => void;
|
||||
accountsByGroup: Record<string, string[]>;
|
||||
onStatusChange: (status: ActionStatus) => void;
|
||||
}
|
||||
|
||||
function ExportAll ({ accountsByGroup, className, onClose, onStatusChange }: Props): React.ReactElement | null {
|
||||
const { t } = useTranslation();
|
||||
const [isBusy, setIsBusy] = useState(false);
|
||||
const [selectedTypes, setSelectedTypes] = useState<string[]>([]);
|
||||
|
||||
// toggle checkboxes
|
||||
const onChangeCheckBox = useCallback((group: string) => {
|
||||
if (!selectedTypes.includes(group)) {
|
||||
setSelectedTypes([...selectedTypes, group]);
|
||||
} else {
|
||||
setSelectedTypes(selectedTypes.filter((prev) => prev !== group));
|
||||
}
|
||||
}, [selectedTypes]);
|
||||
|
||||
const _onExportButtonClick = useCallback(() => {
|
||||
setIsBusy(true);
|
||||
|
||||
nextTick((): void => {
|
||||
const status: Partial<ActionStatus> = { action: 'export' };
|
||||
|
||||
try {
|
||||
let allAccounts: string[] = [];
|
||||
|
||||
// get all accounts for selected groups
|
||||
selectedTypes.forEach((group) => {
|
||||
allAccounts = [...allAccounts, ...accountsByGroup[group]];
|
||||
});
|
||||
|
||||
const accounts = allAccounts.map((account) => {
|
||||
const keyringAccount = keyring.getPair(account);
|
||||
|
||||
return {
|
||||
address: account,
|
||||
meta: keyringAccount.meta
|
||||
};
|
||||
});
|
||||
|
||||
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_accounts_${new Date().getTime()}.json`);
|
||||
|
||||
status.status = 'success';
|
||||
status.message = t('accounts exported');
|
||||
} catch (error) {
|
||||
status.status = 'error';
|
||||
status.message = (error as Error).message;
|
||||
console.error(error);
|
||||
}
|
||||
|
||||
setIsBusy(false);
|
||||
onStatusChange(status as ActionStatus);
|
||||
|
||||
if (status.status !== 'error') {
|
||||
onClose();
|
||||
}
|
||||
});
|
||||
}, [accountsByGroup, onClose, onStatusChange, selectedTypes, t]);
|
||||
|
||||
return (
|
||||
<Modal
|
||||
className={className}
|
||||
header={t('export all accounts')}
|
||||
onClose={onClose}
|
||||
size='large'
|
||||
>
|
||||
<Modal.Content>
|
||||
<MarkWarning
|
||||
content={t('You can export all your accounts metadata for the selected types')}
|
||||
withIcon={false}
|
||||
/>
|
||||
<StyledCheckBoxGroup>
|
||||
{Object.keys(accountsByGroup)
|
||||
.filter((group) => accountsByGroup[group].length > 0)
|
||||
.map((group) => {
|
||||
const isSelected = selectedTypes.includes(group);
|
||||
|
||||
return (
|
||||
<Checkbox
|
||||
key={group}
|
||||
label={`${group} (${accountsByGroup[group].length})`}
|
||||
// eslint-disable-next-line react/jsx-no-bind
|
||||
onChange={() => onChangeCheckBox(group)}
|
||||
value={isSelected}
|
||||
/>
|
||||
);
|
||||
})}
|
||||
</StyledCheckBoxGroup>
|
||||
</Modal.Content>
|
||||
<Modal.Actions>
|
||||
<Button
|
||||
icon='sync'
|
||||
isBusy={isBusy}
|
||||
isDisabled={!selectedTypes.length}
|
||||
label={t('Export')}
|
||||
onClick={_onExportButtonClick}
|
||||
/>
|
||||
</Modal.Actions>
|
||||
</Modal>
|
||||
);
|
||||
}
|
||||
|
||||
const StyledCheckBoxGroup = styled.div`
|
||||
display: flex;
|
||||
align-items: center;
|
||||
gap: 1rem;
|
||||
margin-top: 1.5rem;
|
||||
padding-inline: 2rem;
|
||||
|
||||
label, .ui--Icon {
|
||||
font-size: 1rem;
|
||||
font-weight: 500;
|
||||
}
|
||||
`;
|
||||
|
||||
export default React.memo(ExportAll);
|
||||
142
packages/page-accounts/src/modals/ImportAll.tsx
Normal file
142
packages/page-accounts/src/modals/ImportAll.tsx
Normal file
@ -0,0 +1,142 @@
|
||||
// Copyright 2017-2025 @polkadot/app-accounts authors & contributors
|
||||
// SPDX-License-Identifier: Apache-2.0
|
||||
|
||||
import type { Dispatch, SetStateAction } from 'react';
|
||||
import type { KeyringPair$Meta } from '@polkadot/keyring/types';
|
||||
import type { ActionStatus } from '@polkadot/react-components/Status/types';
|
||||
import type { ModalProps } from '../types.js';
|
||||
|
||||
import React, { useCallback, useState } from 'react';
|
||||
|
||||
import { Button, InputFile, MarkError, Modal } from '@polkadot/react-components';
|
||||
import keyring from '@polkadot/ui-keyring';
|
||||
import { nextTick, u8aToString } from '@polkadot/util';
|
||||
|
||||
import { useTranslation } from '../translate.js';
|
||||
|
||||
interface Props extends ModalProps {
|
||||
className?: string;
|
||||
onClose: () => void;
|
||||
onStatusChange: (status: ActionStatus) => void;
|
||||
}
|
||||
|
||||
interface FileAccount {
|
||||
address: string;
|
||||
meta: KeyringPair$Meta;
|
||||
}
|
||||
|
||||
type File = FileAccount[]
|
||||
|
||||
const acceptedFormats = ['application/json'];
|
||||
|
||||
// Check if file is valid
|
||||
function isValidFile (fileContent: string, setError: Dispatch<SetStateAction<string | null>>): boolean {
|
||||
setError(null);
|
||||
|
||||
try {
|
||||
const content = JSON.parse(fileContent) as File;
|
||||
|
||||
if (!Array.isArray(content)) {
|
||||
throw new Error('Invalid format: File content should be an array');
|
||||
}
|
||||
|
||||
for (const item of content) {
|
||||
if (
|
||||
typeof item.address !== 'string' ||
|
||||
typeof item.meta !== 'object' ||
|
||||
item.meta === null
|
||||
) {
|
||||
throw new Error('Invalid format: Each item must have a string "address" and an object "meta"');
|
||||
}
|
||||
}
|
||||
|
||||
return true;
|
||||
} catch (error) {
|
||||
console.error(error);
|
||||
setError((error as Error).message ? (error as Error).message : (error as Error).toString());
|
||||
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
function ImportAll ({ className, onClose, onStatusChange }: Props): React.ReactElement | null {
|
||||
const { t } = useTranslation();
|
||||
const [isBusy, setIsBusy] = useState(false);
|
||||
const [error, setError] = useState<string | null>(null);
|
||||
const [file, setFile] = useState<File | null>(null);
|
||||
|
||||
const _onChangeFile = useCallback(
|
||||
(file: Uint8Array) => {
|
||||
const fileContent = u8aToString(file);
|
||||
|
||||
setFile(isValidFile(fileContent, setError) ? JSON.parse(fileContent) as File : null);
|
||||
},
|
||||
[]
|
||||
);
|
||||
|
||||
const _onImportButtonClick = useCallback(() => {
|
||||
setIsBusy(true);
|
||||
nextTick((): void => {
|
||||
const status: Partial<ActionStatus> = { action: 'import' };
|
||||
|
||||
try {
|
||||
file?.map((pair) =>
|
||||
keyring.addExternal(pair.address, pair.meta)
|
||||
);
|
||||
|
||||
status.status = 'success';
|
||||
status.message = t('all accounts imported');
|
||||
} catch (error) {
|
||||
status.status = 'error';
|
||||
status.message = (error as Error).message;
|
||||
console.error(error);
|
||||
}
|
||||
|
||||
setIsBusy(false);
|
||||
onStatusChange(status as ActionStatus);
|
||||
|
||||
if (status.status !== 'error') {
|
||||
onClose();
|
||||
}
|
||||
});
|
||||
}, [file, onClose, onStatusChange, t]);
|
||||
|
||||
return (
|
||||
<Modal
|
||||
className={className}
|
||||
header={t('import all accounts')}
|
||||
onClose={onClose}
|
||||
size='large'
|
||||
>
|
||||
<Modal.Content>
|
||||
<Modal.Columns hint={t('Supply a backed-up JSON file')}>
|
||||
<InputFile
|
||||
accept={acceptedFormats}
|
||||
className='full'
|
||||
isError={!file}
|
||||
label={t('backup file')}
|
||||
onChange={_onChangeFile}
|
||||
withLabel
|
||||
/>
|
||||
</Modal.Columns>
|
||||
<Modal.Columns>
|
||||
{error && (
|
||||
<MarkError content={error} />
|
||||
)}
|
||||
</Modal.Columns>
|
||||
</Modal.Content>
|
||||
<Modal.Actions>
|
||||
<Button
|
||||
icon='sync'
|
||||
isBusy={isBusy}
|
||||
isDisabled={!file || !!error}
|
||||
label={t('Import')}
|
||||
onClick={_onImportButtonClick}
|
||||
/>
|
||||
</Modal.Actions>
|
||||
|
||||
</Modal>
|
||||
);
|
||||
}
|
||||
|
||||
export default React.memo(ImportAll);
|
||||
Loading…
Reference in New Issue
Block a user