import { Trans } from "@lingui/react/macro" import { Button, Code, Group, Modal, Slider, Stack, Text } from "@mantine/core" import { useState } from "react" import { Constants } from "@/app/constants" import { setMarkAllAsReadConfirmationDialogOpen } from "@/app/entries/slice" import { markAllEntries } from "@/app/entries/thunks" import { useAppDispatch, useAppSelector } from "@/app/store" import { selectNextUnreadTreeItem } from "@/app/tree/thunks" export function MarkAllAsReadConfirmationDialog() { const [threshold, setThreshold] = useState(0) const open = useAppSelector(state => state.entries.markAllAsReadConfirmationDialogOpen) const source = useAppSelector(state => state.entries.source) const sourceLabel = useAppSelector(state => state.entries.sourceLabel) const entriesTimestamp = useAppSelector(state => state.entries.timestamp) ?? Date.now() const markAllAsReadNavigateToNextUnread = useAppSelector(state => state.user.settings?.markAllAsReadNavigateToNextUnread) const dispatch = useAppDispatch() const onConfirm = async () => { dispatch(setMarkAllAsReadConfirmationDialogOpen(false)) await dispatch( markAllEntries({ sourceType: source.type, req: { id: source.id, read: true, olderThan: Date.now() - threshold * 24 * 60 * 60 * 1000, insertedBefore: entriesTimestamp, }, }) ) const isAllCategorySelected = source.type === "category" && source.id === Constants.categories.all.id if (markAllAsReadNavigateToNextUnread && !isAllCategorySelected) await dispatch(selectNextUnreadTreeItem({ direction: "forward" })) } return ( dispatch(setMarkAllAsReadConfirmationDialogOpen(false))} title={Mark all entries as read} > {threshold === 0 && ( Are you sure you want to mark all entries of {sourceLabel} as read? )} {threshold > 0 && ( Are you sure you want to mark entries older than {threshold} days of {sourceLabel} as read? )} e.key === "Enter" && onConfirm()} /> ) }