//
// Copyright (c) 2025 rustmailer.com (https://rustmailer.com)
//
// This file is part of the Bichon Email Archiving Project
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU Affero General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU Affero General Public License for more details.
//
// You should have received a copy of the GNU Affero General Public License
// along with this program. If not, see .
import { cn, dateFnsLocaleMap, formatBytes } from "@/lib/utils"
import { formatDistanceToNow } from "date-fns"
import { MailIcon, MoreVertical, Paperclip, TagIcon, Trash2 } from "lucide-react"
import { Skeleton } from "@/components/ui/skeleton"
import { EmailEnvelope } from "@/api"
import { useMailboxContext } from "../context"
import { Checkbox } from "@/components/ui/checkbox"
import { MailBulkActions } from "./bulk-actions"
import { Badge } from "@/components/ui/badge"
import { useTranslation } from 'react-i18next'
import { enUS } from "date-fns/locale"
import { DropdownMenu, DropdownMenuContent, DropdownMenuItem, DropdownMenuTrigger } from "@/components/ui/dropdown-menu"
import { Button } from "@/components/ui/button"
interface MailListProps {
items: EmailEnvelope[]
isLoading: boolean
}
export function MailList({
items,
isLoading,
}: MailListProps) {
const { t, i18n } = useTranslation()
const { currentEnvelope, setCurrentEnvelope, setDeleteIds, setOpen, selected, setSelected } = useMailboxContext()
const locale = dateFnsLocaleMap[i18n.language.toLowerCase()] ?? enUS;
const handleDelete = (envelope: EmailEnvelope) => {
setDeleteIds(new Set([envelope.id]))
setOpen("move-to-trash")
}
const totalSelected = selected.size;
const handleToggleAll = () => {
const total = selected.size;
if (total === items.length && items.length > 0) {
setSelected(new Set());
} else {
const set = new Set();
for (const item of items) {
set.add(item.id);
}
setSelected(set);
}
}
const hasSelected = (mailId: number) => {
return selected.has(mailId);
}
const toggleSelected = (id: number) => {
setSelected(prev => {
const next = new Set(prev)
if (next.has(id)) {
next.delete(id)
} else {
next.add(id)
}
return next
});
}
if (isLoading) {
return (