refactor(client): parameterize history restore by version id

This commit is contained in:
Philipinho
2026-08-11 21:03:30 +01:00
parent a83eb5fea4
commit ea4a138c13
4 changed files with 69 additions and 43 deletions
@@ -159,7 +159,7 @@ function HistoryList({ pageId }: Props) {
> >
{t("Cancel")} {t("Cancel")}
</Button> </Button>
<Button size="compact-md" onClick={confirmRestore}> <Button size="compact-md" onClick={() => confirmRestore()}>
{t("Restore")} {t("Restore")}
</Button> </Button>
</Group> </Group>
@@ -166,7 +166,7 @@ export default function HistoryModalMobile({ pageId, pageTitle }: Props) {
<Button variant="default" onClick={() => setHistoryModalOpen(false)}> <Button variant="default" onClick={() => setHistoryModalOpen(false)}>
{t("Cancel")} {t("Cancel")}
</Button> </Button>
<Button onClick={confirmRestore}>{t("Restore")}</Button> <Button onClick={() => confirmRestore()}>{t("Restore")}</Button>
</Group> </Group>
)} )}
@@ -1,4 +1,4 @@
import { useAtom, useAtomValue, useSetAtom } from "jotai"; import { useAtomValue, useSetAtom } from "jotai";
import { useCallback } from "react"; import { useCallback } from "react";
import { useTranslation } from "react-i18next"; import { useTranslation } from "react-i18next";
import { Text } from "@mantine/core"; import { Text } from "@mantine/core";
@@ -9,7 +9,8 @@ import {
activeHistoryIdAtom, activeHistoryIdAtom,
historyAtoms, historyAtoms,
} from "@/features/page-history/atoms/history-atoms"; } from "@/features/page-history/atoms/history-atoms";
import { usePageHistoryQuery } from "@/features/page-history/queries/page-history-query"; import { fetchPageHistory } from "@/features/page-history/queries/page-history-query";
import { IPageHistory } from "@/features/page-history/types/page.types";
import { import {
pageEditorAtom, pageEditorAtom,
titleEditorAtom, titleEditorAtom,
@@ -25,8 +26,6 @@ export function useHistoryRestore() {
const { t } = useTranslation(); const { t } = useTranslation();
const activeHistoryId = useAtomValue(activeHistoryIdAtom); const activeHistoryId = useAtomValue(activeHistoryIdAtom);
const { data: activeHistoryData } = usePageHistoryQuery(activeHistoryId);
const mainEditor = useAtomValue(pageEditorAtom); const mainEditor = useAtomValue(pageEditorAtom);
const mainEditorTitle = useAtomValue(titleEditorAtom); const mainEditorTitle = useAtomValue(titleEditorAtom);
const setHistoryModalOpen = useSetAtom(historyAtoms); const setHistoryModalOpen = useSetAtom(historyAtoms);
@@ -40,8 +39,19 @@ export function useHistoryRestore() {
SpaceCaslSubject.Page, SpaceCaslSubject.Page,
); );
const handleRestore = useCallback(() => { const handleRestore = useCallback(
if (!activeHistoryData) return; async (historyId: string) => {
let historyData: IPageHistory;
try {
historyData = await fetchPageHistory(historyId);
} catch {
notifications.show({
message: t("Error fetching page data."),
color: "red",
});
return;
}
if ( if (
!mainEditor || !mainEditor ||
mainEditor.isDestroyed || mainEditor.isDestroyed ||
@@ -54,20 +64,26 @@ export function useHistoryRestore() {
mainEditorTitle mainEditorTitle
.chain() .chain()
.clearContent() .clearContent()
.setContent(activeHistoryData.title, { emitUpdate: true }) .setContent(historyData.title, { emitUpdate: true })
.run(); .run();
mainEditor mainEditor
.chain() .chain()
.clearContent() .clearContent()
.setContent(activeHistoryData.content) .setContent(historyData.content)
.run(); .run();
setHistoryModalOpen(false); setHistoryModalOpen(false);
notifications.show({ message: t("Successfully restored") }); notifications.show({ message: t("Successfully restored") });
}, [activeHistoryData, mainEditor, mainEditorTitle, setHistoryModalOpen, t]); },
[mainEditor, mainEditorTitle, setHistoryModalOpen, t],
);
const confirmRestore = useCallback(
(historyId?: string) => {
const targetId = historyId ?? activeHistoryId;
if (!targetId) return;
const confirmRestore = useCallback(() => {
modals.openConfirmModal({ modals.openConfirmModal({
title: t("Please confirm your action"), title: t("Please confirm your action"),
children: ( children: (
@@ -78,9 +94,11 @@ export function useHistoryRestore() {
</Text> </Text>
), ),
labels: { confirm: t("Confirm"), cancel: t("Cancel") }, labels: { confirm: t("Confirm"), cancel: t("Cancel") },
onConfirm: handleRestore, onConfirm: () => handleRestore(targetId),
}); });
}, [t, handleRestore]); },
[t, handleRestore, activeHistoryId],
);
return { canRestore, confirmRestore }; return { canRestore, confirmRestore };
} }
@@ -23,6 +23,14 @@ export function prefetchPageHistory(historyId: string) {
}); });
} }
export function fetchPageHistory(historyId: string): Promise<IPageHistory> {
return queryClient.fetchQuery({
queryKey: ["page-history", historyId],
queryFn: () => getPageHistoryById(historyId),
staleTime: HISTORY_STALE_TIME,
});
}
export function usePageHistoryListQuery( export function usePageHistoryListQuery(
pageId: string, pageId: string,
): UseInfiniteQueryResult<InfiniteData<IPagination<IPageHistory>, unknown>> { ): UseInfiniteQueryResult<InfiniteData<IPagination<IPageHistory>, unknown>> {