import { ActionIcon, CloseButton, Group, Paper, ScrollArea, Switch, Text, } from "@mantine/core"; import HistoryList from "@/features/page-history/components/history-list"; import classes from "./css/history.module.css"; import { useAtom, useAtomValue } from "jotai"; import { activeHistoryIdAtom, activeHistoryPrevIdAtom, comparePairAtom, diffCountsAtom, highlightChangesAtom, } from "@/features/page-history/atoms/history-atoms"; import HistoryView from "@/features/page-history/components/history-view"; import { useMemo, useRef } from "react"; import { IconChevronUp, IconChevronDown } from "@tabler/icons-react"; import { useTranslation } from "react-i18next"; import { useDiffNavigation, useHistoryReset, } from "@/features/page-history/hooks"; import { usePageHistoryListQuery } from "@/features/page-history/queries/page-history-query"; import { formattedDate } from "@/lib/time"; interface Props { pageId: string; } export default function HistoryModalBody({ pageId }: Props) { const { t } = useTranslation(); const scrollViewportRef = useRef(null); const activeHistoryId = useAtomValue(activeHistoryIdAtom); const activeHistoryPrevId = useAtomValue(activeHistoryPrevIdAtom); const [highlightChanges, setHighlightChanges] = useAtom(highlightChangesAtom); 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); const { currentChangeIndex, handlePrevChange, handleNextChange } = useDiffNavigation(scrollViewportRef); return (
{comparePair && ( {compareLabel ?? t("Compare versions")} setComparePair(null)} /> )}
{comparePair ? ( ) : ( activeHistoryId && )}
{(comparePair || (activeHistoryId && activeHistoryPrevId)) && ( setHighlightChanges(e.currentTarget.checked)} styles={{ label: { userSelect: "none", whiteSpace: "nowrap" } }} /> {highlightChanges && diffCounts && diffCounts.total > 0 && ( {currentChangeIndex} of {diffCounts.total} )} )}
); }