import { get } from "lodash"; import { useState } from "react"; import BlockIcon from "@mui/icons-material/Block"; import IconCancel from "@mui/icons-material/Cancel"; import ClearIcon from "@mui/icons-material/Clear"; import DeleteSweepIcon from "@mui/icons-material/DeleteSweep"; import FileOpenIcon from "@mui/icons-material/FileOpen"; import LockIcon from "@mui/icons-material/Lock"; import LockOpenIcon from "@mui/icons-material/LockOpen"; import { Box, Dialog, DialogContent, DialogContentText, DialogTitle, Tooltip } from "@mui/material"; import { alpha, useTheme } from "@mui/material/styles"; import { BooleanInput, Button, ButtonProps, DateTimeInput, NumberInput, SaveButton, SimpleForm, Toolbar, ToolbarProps, useCreate, useDataProvider, useDelete, useNotify, useRecordContext, useRefresh, useTranslate, } from "react-admin"; import { useMutation } from "react-query"; import { Link } from "react-router-dom"; import { dateParser } from "./date"; import { DeleteMediaParams, SynapseDataProvider } from "../synapse/dataProvider"; import { getMediaUrl } from "../synapse/synapse"; import storage from "../storage"; const DeleteMediaDialog = ({ open, onClose, onSubmit }) => { const translate = useTranslate(); const DeleteMediaToolbar = (props: ToolbarProps) => ( } /> ); return ( {translate("delete_media.action.send")} {translate("delete_media.helper.send")} } onSubmit={onSubmit}> ); }; export const DeleteMediaButton = (props: ButtonProps) => { const theme = useTheme(); const [open, setOpen] = useState(false); const notify = useNotify(); const dataProvider = useDataProvider(); const { mutate: deleteMedia, isLoading } = useMutation( (values: DeleteMediaParams) => dataProvider.deleteMedia(values), { onSuccess: () => { notify("delete_media.action.send_success"); closeDialog(); }, onError: () => { notify("delete_media.action.send_failure", { type: "error", }); }, } ); const openDialog = () => setOpen(true); const closeDialog = () => setOpen(false); return ( <> ); }; export const ProtectMediaButton = (props: ButtonProps) => { const record = useRecordContext(); const translate = useTranslate(); const refresh = useRefresh(); const notify = useNotify(); const [create, { isLoading }] = useCreate(); const [deleteOne] = useDelete(); if (!record) return null; const handleProtect = () => { create( "protect_media", { data: record }, { onSuccess: () => { notify("resources.protect_media.action.send_success"); refresh(); }, onError: () => notify("resources.protect_media.action.send_failure", { type: "error", }), } ); }; const handleUnprotect = () => { deleteOne( "protect_media", { id: record.id }, { onSuccess: () => { notify("resources.protect_media.action.send_success"); refresh(); }, onError: () => notify("resources.protect_media.action.send_failure", { type: "error", }), } ); }; return ( /* Wrapping Tooltip with
https://github.com/marmelab/react-admin/issues/4349#issuecomment-578594735 */ <> {record.quarantined_by && (
{/* Button instead BooleanField for consistent appearance and position in the column */}
)} {record.safe_from_quarantine && (
)} {!record.safe_from_quarantine && !record.quarantined_by && (
)} ); }; export const QuarantineMediaButton = (props: ButtonProps) => { const record = useRecordContext(); const translate = useTranslate(); const refresh = useRefresh(); const notify = useNotify(); const [create, { isLoading }] = useCreate(); const [deleteOne] = useDelete(); if (!record) return null; const handleQuarantaine = () => { create( "quarantine_media", { data: record }, { onSuccess: () => { notify("resources.quarantine_media.action.send_success"); refresh(); }, onError: () => notify("resources.quarantine_media.action.send_failure", { type: "error", }), } ); }; const handleRemoveQuarantaine = () => { deleteOne( "quarantine_media", { id: record.id, previousData: record }, { onSuccess: () => { notify("resources.quarantine_media.action.send_success"); refresh(); }, onError: () => notify("resources.quarantine_media.action.send_failure", { type: "error", }), } ); }; return ( <> {record.safe_from_quarantine && (
)} {record.quarantined_by && (
)} {!record.safe_from_quarantine && !record.quarantined_by && (
)} ); }; export const ViewMediaButton = ({ media_id, label }) => { const translate = useTranslate(); const url = getMediaUrl(media_id); return ( {label} ); }; export const MediaIDField = ({ source }) => { const homeserver = storage.getItem("home_server"); const record = useRecordContext(); if (!record) return null; const src = get(record, source)?.toString(); if (!src) return null; return ; }; export const MXCField = ({ source }) => { const record = useRecordContext(); if (!record) return null; const src = get(record, source)?.toString(); if (!src) return null; const media_id = src.replace("mxc://", ""); return ; };