* Added dir .idea to .gitignore * Resolve #95: Added date format modes exact/relativepull/98/head
parent
68dc87b743
commit
bc77dcdfbf
@ -0,0 +1,32 @@ |
||||
import React from "react"; |
||||
import { Text } from "grommet"; |
||||
import dayjs from "dayjs"; |
||||
import { DateFormat, useDateFormatMode } from "../../hooks/dateFormatSwitcherHook"; |
||||
import { RelativeTimer } from "./RelativeTimer"; |
||||
|
||||
interface IRelativeTimer { |
||||
date: number | string | Date; |
||||
} |
||||
|
||||
export function DateTime(props: IRelativeTimer) { |
||||
const { date } = props; |
||||
|
||||
const dateFormat = useDateFormatMode(); |
||||
|
||||
if (dateFormat === DateFormat.RELATIVE) { |
||||
return <RelativeTimer date={date} /> |
||||
} |
||||
|
||||
const formattedDate = dayjs(date).format("MM/DD/YYYY, HH:mm:ss"); |
||||
|
||||
return ( |
||||
<Text |
||||
size="small" |
||||
style={{ minWidth: "125px" }} |
||||
color="minorText" |
||||
title={formattedDate} |
||||
> |
||||
{formattedDate} |
||||
</Text> |
||||
); |
||||
} |
@ -0,0 +1,36 @@ |
||||
import { useState } from "react"; |
||||
import { singletonHook } from "react-singleton-hook"; |
||||
|
||||
export enum DateFormat { |
||||
RELATIVE= 'relative', |
||||
EXACT = 'exact' |
||||
} |
||||
|
||||
const defaultDateFormat: DateFormat = DateFormat.EXACT; |
||||
|
||||
let globalSetMode = () => { |
||||
throw new Error("you must useDarkMode before setting its state"); |
||||
}; |
||||
|
||||
const ITEM_NAME = 'dateFormat'; |
||||
|
||||
export const setDateFormatMode = (mode: DateFormat) => { |
||||
//@ts-ignore
|
||||
globalSetMode(mode); |
||||
window.localStorage.setItem(ITEM_NAME, mode); |
||||
}; |
||||
|
||||
export const useDateFormatMode = singletonHook<DateFormat>( |
||||
(window.localStorage.getItem(ITEM_NAME) || defaultDateFormat) as DateFormat, |
||||
() => { |
||||
const currentDateFormat = |
||||
(window.localStorage.getItem(ITEM_NAME) as DateFormat) || defaultDateFormat; |
||||
|
||||
const [mode, setMode] = useState<DateFormat>(currentDateFormat); |
||||
//@ts-ignore
|
||||
globalSetMode = setMode; |
||||
return mode; |
||||
} |
||||
); |
||||
|
||||
|
Loading…
Reference in new issue