import React, { useState, useMemo, useCallback } from 'react'; import { makeStyles } from '@material-ui/core/styles'; import compactDateString from './compactDateString'; interface PropTypes { value: Date | string; } const useStyles = makeStyles({ root: { display: 'block', cursor: 'pointer' } }); const DATE_FORMAT = { month: 'long', day: 'numeric', year: 'numeric', hour: '2-digit', minute: '2-digit' }; const DateString: React.FC = ({ value }) => { const [isCompact, setIsCompact] = useState(true); const classes = useStyles(); const toggleScompact = useCallback(() => setIsCompact(v => !v), [setIsCompact]); const date = useMemo(() => new Date(value), [value]); const formatted = useMemo(() => date.toLocaleString('default', DATE_FORMAT), [date]); const compact = useMemo(() => compactDateString(date), [date]); return (
{isCompact ? compact : formatted}
); }; export default DateString;