ghost-portal/packages/react-query/src/TimeNow.tsx
Jaco eacf028ba6
Use .js imports in src (#9132)
* Use .js imports in src

* Additional

* Additional

* Additional

* Additional

* Additional

* Additional

* Additional

* Additional

* Additional

* Additional

* Additional

* Additional

* Additional

* Add WP extension aliases

* Bump dev

* Additional

* Additional

* Adjust extensionAlias order
2023-03-09 12:11:03 +02:00

38 lines
962 B
TypeScript

// Copyright 2017-2023 @polkadot/react-query authors & contributors
// SPDX-License-Identifier: Apache-2.0
import type { Moment } from '@polkadot/types/interfaces';
import React, { useMemo } from 'react';
import { useApi, useCall } from '@polkadot/react-hooks';
import Elapsed from './Elapsed.js';
interface Props {
children?: React.ReactNode;
className?: string;
label?: React.ReactNode;
value?: Moment;
}
function TimeNow ({ children, className = '', label, value }: Props): React.ReactElement<Props> {
const { api } = useApi();
const timestamp = useCall<Moment>(!value && api.query.timestamp?.now);
const [now, hasValue] = useMemo(
() => [value || timestamp, !!(value || timestamp)],
[timestamp, value]
);
return (
<div className={`${className} ${hasValue ? '' : '--tmp'}`}>
{label || ''}
<Elapsed value={hasValue ? now : Date.now()} />
{children}
</div>
);
}
export default React.memo(TimeNow);