ghost-portal/packages/react-hooks/src/useCoreDescriptor.ts
Daria 4df5c9a8d5
Relay Chain - Coretime Overview UI Tab (#11016)
* exposing coretime wss endpoint to use on relay chain, adjusted hooks to accept api endpoint as a parameter

* adjusting hook calls to new parameters

* added Tag for coretime types, renamed occupancyType to coretimeType

* added cycle progress and cycle end date to summary, fixed the estimation dates

* coretime page draft, added hook for gathering all the coretime information in order to show it on relay chain

* lint

* bumped version

* updated coretime chain summary

* updated summary, added timeslice for the first sale start,

* hook updates to make sure the coretime data is displayed even if workloads are empty, fallback on coreAssignments

* removed unused files

* lint

* tsc

* merging parachainIds with renewalIds

* if there is no record in potential renewals, then the chain has renewed already

* added config for page-coretime

* removed hardcoded regionLength value

* improved function to find out type

* lint

* tsc

* Fix versioning

* fix comments

---------

Co-authored-by: tarikgul <tariksnow37@gmail.com>
2024-10-22 09:38:00 -04:00

71 lines
3.0 KiB
TypeScript

// Copyright 2017-2024 @polkadot/react-hooks authors & contributors
// SPDX-License-Identifier: Apache-2.0
import type { ApiPromise } from '@polkadot/api';
import type { StorageKey, u32 } from '@polkadot/types';
import type { PalletBrokerCoretimeInterfaceCoreAssignment, PolkadotRuntimeParachainsAssignerCoretimeAssignmentState, PolkadotRuntimeParachainsAssignerCoretimeCoreDescriptor, PolkadotRuntimeParachainsAssignerCoretimeQueueDescriptor, PolkadotRuntimeParachainsAssignerCoretimeWorkState } from '@polkadot/types/lookup';
import type { CoreDescriptor } from './types.js';
import { useEffect, useState } from 'react';
import { createNamedHook, useCall, useMapKeys } from '@polkadot/react-hooks';
import { BN } from '@polkadot/util';
function extractInfo (info: PolkadotRuntimeParachainsAssignerCoretimeCoreDescriptor, core: number): CoreDescriptor {
const currentWork: PolkadotRuntimeParachainsAssignerCoretimeWorkState | null = info?.currentWork.isSome ? info.currentWork.unwrap() : null;
const queue: PolkadotRuntimeParachainsAssignerCoretimeQueueDescriptor | null = info?.queue.isSome ? info.queue.unwrap() : null;
const assignments = currentWork?.assignments || [];
return {
core,
info: {
currentWork: {
assignments: assignments?.map((one: [PalletBrokerCoretimeInterfaceCoreAssignment, PolkadotRuntimeParachainsAssignerCoretimeAssignmentState]) => {
return ({
isPool: one[0]?.isPool,
isTask: one[0]?.isTask,
ratio: one[1]?.ratio.toNumber(),
remaining: one[1]?.remaining.toNumber(),
task: one[0]?.isTask ? one[0]?.asTask.toString() : one[0]?.isPool ? 'Pool' : 'Idle'
});
}),
endHint: currentWork?.endHint.isSome ? currentWork?.endHint?.unwrap().toBn() : null,
pos: currentWork?.pos.toNumber() || 0,
step: currentWork?.step.toNumber() || 0
},
queue: {
first: queue?.first.toBn() || new BN(0),
last: queue?.last.toBn() || new BN(0)
}
}
};
}
const OPT_KEY = {
transform: (keys: StorageKey<[u32]>[]): u32[] =>
keys.map(({ args: [id] }) => id)
};
function useCoreDescriptorImpl (api: ApiPromise, ready: boolean): CoreDescriptor[] | undefined {
const keys = useMapKeys(ready && api.query.coretimeAssignmentProvider.coreDescriptors, [], OPT_KEY);
const sanitizedKeys = keys?.map((_, index) => {
return index;
});
sanitizedKeys?.pop();
const coreDescriptors = useCall<[[number[]], PolkadotRuntimeParachainsAssignerCoretimeCoreDescriptor[]]>(ready && api.query.coretimeAssignmentProvider.coreDescriptors.multi, [sanitizedKeys], { withParams: true });
const [state, setState] = useState<CoreDescriptor[] | undefined>();
useEffect((): void => {
coreDescriptors &&
setState(coreDescriptors[0][0].map((info, index) => extractInfo(coreDescriptors[1][index], info))
);
}, [coreDescriptors]);
return state;
}
export const useCoreDescriptor = createNamedHook('useCoreDescriptor', useCoreDescriptorImpl);