import { Download, Pen, Trash } from "lucide-react"; import type { GetServerSideProps } from "next"; import Head from "next/head"; import Link from "next/link"; import { useRouter } from "next/router"; import { type FormEvent, useState } from "react"; import toast from "react-hot-toast"; import z from "zod"; import { AllegatoIframe } from "~/components/allegato-iframe"; import { Confirm } from "~/components/confirm"; import { DocNotFoundPage } from "~/components/doc_not_found"; import { AreaRiservataLayout } from "~/components/Layout"; import { LoadingPage } from "~/components/loading"; import { Button } from "~/components/ui/button"; import { Dialog, DialogContent, DialogDescription, DialogHeader, DialogTitle, DialogTrigger, } from "~/components/ui/dialog"; import { Input } from "~/components/ui/input"; import { getStorageUrl } from "~/lib/storage_utils"; import type { NextPageWithLayout } from "~/pages/_app"; import { useTranslation } from "~/providers/I18nProvider"; import { useEnforcedSession } from "~/providers/SessionProvider"; import { TrpcAuthedFetchingIstance } from "~/server/utils/ssgHelper"; import { api } from "~/utils/api"; type AllegatoViewProps = { allegatoId: string; }; /** * Pagina di visualizzazione dell'allegato: /area-riservata/allegato/[allegatoId] */ const AllegatoView: NextPageWithLayout = ({ allegatoId, }: AllegatoViewProps) => { const { t } = useTranslation(); const session = useEnforcedSession(); const { data: fileInfos, isLoading } = api.storage.getFileInfos.useQuery({ storageId: allegatoId, }); const utils = api.useUtils(); const router = useRouter(); const { mutate: deleteFile } = api.storage.deleteFile.useMutation({ onMutate: () => { const toastId = toast.loading(t.caricamento, { icon: "🪛", }); return { toastId }; }, onSuccess: async (_error, _variables, context) => { await utils.storage.retrieveUserFileData.invalidate(); toast.success(t.allegati.allegato_rimosso, { icon: "🗑️", id: context?.toastId, }); await router.push("/area-riservata/admin/storage"); }, }); if (isLoading) return ; if (!fileInfos) return ; return ( <> Infoalloggi.it | {fileInfos.originalName}
{fileInfos.originalName} caricato il {new Date(fileInfos.uploadedAt).toLocaleString("it")} {session.user.isAdmin && ( deleteFile({ storageId: allegatoId })} title="Elimina allegato" > )}
); }; AllegatoView.getLayout = function getLayout(page) { return {page}; }; export const getServerSideProps = (async (context) => { const allegatoId = context.params?.allegatoId; if (!allegatoId || typeof allegatoId !== "string") { return { notFound: true, }; } const parsedStorageId = z.string().safeParse(allegatoId); if (!parsedStorageId.success) { return { notFound: true, }; } const access_token = context.req.cookies.access_token; const helper = await TrpcAuthedFetchingIstance({ access_token }); if (helper) { await helper.trpc.storage.getFileInfos.prefetch({ storageId: parsedStorageId.data, }); return { props: { allegatoId: parsedStorageId.data, trpcState: helper.trpc.dehydrate(), }, }; } return { redirect: { destination: "/500", permanent: false, }, }; }) satisfies GetServerSideProps; export default AllegatoView; const RinominaDialog = ({ allegatoId, filename, }: { allegatoId: string; filename: string | null; }) => { const { t } = useTranslation(); const utils = api.useUtils(); const [editOpen, setEditOpen] = useState(false); 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.getFileInfos.invalidate({ storageId: allegatoId, }); toast.success(t.successo, { icon: "👍", id: context?.toastId, }); }, }); const handleEditSubmit = (e: FormEvent) => { e.preventDefault(); const form = e.currentTarget; const formData = new FormData(form); if (!formData.get("filename")) { return; } renameFile({ fileId: allegatoId, name: formData.get("filename") as string, }); }; return ( {t.modifica} modifica
); };