Add basic whitelist support (#7942)

This commit is contained in:
Jaco 2022-07-30 18:54:02 +02:00 committed by GitHub
parent c20e79c579
commit 809b8f1b03
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
6 changed files with 164 additions and 3 deletions

View File

@ -37,6 +37,7 @@ import teleport from './teleport';
import transfer from './transfer';
import treasury from './treasury';
import utilities from './utilities';
import whitelist from './whitelist';
export default function create (t: TFunction): Routes {
return [
@ -56,6 +57,7 @@ export default function create (t: TFunction): Routes {
techcomm(t),
membership(t),
alliance(t),
whitelist(t),
parachains(t),
gilt(t),
assets(t),

View File

@ -0,0 +1,22 @@
// Copyright 2017-2022 @polkadot/apps-routing authors & contributors
// SPDX-License-Identifier: Apache-2.0
import type { TFunction } from 'i18next';
import type { Route } from './types';
import Component from '@polkadot/app-whitelist';
export default function create (t: TFunction): Route {
return {
Component,
display: {
needsApi: [
'tx.whitelist.removeWhitelistedCall'
]
},
group: 'governance',
icon: 'list-check',
name: 'whitelist',
text: t('nav.whitelist', 'Whitelist', { ns: 'apps-routing' })
};
}

View File

@ -0,0 +1,23 @@
// Copyright 2017-2022 @polkadot/app-whitelist authors & contributors
// SPDX-License-Identifier: Apache-2.0
import type { HexString } from '@polkadot/util/types';
import React from 'react';
interface Props {
className?: string;
value: HexString;
}
function Hash ({ className, value }: Props): React.ReactElement<Props> {
return (
<tr className={ className }>
<td>
{value}
</td>
</tr>
);
}
export default React.memo(Hash);

View File

@ -0,0 +1,46 @@
// Copyright 2017-2022 @polkadot/app-whitelist authors & contributors
// SPDX-License-Identifier: Apache-2.0
import type { SubmittableExtrinsicFunction } from '@polkadot/api/types';
import React, { useRef } from 'react';
import { Table } from '@polkadot/react-components';
import Hash from './Hash';
import { useTranslation } from './translate';
import useHashes from './useHashes';
interface Props {
className?: string;
defaultPropose?: SubmittableExtrinsicFunction<'promise'>;
filter?: (section: string, method?: string) => boolean;
}
function Hashes ({ className }: Props): React.ReactElement<Props> {
const { t } = useTranslation();
const hashes = useHashes();
const headerRef = useRef([
[t('hashes'), 'start']
]);
return (
<div className={className}>
<Table
className={className}
empty={hashes && t<string>('No call hashes found')}
header={headerRef.current}
>
{hashes && hashes.map((h) => (
<Hash
key={h}
value={h}
/>
))}
</Table>
</div>
);
}
export default React.memo(Hashes);

View File

@ -1,16 +1,37 @@
// Copyright 2017-2022 @polkadot/app-whitelist authors & contributors
// SPDX-License-Identifier: Apache-2.0
import React from 'react';
import React, { useRef } from 'react';
import { Tabs } from '@polkadot/react-components';
import Hashes from './Hashes';
import { useTranslation } from './translate';
interface Props {
basePath: string;
className?: string;
}
function App ({ className }: Props): React.ReactElement<Props> {
function App ({ basePath, className }: Props): React.ReactElement<Props> {
const { t } = useTranslation();
const tabsRef = useRef([
{
isRoot: true,
name: 'overview',
text: t<string>('Overview')
}
]);
return (
<div className={className} />
<main className={className}>
<Tabs
basePath={basePath}
items={tabsRef.current}
/>
<Hashes />
</main>
);
}

View File

@ -0,0 +1,47 @@
// Copyright 2017-2022 @polkadot/app-preimages authors & contributors
// SPDX-License-Identifier: Apache-2.0
import type { Changes } from '@polkadot/react-hooks/useEventChanges';
import type { StorageKey } from '@polkadot/types';
import type { EventRecord, Hash } from '@polkadot/types/interfaces';
import type { HexString } from '@polkadot/util/types';
import { useMemo } from 'react';
import { createNamedHook, useApi, useEventChanges, useMapKeys } from '@polkadot/react-hooks';
const OPT_HASH = {
transform: (keys: StorageKey<[Hash]>[]): Hash[] =>
keys.map(({ args: [hash] }) => hash)
};
function filter (records: EventRecord[]): Changes<Hash> {
const added: Hash[] = [];
const removed: Hash[] = [];
records.forEach(({ event: { data: [hash], method } }): void => {
if (method === 'CallWhitelisted') {
added.push(hash as Hash);
} else {
removed.push(hash as Hash);
}
});
return { added, removed };
}
function useHashesImpl (): HexString[] | undefined {
const { api } = useApi();
const startValue = useMapKeys(api.query.whitelist.whitelistedCall, OPT_HASH);
const hashes = useEventChanges([
api.events.whitelist.CallWhitelisted,
api.events.whitelist.WhitelistedCallRemoved
], filter, startValue);
return useMemo(
() => hashes && hashes.map((h) => h.toHex()),
[hashes]
);
}
export default createNamedHook('useHashes', useHashesImpl);