import { format } from "date-fns"; import { Download, File, FileArchive, FileAudio, FileCode, FileSpreadsheet, FileText, FileVideo, ImageIcon, MoreVertical, Pencil, Trash2, TriangleAlert, } from "lucide-react"; import Link from "next/link"; import { type FormEvent, useState } from "react"; import toast from "react-hot-toast"; import Input from "~/components/custom_ui/input"; import { LoadingPage } from "~/components/loading"; import { Status500 } from "~/components/status-page"; import { Button } from "~/components/ui/button"; import { Dialog, DialogContent, DialogDescription, DialogHeader, DialogTitle, } from "~/components/ui/dialog"; import { DropdownMenu, DropdownMenuContent, DropdownMenuItem, DropdownMenuTrigger, } from "~/components/ui/dropdown-menu"; import { Separator } from "~/components/ui/separator"; import { UploadModal } from "~/components/upload_modal"; import { handleDownload } from "~/hooks/filesHooks"; import { cn } from "~/lib/utils"; import { useTranslation } from "~/providers/I18nProvider"; import type { Storageindex, StorageindexId, } from "~/schemas/public/Storageindex"; import type { UsersId } from "~/schemas/public/Users"; import { api } from "~/utils/api"; export const AllegatiComp = ({ userId, isAdmin, }: { userId: UsersId; isAdmin: boolean; }) => { const { t } = useTranslation(); const { data: allegati, isLoading: isLoadingAllegati } = api.storage.getUserStorage.useQuery({ userId: userId }); const { mutateAsync: getToken } = api.storage.getStorageToken.useMutation(); const { mutate: deleteFile } = api.storage.deleteUserStorage.useMutation({ onMutate: () => { const toastId = toast.loading(t.caricamento, { icon: "🪛", }); return { toastId }; }, onSuccess: async (_error, _variables, context) => { await utils.storage.getUserStorage.invalidate({ userId: userId, }); toast.success(t.allegati.allegato_rimosso, { icon: "🗑️", id: context?.toastId, }); }, }); const { mutate: renameFile } = api.storage.renameFile.useMutation({ onMutate: () => { setEditOpen(false); const toastId = toast.loading(t.caricamento, { icon: "🪛", }); return { toastId }; }, onSuccess: async (_error, _variables, context) => { await utils.storage.getUserStorage.invalidate({ userId: userId, }); toast.success(t.successo, { icon: "👍", id: context?.toastId, }); }, }); const [editData, setEditData] = useState<{ id: StorageindexId; filename: string; } | null>(null); const [editOpen, setEditOpen] = useState(false); const handleEditSubmit = (e: FormEvent) => { e.preventDefault(); const form = e.currentTarget; const formData = new FormData(form); if (!formData.get("id") || !formData.get("filename")) { return; } renameFile({ fileId: formData.get("id") as StorageindexId, name: formData.get("filename") as string, }); }; const utils = api.useUtils(); if (isLoadingAllegati) return ; if (!allegati) return ; const from_admin = allegati.filter((a) => a.from_admin); const not_from_admin = allegati.filter((a) => !a.from_admin); return (

{t.allegati.title}

{t.allegati.da_info}

{from_admin && ( deleteFile({ fileId: id, userId })} download_onClick={(file) => handleDownload({ file, getToken: async () => await getToken(), }) } files={from_admin} isAdmin={isAdmin} selectHandler={(id, filename) => { setEditData({ filename, id }); setEditOpen(true); }} /> )}
{" "} Max 5 MB per file
{t.importante}:
{t.allegati.disclamer}
{not_from_admin && ( deleteFile({ fileId: id, userId })} download_onClick={(file) => handleDownload({ file, getToken: async () => await getToken(), }) } files={not_from_admin} isAdmin={isAdmin} selectHandler={(id, filename) => { setEditData({ filename, id }); setEditOpen(true); }} /> )}
); }; export const ExtIcon = ({ ext, className, }: { ext: string | null | undefined; className?: string; }) => { if (!ext) return ; if (ext.includes("pdf")) { return ; } else if ( ext.includes("image") || ["jpg", "jpeg", "png", "gif", "svg", "webp"].some((ext) => ext.includes(ext), ) ) { return ; } else if ( ["html", "css", "js", "jsx", "ts", "tsx", "json", "xml"].some((ext) => ext.includes(ext), ) ) { return ; } else if (["xls", "xlsx", "csv"].some((ext) => ext.includes(ext))) { return ( ); } else if ( ["zip", "rar", "7z", "tar", "gz"].some((ext) => ext.includes(ext)) ) { return ; } else if (["mp3", "wav", "ogg", "flac"].some((ext) => ext.includes(ext))) { return ; } else if ( ["mp4", "avi", "mov", "wmv", "webm"].some((ext) => ext.includes(ext)) ) { return ; } else { return ; } }; const EditFileDialog = ({ open, onOpenChange, data, handleEditSubmit, }: { open: boolean; onOpenChange: (status: boolean) => void; data: { id: StorageindexId; filename: string; } | null; handleEditSubmit: (e: FormEvent) => void; }) => { const { t } = useTranslation(); return ( {t.modifica} Edit {data && (
)}
); }; interface FileListProps { files: Storageindex[]; delete_onClick: (id: StorageindexId) => void; download_onClick: (file: Storageindex) => void; isAdmin: boolean; selectHandler: (id: StorageindexId, filename: string) => void; } function FileList({ files, delete_onClick, download_onClick, isAdmin, selectHandler, }: FileListProps) { return (
File
Data
Azioni
{files.map((file) => (
{file.filename}
{format(file.created_at, "d MMM yyyy")}
{isAdmin && ( selectHandler(file.id, file.filename || "") } > Rinomina delete_onClick(file.id)} > Elimina )}
))}
); }