@selfcommunity/react-ui
Version:
React UI Components to integrate a Community created with SelfCommunity Platform.
44 lines (43 loc) • 1.82 kB
JavaScript
import { __rest } from "tslib";
import { jsx as _jsx, jsxs as _jsxs } from "react/jsx-runtime";
import { styled } from '@mui/material/styles';
import { Box, Tooltip, Typography } from '@mui/material';
import Icon from '@mui/material/Icon';
import { FormattedRelativeTime, useIntl } from 'react-intl';
import classNames from 'classnames';
import { getRelativeTime } from '../../utils/formatRelativeTime';
const PREFIX = 'SCDateTimeAgo';
const classes = {
root: `${PREFIX}-root`
};
const Root = styled(Box, {
name: PREFIX,
slot: 'Root',
overridesResolver: (props, styles) => [styles.root]
})(() => ({
width: 'auto',
display: 'inline-flex',
alignItems: 'center',
marginRight: 3,
'& .MuiIcon-root': {
fontSize: '18px',
marginRight: 2
},
'& .MuiTypography-root': {
lineHeight: 1.8
}
}));
export default function DateTimeAgo(props) {
// PROPS
const { className, date = null, showStartIcon = true } = props, rest = __rest(props, ["className", "date", "showStartIcon"]);
// INTL
const intl = useIntl();
/**
* Renders root object (if date obj)
*/
if (date) {
const formattedDate = getRelativeTime(date);
return (_jsxs(Root, Object.assign({ component: "span", className: classNames(classes.root, className) }, rest, { children: [showStartIcon && _jsx(Icon, { children: "access_time" }), _jsx(Tooltip, Object.assign({ title: `${intl.formatDate(date, { year: 'numeric', month: 'numeric', day: 'numeric', hour: 'numeric', minute: 'numeric' })}`, enterTouchDelay: 0 }, { children: _jsx(Typography, Object.assign({ variant: 'body2', component: 'span' }, { children: _jsx(FormattedRelativeTime, { value: -formattedDate.value, unit: formattedDate.unit, numeric: "auto" }) })) }))] })));
}
return null;
}