Allow selection of origin for track (when multiples) (#8473)

This commit is contained in:
Jaco 2022-12-04 20:40:49 +02:00 committed by GitHub
parent dd589fc7e3
commit 5a1c3823ca
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 53 additions and 8 deletions

View File

@ -5,10 +5,12 @@ import type { ApiPromise } from '@polkadot/api';
import { KUSAMA_GENESIS } from '../constants';
type Origin = { system: string } | { Origins: string };
interface TrackInfo {
id: number;
name: string;
origin: { system: string } | { Origins: string };
origin: Origin | Origin[];
text?: string;
}
@ -36,7 +38,10 @@ const KUSAMA: Record<string, TrackInfo[]> = {
{
id: 3,
name: 'fellows',
origin: { Origins: 'Fellowship3Dan' },
origin: [
{ Origins: 'Fellowship3Dan' },
{ Origins: 'Fellows' }
],
text: 'Origin commanded by Polkadot Fellows (3rd Dan fellows or greater)'
},
{
@ -48,7 +53,10 @@ const KUSAMA: Record<string, TrackInfo[]> = {
{
id: 5,
name: 'experts',
origin: { Origins: 'Fellowship5Dan' },
origin: [
{ Origins: 'Fellowship5Dan' },
{ Origins: 'FellowshipExperts' }
],
text: 'Origin commanded by Polkadot Experts (5th Dan fellows or greater)'
},
{
@ -60,7 +68,10 @@ const KUSAMA: Record<string, TrackInfo[]> = {
{
id: 7,
name: 'masters',
origin: { Origins: 'Fellowship7Dan' },
origin: [
{ Origins: 'Fellowship7Dan' },
{ Origins: 'FellowshipMasters' }
],
text: 'Origin commanded by Polkadot Masters (7th Dan fellows of greater)'
},
{

View File

@ -110,6 +110,23 @@ function Submit ({ className = '', isMember, members, palletReferenda, tracks }:
[api, palletReferenda]
);
const originOptions = useMemo(
() => trackInfo && Array.isArray(trackInfo.origin)
? trackInfo.origin.map((records, index) => ({
text: Object.values(records)[0],
value: index + 1
}))
: null,
[trackInfo]
);
const selectedOrigin = useMemo(
() => !trackInfo?.origin || Array.isArray(trackInfo?.origin)
? origin
: trackInfo.origin,
[origin, trackInfo]
);
const trackOpts = useMemo(
() => tracks && tracks.map(([id, track]) => ({
text: getTrackName(id, track),
@ -118,6 +135,15 @@ function Submit ({ className = '', isMember, members, palletReferenda, tracks }:
[tracks]
);
const _onChangeOriginMulti = useCallback(
(value: number) => setOrigin(
trackInfo && Array.isArray(trackInfo.origin)
? trackInfo.origin[value - 1]
: null
),
[trackInfo]
);
const _onChangeOrigin = useCallback(
([{ isValid, value }]: RawParam[]) =>
setOrigin(isValid ? value : null),
@ -192,6 +218,14 @@ function Submit ({ className = '', isMember, members, palletReferenda, tracks }:
params={originType}
/>
)}
{originOptions && (
<Dropdown
defaultValue={originOptions[0].value}
label={t<string>('track origin')}
onChange={_onChangeOriginMulti}
options={originOptions}
/>
)}
</Modal.Columns>
{bestNumber && defaultAtAfter && (
<Modal.Columns hint={t<string>('The moment of enactment, either at a specific block, or after a specific block. Currently at #{{bestNumber}}, the selected block should be after the current best.', { replace: { bestNumber: formatNumber(bestNumber) } })}>
@ -217,10 +251,10 @@ function Submit ({ className = '', isMember, members, palletReferenda, tracks }:
<TxButton
accountId={accountId}
icon='plus'
isDisabled={!(trackInfo?.origin || origin) || !atAfter || !isHashValid || !accountId || isInvalidAt || !preimage?.proposalHash}
isDisabled={!selectedOrigin || !atAfter || !isHashValid || !accountId || isInvalidAt || !preimage?.proposalHash}
label={t<string>('Submit proposal')}
onStart={toggleSubmit}
params={[trackInfo?.origin || origin, { Lookup: { hash: preimage?.proposalHash, len: preimage?.proposalLength } }, atAfter]}
params={[selectedOrigin, { Lookup: { hash: preimage?.proposalHash, len: preimage?.proposalLength } }, atAfter]}
tx={api.tx[palletReferenda as 'referenda'].submit}
/>
</Modal.Actions>

View File

@ -20,8 +20,8 @@ export function getTrackName (trackId: BN, { name }: PalletReferendaTrackInfo):
}`;
}
export function getTrackInfo (api: ApiPromise, specName: string, palletReferenda: string, tracks?: [BN, PalletReferendaTrackInfo][], trackId?: number): { origin: Record<string, string>, text?: string } | undefined {
let info: { origin: Record<string, string>, text?: string } | undefined;
export function getTrackInfo (api: ApiPromise, specName: string, palletReferenda: string, tracks?: [BN, PalletReferendaTrackInfo][], trackId?: number): { origin: Record<string, string> | Record<string, string>[], text?: string } | undefined {
let info: { origin: Record<string, string> | Record<string, string>[], text?: string } | undefined;
if (tracks && trackId !== undefined) {
const originMap = getGovernanceTracks(api, specName, palletReferenda);