import { useParams } from "react-router-dom"; import { useGetSpaceBySlugQuery } from "@/features/space/queries/space-query"; import { Container, Title, Table, Group, ActionIcon, Text, Alert, Stack, Menu, } from "@mantine/core"; import { IconInfoCircle, IconDots, IconRestore, IconTrash, IconFileDescription, } from "@tabler/icons-react"; import { useDeletedPagesQuery, useRestorePageMutation, useDeletePageMutation, } from "@/features/page/queries/page-query"; import { modals } from "@mantine/modals"; import { useTranslation } from "react-i18next"; import { formattedDate } from "@/lib/time"; import { useState } from "react"; import TrashPageContentModal from "@/features/page/trash/components/trash-page-content-modal"; import { UserInfo } from "@/components/common/user-info.tsx"; import Paginate from "@/components/common/paginate.tsx"; import { useCursorPaginate } from "@/hooks/use-cursor-paginate"; export default function Trash() { const { t } = useTranslation(); const { spaceSlug } = useParams(); const { cursor, goNext, goPrev } = useCursorPaginate(); const { data: space } = useGetSpaceBySlugQuery(spaceSlug); const { data: deletedPages, isLoading } = useDeletedPagesQuery(space?.id, { cursor, limit: 50 }); const restorePageMutation = useRestorePageMutation(); const deletePageMutation = useDeletePageMutation(); const [selectedPage, setSelectedPage] = useState<{ title: string; content: any; } | null>(null); const [modalOpened, setModalOpened] = useState(false); const handleRestorePage = async (pageId: string) => { await restorePageMutation.mutateAsync(pageId); }; const handleDeletePage = async (pageId: string) => { await deletePageMutation.mutateAsync(pageId); }; const openDeleteModal = (pageId: string, pageTitle: string) => { modals.openConfirmModal({ title: t("Are you sure you want to delete this page?"), children: ( {t( "Are you sure you want to permanently delete '{{title}}'? This action cannot be undone.", { title: pageTitle || "Untitled" }, )} ), centered: true, labels: { confirm: t("Delete"), cancel: t("Cancel") }, confirmProps: { color: "red" }, onConfirm: () => handleDeletePage(pageId), }); }; const openRestoreModal = (pageId: string, pageTitle: string) => { modals.openConfirmModal({ title: t("Restore page"), children: ( {t("Restore '{{title}}' and its sub-pages?", { title: pageTitle || "Untitled", })} ), centered: true, labels: { confirm: t("Restore"), cancel: t("Cancel") }, confirmProps: { color: "blue" }, onConfirm: () => handleRestorePage(pageId), }); }; const hasPages = deletedPages && deletedPages.items.length > 0; const handlePageClick = (page: any) => { setSelectedPage({ title: page.title, content: page.content }); setModalOpened(true); }; return ( {t("Trash")} } variant="light" color="red"> {t("Pages in trash will be permanently deleted after 30 days.")} {isLoading || !deletedPages ? ( <> ) : hasPages ? ( {t("Page")} {t("Deleted by")} {t("Deleted at")} {deletedPages.items.map((page) => ( handlePageClick(page)} > {page.icon || ( )}
{page.title || t("Untitled")}
{formattedDate(page.deletedAt)} } onClick={() => openRestoreModal(page.id, page.title) } > {t("Restore")} } onClick={() => openDeleteModal(page.id, page.title)} > {t("Delete")}
))}
) : ( {t("No pages in trash")} )} {deletedPages && deletedPages.items.length > 0 && ( goNext(deletedPages.meta?.nextCursor)} onPrev={goPrev} /> )}
{selectedPage && ( setModalOpened(false)} pageTitle={selectedPage.title} pageContent={selectedPage.content} /> )}
); }