import { format, isBefore, isSameYear, startOfDay } from "date-fns"; import { HiOutlinePaperClip } from "react-icons/hi"; import { HiBars3BottomLeft, HiChatBubbleLeft, HiOutlineClock, } from "react-icons/hi2"; import { twMerge } from "tailwind-merge"; import Avatar from "~/components/Avatar"; import Badge from "~/components/Badge"; import CircularProgress from "~/components/CircularProgress"; import LabelIcon from "~/components/LabelIcon"; import { useLocalisation } from "~/hooks/useLocalisation"; import { getAvatarUrl } from "~/utils/helpers"; const Card = ({ title, labels, members, checklists, description, comments, attachments, dueDate, }: { title: string; labels: { name: string; colourCode: string | null }[]; members: { publicId: string; email: string; user: { name: string | null; email: string; image: string | null } | null; }[]; checklists: { publicId: string; name: string; items: { publicId: string; title: string; completed: boolean; index: number; }[]; }[]; description: string | null; comments: { publicId: string }[]; attachments?: { publicId: string }[]; dueDate?: Date | null; }) => { const { dateLocale } = useLocalisation(); const showYear = dueDate ? !isSameYear(dueDate, new Date()) : false; const isOverdue = dueDate ? isBefore(dueDate, startOfDay(new Date())) : false; const completedItems = checklists.reduce((acc, checklist) => { return acc + checklist.items.filter((item) => item.completed).length; }, 0); const totalItems = checklists.reduce((acc, checklist) => { return acc + checklist.items.length; }, 0); const progress = totalItems > 0 ? Math.round((completedItems / totalItems) * 100) : 0; const hasDescription = description && description.replace(/<[^>]*>/g, "").trim().length > 0; const hasAttachments = attachments && attachments.length > 0; const hasDueDate = !!dueDate; return (
{title} {labels.length || members.length || checklists.length > 0 || hasDescription || comments.length > 0 || hasDueDate || hasAttachments ? (
{labels.map((label) => ( } /> ))}
{hasDescription && (
)} {hasDueDate && dueDate && (
{format(dueDate, showYear ? "do MMM yyyy" : "do MMM", { locale: dateLocale, })}
)} {comments.length > 0 && (
)} {hasAttachments && (
)}
{checklists.length > 0 && (
{completedItems}/{totalItems}
)} {members.length > 0 && (
{members.map(({ user, email }) => { const avatarUrl = user?.image ? getAvatarUrl(user.image) : undefined; return ( ); })}
)}
) : null}
); }; export default Card;