import { differenceInDays, differenceInMinutes, format } from "date-fns"; import { Paperclip } from "lucide-react"; import Link from "next/link"; import { ExtIcon } from "~/components/area-riservata/allegati"; import { Credenza, CredenzaBody, CredenzaClose, CredenzaContent, CredenzaDescription, CredenzaFooter, CredenzaHeader, CredenzaTitle, CredenzaTrigger, } from "~/components/custom_ui/credenza"; import { LoadingPage } from "~/components/loading"; import { Button, buttonVariants } from "~/components/ui/button"; import { UploadComponent } from "~/components/upload_modal"; import { cn } from "~/lib/utils"; import { useChatContext } from "~/providers/ChatProvider"; import { useTranslation } from "~/providers/I18nProvider"; import { api } from "~/utils/api"; export const ChatAttachments = () => { const { session, userinfo } = useChatContext(); const { t } = useTranslation(); const { data: attachments, isLoading: attachmentsLoading, refetch, } = api.storage.retrieveUserFileData.useQuery( { userId: userinfo.id }, { refetchOnMount: true }, ); return ( o && refetch()}> Allegati Chat Attachments
{attachmentsLoading ? ( ) : ( <> {!attachments || attachments.length === 0 ? (

{t.allegati.nessun_allegato}

) : (

    {t.allegati.allegati_recenti}

    {attachments?.map((file, idx) => { if (idx >= 3) return null; return (
  • idx }`} > console.log("touching")} target="_blank" > {TimeSince(new Date(file.uploadedAt))}
  • ); })}
)} )}
{t.allegati.i_tuoi_allegati} {attachments && attachments.length > 0 && ( {attachments.length} )}
); }; const TimeSince = (date: Date) => { const minsSince = Math.abs(differenceInMinutes(date, new Date())); const daysSince = Math.abs(differenceInDays(date, new Date())); const relevantMinsIncrements = [1, 3, 5, 10, 15, 30, 60, 120]; if (daysSince === 0) { if (minsSince < 1) { return "ora"; } const closestToNow = relevantMinsIncrements.reduce((prev, curr) => { return Math.abs(curr - minsSince) < Math.abs(prev - minsSince) ? curr : prev; }); if (closestToNow === 60) { return "~1 ora fa"; } if (closestToNow === 120 && minsSince < 121) { return "~2 ore fa"; } return `~${closestToNow} min fa`; } if (daysSince === 1) { return "ieri"; } if (daysSince < 30) { return `${daysSince} giorni fa`; } return format(date, "dd/MM/yyyy"); };