* time ago hook
This commit is contained in:
parent
5fd477d074
commit
7fe3c5f177
3 changed files with 21 additions and 2 deletions
|
|
@ -209,6 +209,7 @@ export default function PageEditor({
|
||||||
queryClient.setQueryData(["pages", slugId], {
|
queryClient.setQueryData(["pages", slugId], {
|
||||||
...pageData,
|
...pageData,
|
||||||
content: newContent,
|
content: newContent,
|
||||||
|
updatedAt: new Date(),
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
}, 3000);
|
}, 3000);
|
||||||
|
|
|
||||||
|
|
@ -12,7 +12,7 @@ import {
|
||||||
IconTrash,
|
IconTrash,
|
||||||
IconWifiOff,
|
IconWifiOff,
|
||||||
} from "@tabler/icons-react";
|
} from "@tabler/icons-react";
|
||||||
import React from "react";
|
import React, { useEffect } from "react";
|
||||||
import useToggleAside from "@/hooks/use-toggle-aside.tsx";
|
import useToggleAside from "@/hooks/use-toggle-aside.tsx";
|
||||||
import { useAtom } from "jotai";
|
import { useAtom } from "jotai";
|
||||||
import { historyAtoms } from "@/features/page-history/atoms/history-atoms.ts";
|
import { historyAtoms } from "@/features/page-history/atoms/history-atoms.ts";
|
||||||
|
|
@ -34,6 +34,7 @@ import {
|
||||||
} from "@/features/editor/atoms/editor-atoms.ts";
|
} from "@/features/editor/atoms/editor-atoms.ts";
|
||||||
import { formattedDate, timeAgo } from "@/lib/time.ts";
|
import { formattedDate, timeAgo } from "@/lib/time.ts";
|
||||||
import MovePageModal from "@/features/page/components/move-page-modal.tsx";
|
import MovePageModal from "@/features/page/components/move-page-modal.tsx";
|
||||||
|
import { useTimeAgo } from "@/hooks/use-time-ago.tsx";
|
||||||
|
|
||||||
interface PageHeaderMenuProps {
|
interface PageHeaderMenuProps {
|
||||||
readOnly?: boolean;
|
readOnly?: boolean;
|
||||||
|
|
@ -102,6 +103,7 @@ function PageActionMenu({ readOnly }: PageActionMenuProps) {
|
||||||
{ open: openMovePageModal, close: closeMoveSpaceModal },
|
{ open: openMovePageModal, close: closeMoveSpaceModal },
|
||||||
] = useDisclosure(false);
|
] = useDisclosure(false);
|
||||||
const [pageEditor] = useAtom(pageEditorAtom);
|
const [pageEditor] = useAtom(pageEditorAtom);
|
||||||
|
const pageUpdatedAt = useTimeAgo(page.updatedAt);
|
||||||
|
|
||||||
const handleCopyLink = () => {
|
const handleCopyLink = () => {
|
||||||
const pageUrl =
|
const pageUrl =
|
||||||
|
|
@ -208,7 +210,7 @@ function PageActionMenu({ readOnly }: PageActionMenuProps) {
|
||||||
<Tooltip
|
<Tooltip
|
||||||
label={t("Edited by {{name}} {{time}}", {
|
label={t("Edited by {{name}} {{time}}", {
|
||||||
name: page.lastUpdatedBy.name,
|
name: page.lastUpdatedBy.name,
|
||||||
time: timeAgo(page.updatedAt),
|
time: pageUpdatedAt,
|
||||||
})}
|
})}
|
||||||
position="left-start"
|
position="left-start"
|
||||||
>
|
>
|
||||||
|
|
|
||||||
16
apps/client/src/hooks/use-time-ago.tsx
Normal file
16
apps/client/src/hooks/use-time-ago.tsx
Normal file
|
|
@ -0,0 +1,16 @@
|
||||||
|
import { timeAgo } from "@/lib/time.ts";
|
||||||
|
import { useEffect, useState } from "react";
|
||||||
|
|
||||||
|
export function useTimeAgo(date: Date | string) {
|
||||||
|
const [value, setValue] = useState(() => timeAgo(new Date(date)));
|
||||||
|
|
||||||
|
useEffect(() => {
|
||||||
|
const interval = setInterval(() => {
|
||||||
|
setValue(timeAgo(new Date(date)));
|
||||||
|
}, 5 * 1000);
|
||||||
|
|
||||||
|
return () => clearInterval(interval);
|
||||||
|
}, [date]);
|
||||||
|
|
||||||
|
return value;
|
||||||
|
}
|
||||||
Loading…
Add table
Reference in a new issue