mirror of
https://github.com/pezkuwichain/pezkuwi-apps.git
synced 2026-11-01 06:09:49 +00:00
53 lines
1.6 KiB
TypeScript
53 lines
1.6 KiB
TypeScript
// Copyright 2017-2026 @pezkuwi/react-hooks authors & contributors
|
|
// SPDX-License-Identifier: Apache-2.0
|
|
|
|
import type { Option } from '@pezkuwi/types';
|
|
import type { RegistrarInfo } from '@pezkuwi/types/interfaces';
|
|
import type { Registrar } from './types.js';
|
|
|
|
import { useMemo } from 'react';
|
|
|
|
import { createNamedHook } from './createNamedHook.js';
|
|
import { useAccounts } from './useAccounts.js';
|
|
import { useApi } from './useApi.js';
|
|
import { useCall } from './useCall.js';
|
|
|
|
interface RegistrarNull {
|
|
address: string | null;
|
|
index: number;
|
|
}
|
|
|
|
interface State {
|
|
isRegistrar: boolean;
|
|
registrars: Registrar[];
|
|
skipQuery?: boolean;
|
|
}
|
|
|
|
function useRegistrarsImpl (skipQuery?: boolean): State {
|
|
const { apiIdentity } = useApi();
|
|
const { allAccounts, hasAccounts } = useAccounts();
|
|
const query = useCall<Option<RegistrarInfo>[]>(!skipQuery && apiIdentity.query.identity?.registrars);
|
|
|
|
// determine if we have a registrar or not - registrars are allowed to approve
|
|
return useMemo(
|
|
(): State => {
|
|
const registrars = (query || [])
|
|
.map((registrar, index): RegistrarNull => ({
|
|
address: registrar.isSome
|
|
? registrar.unwrap().account.toString()
|
|
: null,
|
|
index
|
|
}))
|
|
.filter((registrar): registrar is Registrar => !!registrar.address);
|
|
|
|
return {
|
|
isRegistrar: hasAccounts && registrars.some(({ address }) => allAccounts.includes(address)),
|
|
registrars
|
|
};
|
|
},
|
|
[allAccounts, hasAccounts, query]
|
|
);
|
|
}
|
|
|
|
export const useRegistrars = createNamedHook('useRegistrars', useRegistrarsImpl);
|