mirror of
https://github.com/docmost/docmost.git
synced 2026-08-15 07:11:35 +10:00
feat: compare two page versions (#2385)
This commit is contained in:
@@ -1291,5 +1291,11 @@
|
|||||||
"{{count}} rows deleted_one": "1 row deleted",
|
"{{count}} rows deleted_one": "1 row deleted",
|
||||||
"{{count}} rows deleted_other": "{{count}} rows deleted",
|
"{{count}} rows deleted_other": "{{count}} rows deleted",
|
||||||
"{{count}} selected_one": "1 selected",
|
"{{count}} selected_one": "1 selected",
|
||||||
"{{count}} selected_other": "{{count}} selected"
|
"{{count}} selected_other": "{{count}} selected",
|
||||||
|
"Compare": "Compare",
|
||||||
|
"Compare versions": "Compare versions",
|
||||||
|
"Select version from {{date}}": "Select version from {{date}}",
|
||||||
|
"Version actions for {{date}}": "Version actions for {{date}}",
|
||||||
|
"Comparing {{newer}} and {{older}}": "Comparing {{newer}} and {{older}}",
|
||||||
|
"Exit compare": "Exit compare"
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -6,4 +6,13 @@ export const activeHistoryPrevIdAtom = atom<string>("");
|
|||||||
export const highlightChangesAtom = atom<boolean>(true);
|
export const highlightChangesAtom = atom<boolean>(true);
|
||||||
|
|
||||||
export type DiffCounts = { added: number; deleted: number; total: number };
|
export type DiffCounts = { added: number; deleted: number; total: number };
|
||||||
export const diffCountsAtom = atom<DiffCounts | null>(null);
|
export const diffCountsAtom = atom<DiffCounts | null>(
|
||||||
|
null as DiffCounts | null,
|
||||||
|
);
|
||||||
|
|
||||||
|
export type ComparePair = { newerId: string; olderId: string };
|
||||||
|
export const compareModeAtom = atom<boolean>(false);
|
||||||
|
export const compareSelectionAtom = atom<string[]>([]);
|
||||||
|
export const comparePairAtom = atom<ComparePair | null>(
|
||||||
|
null as ComparePair | null,
|
||||||
|
);
|
||||||
|
|||||||
@@ -1,7 +1,7 @@
|
|||||||
.history {
|
.history {
|
||||||
display: block;
|
display: flex;
|
||||||
|
align-items: center;
|
||||||
width: 100%;
|
width: 100%;
|
||||||
padding: var(--mantine-spacing-md);
|
|
||||||
color: light-dark(var(--mantine-color-black), var(--mantine-color-dark-0));
|
color: light-dark(var(--mantine-color-black), var(--mantine-color-dark-0));
|
||||||
|
|
||||||
@mixin hover {
|
@mixin hover {
|
||||||
@@ -12,6 +12,28 @@
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
.historyButton {
|
||||||
|
flex: 1;
|
||||||
|
min-width: 0;
|
||||||
|
color: inherit;
|
||||||
|
}
|
||||||
|
|
||||||
|
.compareCheckbox {
|
||||||
|
padding-left: var(--mantine-spacing-xs);
|
||||||
|
}
|
||||||
|
|
||||||
|
.itemMenu {
|
||||||
|
opacity: 0;
|
||||||
|
margin-right: var(--mantine-spacing-xs);
|
||||||
|
}
|
||||||
|
|
||||||
|
.history:hover .itemMenu,
|
||||||
|
.history:focus-within .itemMenu,
|
||||||
|
.history.active .itemMenu,
|
||||||
|
.itemMenu[aria-expanded="true"] {
|
||||||
|
opacity: 1;
|
||||||
|
}
|
||||||
|
|
||||||
.historyEditor {
|
.historyEditor {
|
||||||
:global(.ProseMirror) {
|
:global(.ProseMirror) {
|
||||||
padding: 0 !important;
|
padding: 0 !important;
|
||||||
@@ -77,3 +99,8 @@
|
|||||||
flex: 1;
|
flex: 1;
|
||||||
padding: rem(16px) rem(40px);
|
padding: rem(16px) rem(40px);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
.compareBanner {
|
||||||
|
border-bottom: rem(1px) solid
|
||||||
|
light-dark(var(--mantine-color-gray-3), var(--mantine-color-dark-4));
|
||||||
|
}
|
||||||
|
|||||||
@@ -170,7 +170,6 @@ export function HistoryEditor({
|
|||||||
}
|
}
|
||||||
|
|
||||||
const total = addedCount + deletedCount;
|
const total = addedCount + deletedCount;
|
||||||
// @ts-ignore
|
|
||||||
setDiffCounts({ added: addedCount, deleted: deletedCount, total });
|
setDiffCounts({ added: addedCount, deleted: deletedCount, total });
|
||||||
|
|
||||||
editor.setOptions({
|
editor.setOptions({
|
||||||
|
|||||||
@@ -1,10 +1,21 @@
|
|||||||
import { Text, Group, UnstyledButton, Avatar, Tooltip } from "@mantine/core";
|
import {
|
||||||
|
Text,
|
||||||
|
Group,
|
||||||
|
UnstyledButton,
|
||||||
|
Avatar,
|
||||||
|
Tooltip,
|
||||||
|
ActionIcon,
|
||||||
|
Checkbox,
|
||||||
|
Menu,
|
||||||
|
} from "@mantine/core";
|
||||||
|
import { IconDots } from "@tabler/icons-react";
|
||||||
import { CustomAvatar } from "@/components/ui/custom-avatar.tsx";
|
import { CustomAvatar } from "@/components/ui/custom-avatar.tsx";
|
||||||
import { formattedDate } from "@/lib/time";
|
import { formattedDate } from "@/lib/time";
|
||||||
import classes from "./css/history.module.css";
|
import classes from "./css/history.module.css";
|
||||||
import clsx from "clsx";
|
import clsx from "clsx";
|
||||||
import { IPageHistory } from "@/features/page-history/types/page.types";
|
import { IPageHistory } from "@/features/page-history/types/page.types";
|
||||||
import { memo, useCallback } from "react";
|
import { memo, useCallback } from "react";
|
||||||
|
import { useTranslation } from "react-i18next";
|
||||||
|
|
||||||
const MAX_VISIBLE_AVATARS = 5;
|
const MAX_VISIBLE_AVATARS = 5;
|
||||||
|
|
||||||
@@ -15,6 +26,13 @@ interface HistoryItemProps {
|
|||||||
onHover?: (id: string, index: number) => void;
|
onHover?: (id: string, index: number) => void;
|
||||||
onHoverEnd?: () => void;
|
onHoverEnd?: () => void;
|
||||||
isActive: boolean;
|
isActive: boolean;
|
||||||
|
compareMode: boolean;
|
||||||
|
isChecked: boolean;
|
||||||
|
isCheckboxDisabled: boolean;
|
||||||
|
canCompare: boolean;
|
||||||
|
onToggleCompare: (id: string) => void;
|
||||||
|
onStartCompare: (id: string) => void;
|
||||||
|
onRestore?: (id: string, index: number) => void;
|
||||||
}
|
}
|
||||||
|
|
||||||
const HistoryItem = memo(function HistoryItem({
|
const HistoryItem = memo(function HistoryItem({
|
||||||
@@ -24,10 +42,24 @@ const HistoryItem = memo(function HistoryItem({
|
|||||||
onHover,
|
onHover,
|
||||||
onHoverEnd,
|
onHoverEnd,
|
||||||
isActive,
|
isActive,
|
||||||
|
compareMode,
|
||||||
|
isChecked,
|
||||||
|
isCheckboxDisabled,
|
||||||
|
canCompare,
|
||||||
|
onToggleCompare,
|
||||||
|
onStartCompare,
|
||||||
|
onRestore,
|
||||||
}: HistoryItemProps) {
|
}: HistoryItemProps) {
|
||||||
|
const { t } = useTranslation();
|
||||||
|
const date = formattedDate(new Date(historyItem.createdAt));
|
||||||
|
|
||||||
const handleClick = useCallback(() => {
|
const handleClick = useCallback(() => {
|
||||||
onSelect(historyItem.id, index);
|
if (compareMode) {
|
||||||
}, [onSelect, historyItem.id, index]);
|
onToggleCompare(historyItem.id);
|
||||||
|
} else {
|
||||||
|
onSelect(historyItem.id, index);
|
||||||
|
}
|
||||||
|
}, [compareMode, onToggleCompare, onSelect, historyItem.id, index]);
|
||||||
|
|
||||||
const handleMouseEnter = useCallback(() => {
|
const handleMouseEnter = useCallback(() => {
|
||||||
onHover?.(historyItem.id, index);
|
onHover?.(historyItem.id, index);
|
||||||
@@ -37,63 +69,115 @@ const HistoryItem = memo(function HistoryItem({
|
|||||||
const hasContributors = contributors && contributors.length > 0;
|
const hasContributors = contributors && contributors.length > 0;
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<UnstyledButton
|
<div
|
||||||
p="xs"
|
className={clsx(classes.history, { [classes.active]: isActive })}
|
||||||
onClick={handleClick}
|
|
||||||
onMouseEnter={handleMouseEnter}
|
onMouseEnter={handleMouseEnter}
|
||||||
onMouseLeave={onHoverEnd}
|
onMouseLeave={onHoverEnd}
|
||||||
className={clsx(classes.history, { [classes.active]: isActive })}
|
|
||||||
>
|
>
|
||||||
<Text size="sm">{formattedDate(new Date(historyItem.createdAt))}</Text>
|
{compareMode && (
|
||||||
|
<Checkbox
|
||||||
|
size="xs"
|
||||||
|
className={classes.compareCheckbox}
|
||||||
|
checked={isChecked}
|
||||||
|
disabled={isCheckboxDisabled}
|
||||||
|
onChange={() => onToggleCompare(historyItem.id)}
|
||||||
|
aria-label={t("Select version from {{date}}", { date })}
|
||||||
|
/>
|
||||||
|
)}
|
||||||
|
|
||||||
<Group gap={6} wrap="nowrap" mt={4}>
|
<UnstyledButton
|
||||||
{hasContributors ? (
|
p="xs"
|
||||||
<>
|
onClick={handleClick}
|
||||||
<Tooltip.Group openDelay={300} closeDelay={100}>
|
className={classes.historyButton}
|
||||||
<Avatar.Group spacing={8}>
|
>
|
||||||
{contributors.slice(0, MAX_VISIBLE_AVATARS).map((contributor) => (
|
<Text size="sm">{date}</Text>
|
||||||
<Tooltip key={contributor.id} label={contributor.name} withArrow>
|
|
||||||
<CustomAvatar
|
<Group gap={6} wrap="nowrap" mt={4}>
|
||||||
size="sm"
|
{hasContributors ? (
|
||||||
avatarUrl={contributor.avatarUrl}
|
<>
|
||||||
name={contributor.name}
|
<Tooltip.Group openDelay={300} closeDelay={100}>
|
||||||
/>
|
<Avatar.Group spacing={8}>
|
||||||
</Tooltip>
|
{contributors
|
||||||
))}
|
.slice(0, MAX_VISIBLE_AVATARS)
|
||||||
{contributors.length > MAX_VISIBLE_AVATARS && (
|
.map((contributor) => (
|
||||||
<Tooltip
|
<Tooltip
|
||||||
withArrow
|
key={contributor.id}
|
||||||
label={contributors.slice(MAX_VISIBLE_AVATARS).map((c) => (
|
label={contributor.name}
|
||||||
<div key={c.id}>{c.name}</div>
|
withArrow
|
||||||
|
>
|
||||||
|
<CustomAvatar
|
||||||
|
size="sm"
|
||||||
|
avatarUrl={contributor.avatarUrl}
|
||||||
|
name={contributor.name}
|
||||||
|
/>
|
||||||
|
</Tooltip>
|
||||||
))}
|
))}
|
||||||
>
|
{contributors.length > MAX_VISIBLE_AVATARS && (
|
||||||
<Avatar size="sm" color="gray">
|
<Tooltip
|
||||||
+{contributors.length - MAX_VISIBLE_AVATARS}
|
withArrow
|
||||||
</Avatar>
|
label={contributors
|
||||||
</Tooltip>
|
.slice(MAX_VISIBLE_AVATARS)
|
||||||
)}
|
.map((c) => (
|
||||||
</Avatar.Group>
|
<div key={c.id}>{c.name}</div>
|
||||||
</Tooltip.Group>
|
))}
|
||||||
{contributors.length === 1 && (
|
>
|
||||||
|
<Avatar size="sm" color="gray">
|
||||||
|
+{contributors.length - MAX_VISIBLE_AVATARS}
|
||||||
|
</Avatar>
|
||||||
|
</Tooltip>
|
||||||
|
)}
|
||||||
|
</Avatar.Group>
|
||||||
|
</Tooltip.Group>
|
||||||
|
{contributors.length === 1 && (
|
||||||
|
<Text size="sm" c="dimmed" lineClamp={1}>
|
||||||
|
{contributors[0].name}
|
||||||
|
</Text>
|
||||||
|
)}
|
||||||
|
</>
|
||||||
|
) : (
|
||||||
|
<>
|
||||||
|
<CustomAvatar
|
||||||
|
size="sm"
|
||||||
|
avatarUrl={historyItem.lastUpdatedBy?.avatarUrl}
|
||||||
|
name={historyItem.lastUpdatedBy?.name}
|
||||||
|
/>
|
||||||
<Text size="sm" c="dimmed" lineClamp={1}>
|
<Text size="sm" c="dimmed" lineClamp={1}>
|
||||||
{contributors[0].name}
|
{historyItem.lastUpdatedBy?.name}
|
||||||
</Text>
|
</Text>
|
||||||
|
</>
|
||||||
|
)}
|
||||||
|
</Group>
|
||||||
|
</UnstyledButton>
|
||||||
|
|
||||||
|
{!compareMode && (
|
||||||
|
<Menu shadow="md" width={180} position="bottom-end">
|
||||||
|
<Menu.Target>
|
||||||
|
<ActionIcon
|
||||||
|
variant="subtle"
|
||||||
|
color="gray"
|
||||||
|
className={classes.itemMenu}
|
||||||
|
aria-label={t("Version actions for {{date}}", { date })}
|
||||||
|
onClick={(e) => e.stopPropagation()}
|
||||||
|
>
|
||||||
|
<IconDots size={18} />
|
||||||
|
</ActionIcon>
|
||||||
|
</Menu.Target>
|
||||||
|
<Menu.Dropdown>
|
||||||
|
<Menu.Item
|
||||||
|
disabled={!canCompare}
|
||||||
|
onClick={() => onStartCompare(historyItem.id)}
|
||||||
|
>
|
||||||
|
{t("Compare")}
|
||||||
|
</Menu.Item>
|
||||||
|
{onRestore && (
|
||||||
|
<Menu.Item onClick={() => onRestore(historyItem.id, index)}>
|
||||||
|
{t("Restore")}
|
||||||
|
</Menu.Item>
|
||||||
)}
|
)}
|
||||||
</>
|
</Menu.Dropdown>
|
||||||
) : (
|
</Menu>
|
||||||
<>
|
)}
|
||||||
<CustomAvatar
|
</div>
|
||||||
size="sm"
|
|
||||||
avatarUrl={historyItem.lastUpdatedBy?.avatarUrl}
|
|
||||||
name={historyItem.lastUpdatedBy?.name}
|
|
||||||
/>
|
|
||||||
<Text size="sm" c="dimmed" lineClamp={1}>
|
|
||||||
{historyItem.lastUpdatedBy?.name}
|
|
||||||
</Text>
|
|
||||||
</>
|
|
||||||
)}
|
|
||||||
</Group>
|
|
||||||
</UnstyledButton>
|
|
||||||
);
|
);
|
||||||
});
|
});
|
||||||
|
|
||||||
|
|||||||
@@ -6,8 +6,12 @@ import HistoryItem from "@/features/page-history/components/history-item";
|
|||||||
import {
|
import {
|
||||||
activeHistoryIdAtom,
|
activeHistoryIdAtom,
|
||||||
activeHistoryPrevIdAtom,
|
activeHistoryPrevIdAtom,
|
||||||
|
compareModeAtom,
|
||||||
|
comparePairAtom,
|
||||||
|
compareSelectionAtom,
|
||||||
historyAtoms,
|
historyAtoms,
|
||||||
} from "@/features/page-history/atoms/history-atoms";
|
} from "@/features/page-history/atoms/history-atoms";
|
||||||
|
import { resolveComparePair } from "@/features/page-history/utils/resolve-compare-pair";
|
||||||
import { useAtom, useSetAtom } from "jotai";
|
import { useAtom, useSetAtom } from "jotai";
|
||||||
import { useCallback, useEffect, useMemo, useRef } from "react";
|
import { useCallback, useEffect, useMemo, useRef } from "react";
|
||||||
import {
|
import {
|
||||||
@@ -32,6 +36,9 @@ function HistoryList({ pageId }: Props) {
|
|||||||
const [activeHistoryId, setActiveHistoryId] = useAtom(activeHistoryIdAtom);
|
const [activeHistoryId, setActiveHistoryId] = useAtom(activeHistoryIdAtom);
|
||||||
const setActiveHistoryPrevId = useSetAtom(activeHistoryPrevIdAtom);
|
const setActiveHistoryPrevId = useSetAtom(activeHistoryPrevIdAtom);
|
||||||
const setHistoryModalOpen = useSetAtom(historyAtoms);
|
const setHistoryModalOpen = useSetAtom(historyAtoms);
|
||||||
|
const [compareMode, setCompareMode] = useAtom(compareModeAtom);
|
||||||
|
const [compareSelection, setCompareSelection] = useAtom(compareSelectionAtom);
|
||||||
|
const setComparePair = useSetAtom(comparePairAtom);
|
||||||
|
|
||||||
const {
|
const {
|
||||||
data: pageHistoryData,
|
data: pageHistoryData,
|
||||||
@@ -79,10 +86,58 @@ function HistoryList({ pageId }: Props) {
|
|||||||
|
|
||||||
const handleSelect = useCallback(
|
const handleSelect = useCallback(
|
||||||
(id: string, index: number) => {
|
(id: string, index: number) => {
|
||||||
|
setComparePair(null);
|
||||||
setActiveHistoryId(id);
|
setActiveHistoryId(id);
|
||||||
setActiveHistoryPrevId(historyItems[index + 1]?.id ?? "");
|
setActiveHistoryPrevId(historyItems[index + 1]?.id ?? "");
|
||||||
},
|
},
|
||||||
[historyItems, setActiveHistoryId, setActiveHistoryPrevId],
|
[historyItems, setActiveHistoryId, setActiveHistoryPrevId, setComparePair],
|
||||||
|
);
|
||||||
|
|
||||||
|
const handleToggleCompare = useCallback(
|
||||||
|
(id: string) => {
|
||||||
|
setCompareSelection((prev) => {
|
||||||
|
if (prev.includes(id)) return prev.filter((item) => item !== id);
|
||||||
|
if (prev.length >= 2) return prev;
|
||||||
|
return [...prev, id];
|
||||||
|
});
|
||||||
|
},
|
||||||
|
[setCompareSelection],
|
||||||
|
);
|
||||||
|
|
||||||
|
const handleStartCompare = useCallback(
|
||||||
|
(id: string) => {
|
||||||
|
setComparePair(null);
|
||||||
|
setCompareMode(true);
|
||||||
|
setCompareSelection([id]);
|
||||||
|
},
|
||||||
|
[setComparePair, setCompareMode, setCompareSelection],
|
||||||
|
);
|
||||||
|
|
||||||
|
const handleCancelCompare = useCallback(() => {
|
||||||
|
setCompareMode(false);
|
||||||
|
setCompareSelection([]);
|
||||||
|
}, [setCompareMode, setCompareSelection]);
|
||||||
|
|
||||||
|
const handleConfirmCompare = useCallback(() => {
|
||||||
|
const pair = resolveComparePair(historyItems, compareSelection);
|
||||||
|
if (!pair) return;
|
||||||
|
setComparePair(pair);
|
||||||
|
setCompareMode(false);
|
||||||
|
setCompareSelection([]);
|
||||||
|
}, [
|
||||||
|
historyItems,
|
||||||
|
compareSelection,
|
||||||
|
setComparePair,
|
||||||
|
setCompareMode,
|
||||||
|
setCompareSelection,
|
||||||
|
]);
|
||||||
|
|
||||||
|
const handleRestoreItem = useCallback(
|
||||||
|
(id: string, index: number) => {
|
||||||
|
handleSelect(id, index);
|
||||||
|
confirmRestore(id);
|
||||||
|
},
|
||||||
|
[handleSelect, confirmRestore],
|
||||||
);
|
);
|
||||||
|
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
@@ -138,6 +193,16 @@ function HistoryList({ pageId }: Props) {
|
|||||||
onHover={handleHover}
|
onHover={handleHover}
|
||||||
onHoverEnd={clearPrefetchTimeout}
|
onHoverEnd={clearPrefetchTimeout}
|
||||||
isActive={historyItem.id === activeHistoryId}
|
isActive={historyItem.id === activeHistoryId}
|
||||||
|
compareMode={compareMode}
|
||||||
|
isChecked={compareSelection.includes(historyItem.id)}
|
||||||
|
isCheckboxDisabled={
|
||||||
|
!compareSelection.includes(historyItem.id) &&
|
||||||
|
compareSelection.length >= 2
|
||||||
|
}
|
||||||
|
canCompare={historyItems.length >= 2}
|
||||||
|
onToggleCompare={handleToggleCompare}
|
||||||
|
onStartCompare={handleStartCompare}
|
||||||
|
onRestore={canRestore ? handleRestoreItem : undefined}
|
||||||
/>
|
/>
|
||||||
))}
|
))}
|
||||||
{hasNextPage && <div ref={loadMoreRef} style={{ height: 1 }} />}
|
{hasNextPage && <div ref={loadMoreRef} style={{ height: 1 }} />}
|
||||||
@@ -148,22 +213,44 @@ function HistoryList({ pageId }: Props) {
|
|||||||
)}
|
)}
|
||||||
</ScrollArea>
|
</ScrollArea>
|
||||||
|
|
||||||
{canRestore && (
|
{compareMode ? (
|
||||||
<>
|
<>
|
||||||
<Divider />
|
<Divider />
|
||||||
<Group p="xs" wrap="nowrap">
|
<Group p="xs" wrap="nowrap">
|
||||||
<Button
|
<Button
|
||||||
variant="default"
|
variant="default"
|
||||||
size="compact-md"
|
size="compact-md"
|
||||||
onClick={() => setHistoryModalOpen(false)}
|
onClick={handleCancelCompare}
|
||||||
>
|
>
|
||||||
{t("Cancel")}
|
{t("Cancel")}
|
||||||
</Button>
|
</Button>
|
||||||
<Button size="compact-md" onClick={confirmRestore}>
|
<Button
|
||||||
{t("Restore")}
|
size="compact-md"
|
||||||
|
disabled={compareSelection.length !== 2}
|
||||||
|
onClick={handleConfirmCompare}
|
||||||
|
>
|
||||||
|
{t("Compare")}
|
||||||
</Button>
|
</Button>
|
||||||
</Group>
|
</Group>
|
||||||
</>
|
</>
|
||||||
|
) : (
|
||||||
|
canRestore && (
|
||||||
|
<>
|
||||||
|
<Divider />
|
||||||
|
<Group p="xs" wrap="nowrap">
|
||||||
|
<Button
|
||||||
|
variant="default"
|
||||||
|
size="compact-md"
|
||||||
|
onClick={() => setHistoryModalOpen(false)}
|
||||||
|
>
|
||||||
|
{t("Cancel")}
|
||||||
|
</Button>
|
||||||
|
<Button size="compact-md" onClick={() => confirmRestore()}>
|
||||||
|
{t("Restore")}
|
||||||
|
</Button>
|
||||||
|
</Group>
|
||||||
|
</>
|
||||||
|
)
|
||||||
)}
|
)}
|
||||||
</div>
|
</div>
|
||||||
);
|
);
|
||||||
|
|||||||
@@ -1,5 +1,6 @@
|
|||||||
import {
|
import {
|
||||||
ActionIcon,
|
ActionIcon,
|
||||||
|
CloseButton,
|
||||||
Group,
|
Group,
|
||||||
Paper,
|
Paper,
|
||||||
ScrollArea,
|
ScrollArea,
|
||||||
@@ -12,17 +13,20 @@ import { useAtom, useAtomValue } from "jotai";
|
|||||||
import {
|
import {
|
||||||
activeHistoryIdAtom,
|
activeHistoryIdAtom,
|
||||||
activeHistoryPrevIdAtom,
|
activeHistoryPrevIdAtom,
|
||||||
|
comparePairAtom,
|
||||||
diffCountsAtom,
|
diffCountsAtom,
|
||||||
highlightChangesAtom,
|
highlightChangesAtom,
|
||||||
} from "@/features/page-history/atoms/history-atoms";
|
} from "@/features/page-history/atoms/history-atoms";
|
||||||
import HistoryView from "@/features/page-history/components/history-view";
|
import HistoryView from "@/features/page-history/components/history-view";
|
||||||
import { useRef } from "react";
|
import { useMemo, useRef } from "react";
|
||||||
import { IconChevronUp, IconChevronDown } from "@tabler/icons-react";
|
import { IconChevronUp, IconChevronDown } from "@tabler/icons-react";
|
||||||
import { useTranslation } from "react-i18next";
|
import { useTranslation } from "react-i18next";
|
||||||
import {
|
import {
|
||||||
useDiffNavigation,
|
useDiffNavigation,
|
||||||
useHistoryReset,
|
useHistoryReset,
|
||||||
} from "@/features/page-history/hooks";
|
} from "@/features/page-history/hooks";
|
||||||
|
import { usePageHistoryListQuery } from "@/features/page-history/queries/page-history-query";
|
||||||
|
import { formattedDate } from "@/lib/time";
|
||||||
|
|
||||||
interface Props {
|
interface Props {
|
||||||
pageId: string;
|
pageId: string;
|
||||||
@@ -36,6 +40,28 @@ export default function HistoryModalBody({ pageId }: Props) {
|
|||||||
const activeHistoryPrevId = useAtomValue(activeHistoryPrevIdAtom);
|
const activeHistoryPrevId = useAtomValue(activeHistoryPrevIdAtom);
|
||||||
const [highlightChanges, setHighlightChanges] = useAtom(highlightChangesAtom);
|
const [highlightChanges, setHighlightChanges] = useAtom(highlightChangesAtom);
|
||||||
const diffCounts = useAtomValue(diffCountsAtom);
|
const diffCounts = useAtomValue(diffCountsAtom);
|
||||||
|
const [comparePair, setComparePair] = useAtom(comparePairAtom);
|
||||||
|
|
||||||
|
const { data: pageHistoryData } = usePageHistoryListQuery(pageId);
|
||||||
|
const historyItems = useMemo(
|
||||||
|
() => pageHistoryData?.pages.flatMap((page) => page.items) ?? [],
|
||||||
|
[pageHistoryData],
|
||||||
|
);
|
||||||
|
|
||||||
|
const compareLabel = useMemo(() => {
|
||||||
|
if (!comparePair) return null;
|
||||||
|
const newerItem = historyItems.find(
|
||||||
|
(item) => item.id === comparePair.newerId,
|
||||||
|
);
|
||||||
|
const olderItem = historyItems.find(
|
||||||
|
(item) => item.id === comparePair.olderId,
|
||||||
|
);
|
||||||
|
if (!newerItem || !olderItem) return null;
|
||||||
|
return t("Comparing {{newer}} and {{older}}", {
|
||||||
|
newer: formattedDate(new Date(newerItem.createdAt)),
|
||||||
|
older: formattedDate(new Date(olderItem.createdAt)),
|
||||||
|
});
|
||||||
|
}, [comparePair, historyItems, t]);
|
||||||
|
|
||||||
useHistoryReset(pageId);
|
useHistoryReset(pageId);
|
||||||
const { currentChangeIndex, handlePrevChange, handleNextChange } =
|
const { currentChangeIndex, handlePrevChange, handleNextChange } =
|
||||||
@@ -50,6 +76,25 @@ export default function HistoryModalBody({ pageId }: Props) {
|
|||||||
</nav>
|
</nav>
|
||||||
|
|
||||||
<div style={{ position: "relative", flex: 1 }}>
|
<div style={{ position: "relative", flex: 1 }}>
|
||||||
|
{comparePair && (
|
||||||
|
<Group
|
||||||
|
justify="space-between"
|
||||||
|
wrap="nowrap"
|
||||||
|
px="md"
|
||||||
|
py={4}
|
||||||
|
className={classes.compareBanner}
|
||||||
|
>
|
||||||
|
<Text size="sm" fw={500} lineClamp={1}>
|
||||||
|
{compareLabel ?? t("Compare versions")}
|
||||||
|
</Text>
|
||||||
|
<CloseButton
|
||||||
|
size="sm"
|
||||||
|
aria-label={t("Exit compare")}
|
||||||
|
onClick={() => setComparePair(null)}
|
||||||
|
/>
|
||||||
|
</Group>
|
||||||
|
)}
|
||||||
|
|
||||||
<ScrollArea
|
<ScrollArea
|
||||||
h={650}
|
h={650}
|
||||||
w="100%"
|
w="100%"
|
||||||
@@ -57,11 +102,18 @@ export default function HistoryModalBody({ pageId }: Props) {
|
|||||||
viewportRef={scrollViewportRef}
|
viewportRef={scrollViewportRef}
|
||||||
>
|
>
|
||||||
<div className={classes.sidebarRightSection}>
|
<div className={classes.sidebarRightSection}>
|
||||||
{activeHistoryId && <HistoryView />}
|
{comparePair ? (
|
||||||
|
<HistoryView
|
||||||
|
historyId={comparePair.newerId}
|
||||||
|
prevHistoryId={comparePair.olderId}
|
||||||
|
/>
|
||||||
|
) : (
|
||||||
|
activeHistoryId && <HistoryView />
|
||||||
|
)}
|
||||||
</div>
|
</div>
|
||||||
</ScrollArea>
|
</ScrollArea>
|
||||||
|
|
||||||
{activeHistoryId && activeHistoryPrevId && (
|
{(comparePair || (activeHistoryId && activeHistoryPrevId)) && (
|
||||||
<Paper
|
<Paper
|
||||||
shadow="md"
|
shadow="md"
|
||||||
radius="xl"
|
radius="xl"
|
||||||
|
|||||||
@@ -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>
|
||||||
)}
|
)}
|
||||||
|
|
||||||
|
|||||||
@@ -7,21 +7,29 @@ import {
|
|||||||
activeHistoryPrevIdAtom,
|
activeHistoryPrevIdAtom,
|
||||||
} from "@/features/page-history/atoms/history-atoms";
|
} from "@/features/page-history/atoms/history-atoms";
|
||||||
|
|
||||||
function HistoryView() {
|
interface Props {
|
||||||
|
historyId?: string;
|
||||||
|
prevHistoryId?: string;
|
||||||
|
}
|
||||||
|
|
||||||
|
function HistoryView({ historyId, prevHistoryId }: Props) {
|
||||||
const { t } = useTranslation();
|
const { t } = useTranslation();
|
||||||
const historyId = useAtomValue(activeHistoryIdAtom);
|
const activeId = useAtomValue(activeHistoryIdAtom);
|
||||||
const prevHistoryId = useAtomValue(activeHistoryPrevIdAtom);
|
const activePrevId = useAtomValue(activeHistoryPrevIdAtom);
|
||||||
|
|
||||||
|
const resolvedId = historyId ?? activeId;
|
||||||
|
const resolvedPrevId = prevHistoryId ?? activePrevId;
|
||||||
|
|
||||||
const {
|
const {
|
||||||
data,
|
data,
|
||||||
isLoading: isLoadingCurrent,
|
isLoading: isLoadingCurrent,
|
||||||
isError: isErrorCurrent,
|
isError: isErrorCurrent,
|
||||||
} = usePageHistoryQuery(historyId);
|
} = usePageHistoryQuery(resolvedId);
|
||||||
const {
|
const {
|
||||||
data: prevData,
|
data: prevData,
|
||||||
isLoading: isLoadingPrev,
|
isLoading: isLoadingPrev,
|
||||||
isError: isErrorPrev,
|
isError: isErrorPrev,
|
||||||
} = usePageHistoryQuery(prevHistoryId);
|
} = usePageHistoryQuery(resolvedPrevId);
|
||||||
|
|
||||||
if (isLoadingCurrent || isLoadingPrev) {
|
if (isLoadingCurrent || isLoadingPrev) {
|
||||||
return <></>;
|
return <></>;
|
||||||
|
|||||||
@@ -3,22 +3,45 @@ import { useEffect } from "react";
|
|||||||
import {
|
import {
|
||||||
activeHistoryIdAtom,
|
activeHistoryIdAtom,
|
||||||
activeHistoryPrevIdAtom,
|
activeHistoryPrevIdAtom,
|
||||||
|
compareModeAtom,
|
||||||
|
comparePairAtom,
|
||||||
|
compareSelectionAtom,
|
||||||
diffCountsAtom,
|
diffCountsAtom,
|
||||||
} from "@/features/page-history/atoms/history-atoms";
|
} from "@/features/page-history/atoms/history-atoms";
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Resets history state when pageId changes.
|
* Resets history state when pageId changes.
|
||||||
* Clears active selection and diff counts.
|
* Clears active selection, diff counts, and compare state.
|
||||||
|
* Compare state also resets on unmount so reopening the modal starts clean.
|
||||||
*/
|
*/
|
||||||
export function useHistoryReset(pageId: string) {
|
export function useHistoryReset(pageId: string) {
|
||||||
const [, setActiveHistoryId] = useAtom(activeHistoryIdAtom);
|
const [, setActiveHistoryId] = useAtom(activeHistoryIdAtom);
|
||||||
const [, setActiveHistoryPrevId] = useAtom(activeHistoryPrevIdAtom);
|
const [, setActiveHistoryPrevId] = useAtom(activeHistoryPrevIdAtom);
|
||||||
const [, setDiffCounts] = useAtom(diffCountsAtom);
|
const [, setDiffCounts] = useAtom(diffCountsAtom);
|
||||||
|
const [, setCompareMode] = useAtom(compareModeAtom);
|
||||||
|
const [, setCompareSelection] = useAtom(compareSelectionAtom);
|
||||||
|
const [, setComparePair] = useAtom(comparePairAtom);
|
||||||
|
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
|
const resetCompare = () => {
|
||||||
|
setCompareMode(false);
|
||||||
|
setCompareSelection([]);
|
||||||
|
setComparePair(null);
|
||||||
|
};
|
||||||
|
|
||||||
setActiveHistoryId("");
|
setActiveHistoryId("");
|
||||||
setActiveHistoryPrevId("");
|
setActiveHistoryPrevId("");
|
||||||
// @ts-ignore
|
|
||||||
setDiffCounts(null);
|
setDiffCounts(null);
|
||||||
}, [pageId, setActiveHistoryId, setActiveHistoryPrevId, setDiffCounts]);
|
resetCompare();
|
||||||
|
|
||||||
|
return resetCompare;
|
||||||
|
}, [
|
||||||
|
pageId,
|
||||||
|
setActiveHistoryId,
|
||||||
|
setActiveHistoryPrevId,
|
||||||
|
setDiffCounts,
|
||||||
|
setCompareMode,
|
||||||
|
setCompareSelection,
|
||||||
|
setComparePair,
|
||||||
|
]);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -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,47 +39,66 @@ export function useHistoryRestore() {
|
|||||||
SpaceCaslSubject.Page,
|
SpaceCaslSubject.Page,
|
||||||
);
|
);
|
||||||
|
|
||||||
const handleRestore = useCallback(() => {
|
const handleRestore = useCallback(
|
||||||
if (!activeHistoryData) return;
|
async (historyId: string) => {
|
||||||
if (
|
let historyData: IPageHistory;
|
||||||
!mainEditor ||
|
try {
|
||||||
mainEditor.isDestroyed ||
|
historyData = await fetchPageHistory(historyId);
|
||||||
!mainEditorTitle ||
|
} catch {
|
||||||
mainEditorTitle.isDestroyed
|
notifications.show({
|
||||||
) {
|
message: t("Error fetching page data."),
|
||||||
return;
|
color: "red",
|
||||||
}
|
});
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
mainEditorTitle
|
if (
|
||||||
.chain()
|
!mainEditor ||
|
||||||
.clearContent()
|
mainEditor.isDestroyed ||
|
||||||
.setContent(activeHistoryData.title, { emitUpdate: true })
|
!mainEditorTitle ||
|
||||||
.run();
|
mainEditorTitle.isDestroyed
|
||||||
|
) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
mainEditor
|
mainEditorTitle
|
||||||
.chain()
|
.chain()
|
||||||
.clearContent()
|
.clearContent()
|
||||||
.setContent(activeHistoryData.content)
|
.setContent(historyData.title, { emitUpdate: true })
|
||||||
.run();
|
.run();
|
||||||
|
|
||||||
setHistoryModalOpen(false);
|
mainEditor
|
||||||
notifications.show({ message: t("Successfully restored") });
|
.chain()
|
||||||
}, [activeHistoryData, mainEditor, mainEditorTitle, setHistoryModalOpen, t]);
|
.clearContent()
|
||||||
|
.setContent(historyData.content)
|
||||||
|
.run();
|
||||||
|
|
||||||
const confirmRestore = useCallback(() => {
|
setHistoryModalOpen(false);
|
||||||
modals.openConfirmModal({
|
notifications.show({ message: t("Successfully restored") });
|
||||||
title: t("Please confirm your action"),
|
},
|
||||||
children: (
|
[mainEditor, mainEditorTitle, setHistoryModalOpen, t],
|
||||||
<Text size="sm">
|
);
|
||||||
{t(
|
|
||||||
"Are you sure you want to restore this version? Any changes not versioned will be lost.",
|
const confirmRestore = useCallback(
|
||||||
)}
|
(historyId?: string) => {
|
||||||
</Text>
|
const targetId = historyId ?? activeHistoryId;
|
||||||
),
|
if (!targetId) return;
|
||||||
labels: { confirm: t("Confirm"), cancel: t("Cancel") },
|
|
||||||
onConfirm: handleRestore,
|
modals.openConfirmModal({
|
||||||
});
|
title: t("Please confirm your action"),
|
||||||
}, [t, handleRestore]);
|
children: (
|
||||||
|
<Text size="sm">
|
||||||
|
{t(
|
||||||
|
"Are you sure you want to restore this version? Any changes not versioned will be lost.",
|
||||||
|
)}
|
||||||
|
</Text>
|
||||||
|
),
|
||||||
|
labels: { confirm: t("Confirm"), cancel: t("Cancel") },
|
||||||
|
onConfirm: () => handleRestore(targetId),
|
||||||
|
});
|
||||||
|
},
|
||||||
|
[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>> {
|
||||||
|
|||||||
@@ -0,0 +1,32 @@
|
|||||||
|
import { describe, expect, it } from "vitest";
|
||||||
|
import { resolveComparePair } from "./resolve-compare-pair";
|
||||||
|
|
||||||
|
// list is newest-first, matching usePageHistoryListQuery order
|
||||||
|
const items = [{ id: "v3" }, { id: "v2" }, { id: "v1" }];
|
||||||
|
|
||||||
|
describe("resolveComparePair", () => {
|
||||||
|
it("orders newer before older regardless of selection order", () => {
|
||||||
|
expect(resolveComparePair(items, ["v1", "v3"])).toEqual({
|
||||||
|
newerId: "v3",
|
||||||
|
olderId: "v1",
|
||||||
|
});
|
||||||
|
expect(resolveComparePair(items, ["v3", "v1"])).toEqual({
|
||||||
|
newerId: "v3",
|
||||||
|
olderId: "v1",
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
it("returns null unless exactly two versions are selected", () => {
|
||||||
|
expect(resolveComparePair(items, [])).toBeNull();
|
||||||
|
expect(resolveComparePair(items, ["v1"])).toBeNull();
|
||||||
|
expect(resolveComparePair(items, ["v1", "v2", "v3"])).toBeNull();
|
||||||
|
});
|
||||||
|
|
||||||
|
it("returns null when a selected id is not in the list", () => {
|
||||||
|
expect(resolveComparePair(items, ["v1", "missing"])).toBeNull();
|
||||||
|
});
|
||||||
|
|
||||||
|
it("returns null when the same id is selected twice", () => {
|
||||||
|
expect(resolveComparePair(items, ["v2", "v2"])).toBeNull();
|
||||||
|
});
|
||||||
|
});
|
||||||
@@ -0,0 +1,18 @@
|
|||||||
|
import { ComparePair } from "@/features/page-history/atoms/history-atoms";
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Resolves which of the two selected versions is newer using their position
|
||||||
|
* in the history list (list is newest-first: lower index = newer).
|
||||||
|
*/
|
||||||
|
export function resolveComparePair(
|
||||||
|
historyItems: { id: string }[],
|
||||||
|
selection: string[],
|
||||||
|
): ComparePair | null {
|
||||||
|
if (selection.length !== 2) return null;
|
||||||
|
const indexA = historyItems.findIndex((item) => item.id === selection[0]);
|
||||||
|
const indexB = historyItems.findIndex((item) => item.id === selection[1]);
|
||||||
|
if (indexA === -1 || indexB === -1 || indexA === indexB) return null;
|
||||||
|
return indexA < indexB
|
||||||
|
? { newerId: selection[0], olderId: selection[1] }
|
||||||
|
: { newerId: selection[1], olderId: selection[0] };
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user