ghost-portal/packages/page-broker/src/BrokerContext.tsx
Daria 468a3d5107
Feedback/#11253 (#11304)
* countdown component

* filtering by parachain id in workplan and workload

* lint

* rename chainName to relayName, fixed subscan block link

* always showing price even if the cores are sold out

* moved some calculations to the coretime context

* added broker context for page-broker, fix for wrong dates

* showing time with the estimated date in places

* removed extra code when calculating renewal status

* tsc for page-broker

* page-coretime tsc

* fix showing correct dates for fixed price phase in sale history

* minified workinforow as all rows show the same information now

* default values for fn input

* showing start price of the sale on the sale tab

* replaced custom Countdown element with existing implementation

* adjusted estimateTime function for broker

* adjusted estimateTime function for coretime page

* lint

* added Renewal link for RegionX
2025-04-04 12:01:20 +13:00

84 lines
3.0 KiB
TypeScript

// Copyright 2017-2025 @polkadot/app-broker authors & contributors
// SPDX-License-Identifier: Apache-2.0
import type { ReactNode } from 'react';
import type { ApiPromise } from '@polkadot/api';
import type { BrokerStatus, ChainConstants, PalletBrokerConfigRecord, PalletBrokerSaleInfoRecord } from '@polkadot/react-hooks/types';
import type { CurrentRegion } from './types.js';
import React, { createContext, useContext, useMemo } from 'react';
import { useBrokerConfig, useBrokerSalesInfo, useBrokerStatus, useCoretimeConsts } from '@polkadot/react-hooks';
import { estimateTime } from './utils.js';
interface BrokerProviderProps {
children: ReactNode;
api: ApiPromise;
isApiReady: boolean;
}
interface BrokerContextProps {
config: PalletBrokerConfigRecord | null,
coretimeConsts: ChainConstants | null,
currentRegion: CurrentRegion,
saleInfo: PalletBrokerSaleInfoRecord | null,
status: BrokerStatus | null,
}
const initialState = {
config: null,
coretimeConsts: null,
currentRegion: {
begin: null,
beginDate: null,
end: null,
endDate: null
},
saleInfo: null,
status: null
};
export const BrokerContext = createContext<BrokerContextProps>(initialState);
export const BrokerProvider = ({ api, children, isApiReady }: BrokerProviderProps) => {
const coretimeConsts = useCoretimeConsts();
const config = useBrokerConfig(api, isApiReady);
const saleInfo = useBrokerSalesInfo(api, isApiReady);
const status = useBrokerStatus(api, isApiReady);
const currentRegionEnd = useMemo(() => saleInfo?.regionBegin, [saleInfo]);
const currentRegionBegin = useMemo(() => saleInfo && config && saleInfo?.regionBegin - config?.regionLength, [saleInfo, config]);
const lastBlock = useMemo(() => status && coretimeConsts && status.lastTimeslice * coretimeConsts.relay.blocksPerTimeslice, [status, coretimeConsts]);
const currentRegionEndDate = useMemo(() => currentRegionEnd && lastBlock && estimateTime(Number(currentRegionEnd), lastBlock)?.formattedDate, [currentRegionEnd, lastBlock]);
const currentRegionBeginDate = useMemo(() => currentRegionBegin && lastBlock && estimateTime(Number(currentRegionBegin), lastBlock)?.formattedDate, [currentRegionBegin, lastBlock]);
const value = useMemo(() => {
if (!config || !saleInfo || !status || !currentRegionBegin || !currentRegionBeginDate || !currentRegionEnd || !currentRegionEndDate || !coretimeConsts) {
return initialState;
}
return ({
config,
coretimeConsts,
currentRegion: {
begin: currentRegionBegin,
beginDate: currentRegionBeginDate,
end: currentRegionEnd,
endDate: currentRegionEndDate
},
saleInfo,
status
});
}, [currentRegionBegin, currentRegionEnd, currentRegionBeginDate, currentRegionEndDate, config, saleInfo, status, coretimeConsts]);
return (
<BrokerContext.Provider value={value}>
{children}
</BrokerContext.Provider>
);
};
export const useBrokerContext = () => useContext(BrokerContext);