mirror of
https://github.com/billsonnn/nitro-react.git
synced 2024-11-27 08:00:51 +01:00
Add friendly time component
This commit is contained in:
parent
63f41afe58
commit
cf3a562064
34
src/views/shared/friendly-time/FriendlyTimeView.tsx
Normal file
34
src/views/shared/friendly-time/FriendlyTimeView.tsx
Normal file
@ -0,0 +1,34 @@
|
|||||||
|
import { FriendlyTime } from '@nitrots/nitro-renderer';
|
||||||
|
import { FC, useCallback, useEffect, useMemo, useState } from 'react';
|
||||||
|
import { FriendlyTimeViewProps } from './FriendlyTimeView.types';
|
||||||
|
|
||||||
|
export const FriendlyTimeView: FC<FriendlyTimeViewProps> = props =>
|
||||||
|
{
|
||||||
|
const { seconds = 0, isShort = false, ...rest } = props;
|
||||||
|
const [ updateId, setUpdateId ] = useState(-1);
|
||||||
|
|
||||||
|
const getStartSeconds = useMemo(() =>
|
||||||
|
{
|
||||||
|
return (Math.round(new Date().getSeconds()) - seconds);
|
||||||
|
}, [ seconds ]);
|
||||||
|
|
||||||
|
const getFriendlyTime = useCallback(() =>
|
||||||
|
{
|
||||||
|
const value = (Math.round(new Date().getSeconds()) - getStartSeconds);
|
||||||
|
|
||||||
|
if(isShort) return FriendlyTime.format(value);
|
||||||
|
|
||||||
|
return FriendlyTime.format(value);
|
||||||
|
}, [ getStartSeconds, isShort ]);
|
||||||
|
|
||||||
|
useEffect(() =>
|
||||||
|
{
|
||||||
|
const interval = setInterval(() => setUpdateId(prevValue => (prevValue + 1)), 10000);
|
||||||
|
|
||||||
|
return () => clearInterval(interval);
|
||||||
|
}, []);
|
||||||
|
|
||||||
|
return (
|
||||||
|
<div { ...rest }>{ getFriendlyTime() }</div>
|
||||||
|
);
|
||||||
|
}
|
7
src/views/shared/friendly-time/FriendlyTimeView.types.ts
Normal file
7
src/views/shared/friendly-time/FriendlyTimeView.types.ts
Normal file
@ -0,0 +1,7 @@
|
|||||||
|
import { DetailsHTMLAttributes } from 'react';
|
||||||
|
|
||||||
|
export interface FriendlyTimeViewProps extends DetailsHTMLAttributes<HTMLDivElement>
|
||||||
|
{
|
||||||
|
seconds: number;
|
||||||
|
isShort?: boolean;
|
||||||
|
}
|
Loading…
Reference in New Issue
Block a user