Skip to content

Commit

Permalink
chore: automatically update relative time
Browse files Browse the repository at this point in the history
  • Loading branch information
drodil committed Oct 16, 2023
1 parent 141765d commit 6d0dc62
Showing 1 changed file with 11 additions and 1 deletion.
Original file line number Diff line number Diff line change
@@ -1,14 +1,24 @@
import { Tooltip } from '@material-ui/core';
import React from 'react';
import React, { useEffect } from 'react';
// @ts-ignore
import RelativeTime from 'react-relative-time';

export const RelativeTimeWithTooltip = (props: { value: Date | string }) => {
const { value } = props;
let date = value;
const [updates, setUpdates] = React.useState(1);

useEffect(() => {
const interval = setInterval(() => {
setUpdates(updates === 1 ? 0 : 1);
}, 30000);
return () => clearInterval(interval);
}, [updates, setUpdates]);

if (typeof date === 'string' || date instanceof String) {
date = new Date(date);
}

return (
<Tooltip title={date.toLocaleString(navigator.languages)}>
<RelativeTime value={date} />
Expand Down

0 comments on commit 6d0dc62

Please sign in to comment.