import { ChevronsUpDown, RefreshCcw } from "lucide-react"; import type { GetServerSideProps } from "next"; import { useState } from "react"; import toast from "react-hot-toast"; import { AreaRiservataLayout } from "~/components/Layout"; import { LoadingPage } from "~/components/loading"; import { Status500 } from "~/components/status-page"; import { StorageTable } from "~/components/tables/storage-table"; import { Button } from "~/components/ui/button"; import { Command, CommandEmpty, CommandGroup, CommandInput, CommandItem, CommandList, } from "~/components/ui/command"; import { Dialog, DialogContent, DialogDescription, DialogHeader, DialogTitle, } from "~/components/ui/dialog"; import { Popover, PopoverContent, PopoverTrigger, } from "~/components/ui/popover"; import { Separator } from "~/components/ui/separator"; import { UploadModal } from "~/components/upload_modal"; import { UserAvatar } from "~/components/user_avatar"; import { cn } from "~/lib/utils"; import type { NextPageWithLayout } from "~/pages/_app"; import { useTranslation } from "~/providers/I18nProvider"; import { StorageTableProvider, type StorageTable as StorageTableType, useStorageTable, } from "~/providers/StorageTableProvider"; import type { Storageindex, StorageindexId, } from "~/schemas/public/Storageindex"; import type { Users } from "~/schemas/public/Users"; import { TrpcAuthedFetchingIstance } from "~/server/utils/ssgHelper"; import { api } from "~/utils/api"; const AdminStorage: NextPageWithLayout = () => { const { data, isLoading, refetch } = api.storage.getAll.useQuery(); const { t } = useTranslation(); const utils = api.useUtils(); const { mutate: deleteFile } = api.storage.deleteStorage.useMutation({ onMutate: () => { const toastId = toast.loading(t.caricamento, { icon: "🪛", }); return { toastId }; }, onSuccess: async (_error, _variables, context) => { await utils.storage.getAll.invalidate(); toast.success(t.allegati.allegato_rimosso, { icon: "🗑️", id: context?.toastId, }); }, }); const handleDelete = (id: StorageindexId) => { deleteFile({ storageId: id }); }; const handleRefetch = async () => { await refetch(); }; if (isLoading) return ; if (!data) return ; //magari query amche file ownership per poter filtrare in tab se da admin o da utente o una lista di utenti a cui è visibile il file return (
); }; export const getServerSideProps = (async (context) => { const access_token = context.req.cookies.access_token; const helpers = await TrpcAuthedFetchingIstance({ access_token }); if (helpers) { await helpers.trpc.storage.getAll.prefetch(); return { props: { trpcState: helpers.trpc.dehydrate(), }, }; } return { props: {}, }; }) satisfies GetServerSideProps; export default AdminStorage; const StorageTableHeader = ({ handleRefetch, handleDelete, }: { handleRefetch: () => Promise; handleDelete: (id: StorageindexId) => void; }) => { const { table } = useStorageTable(); if (!table) return null; return (

Impostazioni

{ table.setRowSelection({}); }} />
); }; AdminStorage.getLayout = function getLayout(page) { return {page}; }; const Actions = ({ handleDelete, table, }: { handleDelete: (id: StorageindexId) => void; table: StorageTableType; }) => { const { selectedRows } = useStorageTable(); const [visbOpen, setVisbOpen] = useState(false); const deleteSelected = () => { selectedRows.forEach((row) => handleDelete(row.id)); table.resetRowSelection(); }; return (
{visbOpen && ( )}
); }; const VisibComponent = ({ allegati, open, setOpen, }: { allegati: Storageindex[]; open: boolean; setOpen: (v: boolean) => void; }) => { const utils = api.useUtils(); const { data, isLoading } = api.storage.getStorageVisibility.useQuery({ storageIds: allegati.map((a) => a.id), }); const { data: users } = api.users.getUsers.useQuery(); const { mutate: remove } = api.storage.removeStorageVisibility.useMutation({ onMutate: () => { const toastId = toast.loading("Rimozione utente in corso", { icon: "🪛", }); return { toastId }; }, onSuccess: async (_error, _variables, context) => { await utils.storage.getStorageVisibility.invalidate(); toast.success("Utente rimosso", { icon: "🗑️", id: context?.toastId, }); }, }); const { mutate: add } = api.storage.addStorageVisibility.useMutation({ onMutate: () => { const toastId = toast.loading("Aggiunta utente in corso", { icon: "🪛", }); return { toastId }; }, onSuccess: async (_error, _variables, context) => { await utils.storage.getStorageVisibility.invalidate(); toast.success("Utente aggiunto", { icon: "👍", id: context?.toastId, }); }, }); const { mutate: reset } = api.storage.resetStorageVisibility.useMutation({ onMutate: () => { const toastId = toast.loading("Reset visibilità in corso", { icon: "🪛", }); return { toastId }; }, onSuccess: async (_error, _variables, context) => { await utils.storage.getStorageVisibility.invalidate(); toast.success("Reset effettuato", { icon: "👍", id: context?.toastId, }); }, }); return ( <> Modifica Visibilità Modifica la visibilità degli allegati selezionati
{isLoading ? ( ) : ( <>

Seleziona utente:

{ for (const allegato of allegati) { add({ storageId: allegato.id, userId: user.id }); } }} users={users || []} />
{data?.map((allegato, i) => (

File: {allegato.filename}

Visibile a:

{allegato.users_storages.length === 0 ? (

Nessuno

) : (
    {allegato.users_storages.map((user) => (
  • ))}
)}
))} )}
); }; const UtentiCombo = ({ users, onSelect, }: { users: Users[]; onSelect: (user: Users) => void; }) => { const [open, setOpen] = useState(false); return ( Nessun utente. {users.map((user) => ( { // biome-ignore lint/style/noNonNullAssertion: onSelect(users.find((u) => u.id === currentValue)!); setOpen(false); }} value={user.id} > {user.username} ))} ); };