import { ActionIcon, Box, Button, Group, Paper, ScrollArea, Select, Switch, Text, } from "@mantine/core"; import { useAtom, useAtomValue, useSetAtom } from "jotai"; import { activeHistoryIdAtom, activeHistoryPrevIdAtom, diffCountsAtom, highlightChangesAtom, historyAtoms, } from "@/features/page-history/atoms/history-atoms"; import HistoryView from "@/features/page-history/components/history-view"; import { useCallback, useEffect, useMemo, useRef } from "react"; import { IconCheck, IconChevronDown, IconChevronUp } from "@tabler/icons-react"; import { useTranslation } from "react-i18next"; import { usePageHistoryListQuery } from "@/features/page-history/queries/page-history-query"; import { formattedDate } from "@/lib/time"; import { useDiffNavigation, useHistoryReset, useHistoryRestore, } from "@/features/page-history/hooks"; import classes from "./css/history-mobile.module.css"; interface Props { pageId: string; pageTitle?: string; } export default function HistoryModalMobile({ pageId, pageTitle }: Props) { const { t } = useTranslation(); const [activeHistoryId, setActiveHistoryId] = useAtom(activeHistoryIdAtom); const setActiveHistoryPrevId = useSetAtom(activeHistoryPrevIdAtom); const [highlightChanges, setHighlightChanges] = useAtom(highlightChangesAtom); const diffCounts = useAtomValue(diffCountsAtom); const setHistoryModalOpen = useSetAtom(historyAtoms); const scrollViewportRef = useRef(null); const dropdownViewportRef = useRef(null); const { data: pageHistoryData, isLoading, fetchNextPage, hasNextPage, isFetchingNextPage, } = usePageHistoryListQuery(pageId); const historyItems = useMemo( () => pageHistoryData?.pages.flatMap((page) => page.items) ?? [], [pageHistoryData], ); const selectData = useMemo( () => historyItems.map((item) => { const contributors = item.contributors; const hasContributors = contributors && contributors.length > 0; const names = hasContributors ? contributors.map((c) => c.name).join(", ") : item.lastUpdatedBy?.name; return { value: item.id, label: formattedDate(new Date(item.createdAt)), userName: names, }; }), [historyItems], ); useHistoryReset(pageId); const { canRestore, confirmRestore } = useHistoryRestore(); const { currentChangeIndex, handlePrevChange, handleNextChange } = useDiffNavigation(scrollViewportRef); useEffect(() => { if (historyItems.length > 0 && !activeHistoryId) { setActiveHistoryId(historyItems[0].id); setActiveHistoryPrevId(historyItems[1]?.id ?? ""); } }, [ historyItems, activeHistoryId, setActiveHistoryId, setActiveHistoryPrevId, ]); const handleDropdownScroll = useCallback(() => { const viewport = dropdownViewportRef.current; if (!viewport || !hasNextPage || isFetchingNextPage) return; const { scrollTop, scrollHeight, clientHeight } = viewport; const isNearBottom = scrollTop + clientHeight >= scrollHeight - 50; if (isNearBottom) { fetchNextPage(); } }, [fetchNextPage, hasNextPage, isFetchingNextPage]); const handleSelectVersion = useCallback( (value: string | null) => { if (!value) return; const index = historyItems.findIndex((item) => item.id === value); if (index >= 0) { setActiveHistoryId(value); setActiveHistoryPrevId(historyItems[index + 1]?.id ?? ""); } }, [historyItems, setActiveHistoryId, setActiveHistoryPrevId], ); if (isLoading) { return null; } return (