aboutsummaryrefslogtreecommitdiffstats
path: root/apps/web/lib/hooks
diff options
context:
space:
mode:
authorMohamed Bassem <me@mbassem.com>2024-12-28 12:30:24 +0000
committerMohamed Bassem <me@mbassem.com>2024-12-28 12:30:24 +0000
commit7956e9fa6772ab57a0794fb7cba7e9d9c0bd7878 (patch)
tree10d6e47afed8be007361cd40ec8b05fc52d6d4d9 /apps/web/lib/hooks
parent7dd5b2bc8751911f86448e6c4619b2583d9a4b53 (diff)
downloadkarakeep-7956e9fa6772ab57a0794fb7cba7e9d9c0bd7878.tar.zst
feat: Implement the all highlights page. Fixes #620
Diffstat (limited to 'apps/web/lib/hooks')
-rw-r--r--apps/web/lib/hooks/relative-time.ts22
1 files changed, 22 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;
+}