39 lines
1.1 KiB
TypeScript
39 lines
1.1 KiB
TypeScript
// Copyright 2017-2022 @polkadot/app-democracy authors & contributors
|
|
// SPDX-License-Identifier: Apache-2.0
|
|
|
|
import type { AccountId } from '@polkadot/types/interfaces';
|
|
import type { CollectiveType } from './types';
|
|
|
|
import { useMemo } from 'react';
|
|
|
|
import { createNamedHook } from './createNamedHook';
|
|
import { useAccounts } from './useAccounts';
|
|
import { useApi } from './useApi';
|
|
import { useCall } from './useCall';
|
|
|
|
interface Result {
|
|
isMember: boolean;
|
|
members: string[];
|
|
}
|
|
|
|
const OPT = {
|
|
transform: (accounts: AccountId[]) =>
|
|
accounts.map((accountId) => accountId.toString())
|
|
};
|
|
|
|
function useCollectiveMembersImpl (collective: CollectiveType): Result {
|
|
const { api } = useApi();
|
|
const { allAccounts, hasAccounts } = useAccounts();
|
|
const retrieved = useCall<string[]>(hasAccounts && api.derive[collective]?.members, undefined, OPT);
|
|
|
|
return useMemo(
|
|
() => ({
|
|
isMember: (retrieved || []).some((accountId) => allAccounts.includes(accountId)),
|
|
members: (retrieved || [])
|
|
}),
|
|
[allAccounts, retrieved]
|
|
);
|
|
}
|
|
|
|
export const useCollectiveMembers = createNamedHook('useCollectiveMembers', useCollectiveMembersImpl);
|