diff --git a/apps/client/src/features/page-history/atoms/history-atoms.ts b/apps/client/src/features/page-history/atoms/history-atoms.ts index a20d40a3b..2acf163d5 100644 --- a/apps/client/src/features/page-history/atoms/history-atoms.ts +++ b/apps/client/src/features/page-history/atoms/history-atoms.ts @@ -3,3 +3,7 @@ import { atom } from "jotai"; export const historyAtoms = atom(false); export const activeHistoryIdAtom = atom(""); export const activeHistoryPrevIdAtom = atom(""); +export const highlightChangesAtom = atom(true); + +export type DiffCounts = { added: number; deleted: number; total: number }; +export const diffCountsAtom = atom(null); diff --git a/apps/client/src/features/page-history/components/history-editor.tsx b/apps/client/src/features/page-history/components/history-editor.tsx index 3d012c50f..5ef65c7fe 100644 --- a/apps/client/src/features/page-history/components/history-editor.tsx +++ b/apps/client/src/features/page-history/components/history-editor.tsx @@ -9,24 +9,26 @@ import historyClasses from "./history.module.css"; import { recreateTransform } from "@docmost/editor-ext"; import { DOMSerializer, Node } from "@tiptap/pm/model"; import { ChangeSet, simplifyChanges } from "prosemirror-changeset"; - -export type DiffCounts = { added: number; deleted: number; total: number }; +import { useAtom } from "jotai"; +import { + diffCountsAtom, + highlightChangesAtom, +} from "@/features/page-history/atoms/history-atoms"; export interface HistoryEditorProps { title: string; content: any; previousContent?: any; - highlightChanges?: boolean; - onDiffCalculated?: (counts: DiffCounts) => void; } export function HistoryEditor({ title, content, previousContent, - highlightChanges = true, - onDiffCalculated, }: HistoryEditorProps) { + const [highlightChanges] = useAtom(highlightChangesAtom); + const [, setDiffCounts] = useAtom(diffCountsAtom); + const editor = useEditor({ extensions: mainExtensions, editable: false, @@ -169,7 +171,8 @@ export function HistoryEditor({ } const total = addedCount + deletedCount; - onDiffCalculated?.({ added: addedCount, deleted: deletedCount, total }); + // @ts-ignore + setDiffCounts({ added: addedCount, deleted: deletedCount, total }); editor.setOptions({ editorProps: { @@ -178,7 +181,14 @@ export function HistoryEditor({ highlightChanges ? decorationSet : DecorationSet.empty, }, }); - }, [title, content, editor, previousContent, highlightChanges]); + }, [ + title, + content, + editor, + previousContent, + highlightChanges, + setDiffCounts, + ]); return (
diff --git a/apps/client/src/features/page-history/components/history-modal-body.tsx b/apps/client/src/features/page-history/components/history-modal-body.tsx index df8d322f1..0da39aebb 100644 --- a/apps/client/src/features/page-history/components/history-modal-body.tsx +++ b/apps/client/src/features/page-history/components/history-modal-body.tsx @@ -7,13 +7,14 @@ import { Switch, Text, } from "@mantine/core"; -import { DiffCounts } from "@/features/page-history/components/history-editor"; import HistoryList from "@/features/page-history/components/history-list"; import classes from "./history.module.css"; -import { useAtom } from "jotai"; +import { useAtom, useSetAtom } from "jotai"; import { activeHistoryIdAtom, activeHistoryPrevIdAtom, + diffCountsAtom, + highlightChangesAtom, } from "@/features/page-history/atoms/history-atoms"; import HistoryView from "@/features/page-history/components/history-view"; import { useEffect, useRef, useState } from "react"; @@ -28,14 +29,17 @@ export default function HistoryModalBody({ pageId }: Props) { const [activeHistoryPrevId, setActiveHistoryPrevId] = useAtom( activeHistoryPrevIdAtom, ); - const [highlightChanges, setHighlightChanges] = useState(true); - const [diffCounts, setDiffCounts] = useState(null); + const [highlightChanges, setHighlightChanges] = useAtom(highlightChangesAtom); + const [diffCounts] = useAtom(diffCountsAtom); + const setDiffCounts = useSetAtom(diffCountsAtom); + const [currentChangeIndex, setCurrentChangeIndex] = useState(0); const scrollViewportRef = useRef(null); useEffect(() => { setActiveHistoryId(""); setActiveHistoryPrevId(""); + setDiffCounts(null); }, [pageId]); useEffect(() => { @@ -49,7 +53,8 @@ export default function HistoryModalBody({ pageId }: Props) { if (element instanceof HTMLElement) { const elementTop = element.offsetTop; const viewportHeight = viewport.clientHeight; - const scrollTarget = elementTop - viewportHeight / 2 + element.offsetHeight / 2; + const scrollTarget = + elementTop - viewportHeight / 2 + element.offsetHeight / 2; viewport.scrollTo({ top: scrollTarget, behavior: "smooth" }); } }; @@ -79,16 +84,14 @@ export default function HistoryModalBody({ pageId }: Props) {
- +
- {activeHistoryId && ( - - )} + {activeHistoryId && }
diff --git a/apps/client/src/features/page-history/components/history-view.tsx b/apps/client/src/features/page-history/components/history-view.tsx index 7b749bc5a..ed8a41f9a 100644 --- a/apps/client/src/features/page-history/components/history-view.tsx +++ b/apps/client/src/features/page-history/components/history-view.tsx @@ -1,24 +1,17 @@ import { usePageHistoryQuery } from "@/features/page-history/queries/page-history-query"; -import { - DiffCounts, - HistoryEditor, -} from "@/features/page-history/components/history-editor"; +import { HistoryEditor } from "@/features/page-history/components/history-editor"; import { useTranslation } from "react-i18next"; +import { useAtomValue } from "jotai"; +import { + activeHistoryIdAtom, + activeHistoryPrevIdAtom, +} from "@/features/page-history/atoms/history-atoms"; -interface HistoryProps { - historyId: string; - prevHistoryId?: string; - highlightChanges?: boolean; - onDiffCalculated?: (counts: DiffCounts) => void; -} - -function HistoryView({ - historyId, - prevHistoryId, - highlightChanges, - onDiffCalculated, -}: HistoryProps) { +function HistoryView() { const { t } = useTranslation(); + const historyId = useAtomValue(activeHistoryIdAtom); + const prevHistoryId = useAtomValue(activeHistoryPrevIdAtom); + const { data, isLoading: isLoadingCurrent, @@ -28,7 +21,7 @@ function HistoryView({ data: prevData, isLoading: isLoadingPrev, isError: isErrorPrev, - } = usePageHistoryQuery(prevHistoryId ?? ""); + } = usePageHistoryQuery(prevHistoryId); if (isLoadingCurrent || isLoadingPrev) { return <>; @@ -39,17 +32,13 @@ function HistoryView({ } return ( - data && ( -
- -
- ) +
+ +
); }