diff options
Diffstat (limited to 'apps/web/lib')
| -rw-r--r-- | apps/web/lib/hooks/relative-time.ts | 22 | ||||
| -rw-r--r-- | apps/web/lib/i18n/locales/en/translation.json | 1 |
2 files changed, 23 insertions, 0 deletions
diff --git a/apps/web/lib/hooks/relative-time.ts b/apps/web/lib/hooks/relative-time.ts new file mode 100644 index 00000000..f7c38497 --- /dev/null +++ b/apps/web/lib/hooks/relative-time.ts @@ -0,0 +1,22 @@ +import { useEffect, useState } from "react"; +import dayjs from "dayjs"; +import relativeTime from "dayjs/plugin/relativeTime"; + +dayjs.extend(relativeTime); + +export default function useRelativeTime(date: Date) { + const [state, setState] = useState({ + fromNow: "", + localCreatedAt: "", + }); + + // This is to avoid hydration errors when server and clients are in different timezones + useEffect(() => { + setState({ + fromNow: dayjs(date).fromNow(), + localCreatedAt: date.toLocaleString(), + }); + }, [date]); + + return state; +} diff --git a/apps/web/lib/i18n/locales/en/translation.json b/apps/web/lib/i18n/locales/en/translation.json index 9caad519..2812df88 100644 --- a/apps/web/lib/i18n/locales/en/translation.json +++ b/apps/web/lib/i18n/locales/en/translation.json @@ -20,6 +20,7 @@ "note": "Note", "attachments": "Attachments", "highlights": "Highlights", + "source": "Source", "screenshot": "Screenshot", "video": "Video", "archive": "Archive", |
