Adjust alliance summary (+ both way ipfs encode tests) (#7923)

This commit is contained in:
Jaco 2022-07-28 10:08:06 +02:00 committed by GitHub
parent d10c9dcafa
commit 5989defc69
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
7 changed files with 139 additions and 30 deletions

View File

@ -1,17 +1,42 @@
// Copyright 2017-2022 @polkadot/app-alliance authors & contributors
// SPDX-License-Identifier: Apache-2.0
import type { Member as MemberType, Rule } from '../types';
import React from 'react';
import { SummaryBox } from '@polkadot/react-components';
import { CardSummary, Spinner, SummaryBox } from '@polkadot/react-components';
import { formatNumber } from '@polkadot/util';
import { useTranslation } from '../translate';
interface Props {
className?: string;
members?: MemberType[];
rule?: Rule;
}
function Summary ({ className }: Props): React.ReactElement<Props> {
function Summary ({ className, members, rule }: Props): React.ReactElement<Props> {
const { t } = useTranslation();
// TODO Turn rule into IPFS link
return (
<SummaryBox className={className} />
<SummaryBox className={className}>
<CardSummary label={t<string>('rule')}>
{rule
? rule.hasRule
? t<string>('yes')
: t<string>('no')
: <Spinner />
}
</CardSummary>
<CardSummary label={t<string>('members')}>
{members
? formatNumber(members.length)
: <Spinner />
}
</CardSummary>
</SummaryBox>
);
}

View File

@ -1,7 +1,7 @@
// Copyright 2017-2022 @polkadot/app-alliance authors & contributors
// SPDX-License-Identifier: Apache-2.0
import type { Member as MemberType, Unscrupelous } from '../types';
import type { Member as MemberType, Rule, Unscrupelous } from '../types';
import React from 'react';
@ -16,16 +16,20 @@ import Summary from './Summary';
interface Props {
className?: string;
members?: MemberType[];
rule?: Rule;
unscrupelous?: Unscrupelous;
}
function Overview ({ className, members, unscrupelous }: Props): React.ReactElement<Props> {
function Overview ({ className, members, rule, unscrupelous }: Props): React.ReactElement<Props> {
const { t } = useTranslation();
const [isJoinOpen, toggleJoin] = useToggle();
return (
<div className={className}>
<Summary />
<Summary
members={members}
rule={rule}
/>
<Button.Group>
<Button
icon='add'

View File

@ -12,6 +12,7 @@ import { useTranslation } from './translate';
import Unscrupelous from './Unscrupelous';
import useAnnoucements from './useAnnoucements';
import useMembers from './useMembers';
import useRule from './useRule';
import useUnscrupelous from './useUnscrupelous';
interface Props {
@ -23,6 +24,7 @@ function AllianceApp ({ basePath, className }: Props): React.ReactElement<Props>
const { t } = useTranslation();
const accouncements = useAnnoucements();
const members = useMembers();
const rule = useRule();
const unscrupelous = useUnscrupelous();
const itemsRef = useRef([
@ -57,6 +59,7 @@ function AllianceApp ({ basePath, className }: Props): React.ReactElement<Props>
<Route>
<Overview
members={members}
rule={rule}
unscrupelous={unscrupelous}
/>
</Route>

View File

@ -4,6 +4,12 @@
import type { PalletAllianceCid, PalletAllianceMemberRole } from '@polkadot/types/lookup';
import type { BN } from '@polkadot/util';
export interface Cid {
cid: PalletAllianceCid;
ipfs: string | null;
key: string;
}
export interface Member {
accountId: string;
role: PalletAllianceMemberRole['type'];
@ -15,13 +21,12 @@ export interface MemberInfo {
isUpForKicking: boolean;
}
export interface Rule {
cid: Cid | null;
hasRule: boolean;
}
export interface Unscrupelous {
accounts: string[];
websites: string[];
}
export interface Cid {
cid: PalletAllianceCid;
ipfs: string;
key: string;
}

View File

@ -3,23 +3,23 @@
import type { Option } from '@polkadot/types';
import type { PalletAllianceCid } from '@polkadot/types/lookup';
import type { Cid } from './types';
import type { Rule } from './types';
import { createNamedHook, useApi, useCall } from '@polkadot/react-hooks';
import { createCid } from './util';
const OPT_RULE = {
transform: (opt: Option<PalletAllianceCid>): Cid | null =>
transform: (opt: Option<PalletAllianceCid>): Rule =>
opt.isSome
? createCid(opt.unwrap())
: null
? { cid: createCid(opt.unwrap()), hasRule: true }
: { cid: null, hasRule: false }
};
function useRuleImpl (): Cid | null | undefined {
function useRuleImpl (): Rule | undefined {
const { api } = useApi();
return useCall<Cid | null>(api.query.alliance.rule, [], OPT_RULE);
return useCall<Rule>(api.query.alliance.rule, [], OPT_RULE);
}
export default createNamedHook('useRule', useRuleImpl);

View File

@ -0,0 +1,46 @@
// Copyright 2017-2022 @polkadot/app-alliance authors & contributors
// SPDX-License-Identifier: Apache-2.0
import { Metadata, TypeRegistry } from '@polkadot/types';
import substrate from '@polkadot/types-support/metadata/static-substrate';
import { createIpfsHash, createPalletCid } from './util';
const registry = new TypeRegistry();
const metadata = new Metadata(registry, substrate);
registry.setMetadata(metadata);
describe('util', (): void => {
describe('createPalletCid', (): void => {
it('encodes an ipfs hash to a pallet CID', (): void => {
expect(
createPalletCid(registry, 'QmTx8GZrQGwQ1ZLquZ4yPqSukDdkvKwpYCULsfLo6G47TX')?.toHuman()
).toEqual({
codec: 0x70.toString(),
hash_: {
code: 0x12.toString(),
digest: '0x5360ecbd380c10f43bc0a6aba27556580011a5e833592df8dcf330c94759e862'
},
version: 'V0'
});
});
});
describe('createIpfsHash', (): void => {
it('encodes a pallet CID into an ipfs hash', (): void => {
expect(
createIpfsHash(
registry.createType('PalletAllianceCid', {
codec: 0x70,
hash_: {
code: 0x12,
digest: '0x5360ecbd380c10f43bc0a6aba27556580011a5e833592df8dcf330c94759e862'
},
version: 'V0'
})
)
).toEqual('QmTx8GZrQGwQ1ZLquZ4yPqSukDdkvKwpYCULsfLo6G47TX');
});
});
});

View File

@ -2,35 +2,61 @@
// SPDX-License-Identifier: Apache-2.0
import type { PalletAllianceCid } from '@polkadot/types/lookup';
import type { Registry } from '@polkadot/types/types';
import type { Cid } from './types';
import { CID } from 'multiformats/cid';
import { CID, digest, varint } from 'multiformats';
import { u8aToBn } from '@polkadot/util';
import { u8aToHex } from '@polkadot/util';
function createIpfs (cid: PalletAllianceCid): string {
export function createIpfsHash (cid: PalletAllianceCid): string | null {
try {
const { codec, hash_: { code, digest }, version } = cid;
const { codec, hash_: { code, digest: _bytes }, version } = cid;
// Since we use parse, encode into a fully-specified bytes to
// pass - <varint code> + <varint length> + bytes
const bytes = _bytes.toU8a(true);
const codeLen = varint.encodingLength(code.toNumber());
const sizeLen = varint.encodingLength(bytes.length);
const encoded = new Uint8Array(codeLen + sizeLen + bytes.length);
varint.encodeTo(code.toNumber(), encoded, 0);
varint.encodeTo(bytes.length, encoded, codeLen);
encoded.set(bytes, codeLen + sizeLen);
return CID
.create(version.index as 0, codec.toNumber(), {
bytes: digest.subarray(4),
code: code.toNumber(),
digest: digest.toU8a(true),
size: u8aToBn(digest.subarray(0, 4)).toNumber()
})
.create(version.index as 0, codec.toNumber(), digest.decode(encoded))
.toString();
} catch (error) {
console.error(`createIpfs: ${(error as Error).message}::`, cid.toHuman());
return '';
return null;
}
}
export function createPalletCid (registry: Registry, cid: string): PalletAllianceCid | null {
try {
const { code: codec, multihash: { code, digest }, version } = CID.parse(cid);
return registry.createType('PalletAllianceCid', {
codec,
hash: {
code,
digest: u8aToHex(digest)
},
version
});
} catch (error) {
console.error(`encodeCid: ${(error as Error).message}::`, cid);
return null;
}
}
export function createCid (cid: PalletAllianceCid): Cid {
return {
cid,
ipfs: createIpfs(cid),
ipfs: createIpfsHash(cid),
key: cid.toHex()
};
}