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 = ({ mxcURL, label, mimetype }) => {
if (!mimetype.startsWith("image/")) {
return (
<>
{label}
>
);
}
const translate = useTranslate();
const openFileInNewTab = (blobURL: string) => {
const anchorElement = document.createElement("a");
anchorElement.href = blobURL;
anchorElement.target = "_blank";
document.body.appendChild(anchorElement);
anchorElement.click();
document.body.removeChild(anchorElement);
setTimeout(() => URL.revokeObjectURL(blobURL), 10);
};
const previewFile = async () => {
const response = await fetchAuthenticatedMedia(mxcURL, "original");
const blob = await response.blob();
const blobURL = URL.createObjectURL(blob);
openFileInNewTab(blobURL);
};
return (
<>
{label}
>
);
};
export const MediaIDField = ({ source }) => {
const record = useRecordContext();
if (!record) {
return null;
}
const homeserver = storage.getItem("home_server");
const mediaID = get(record, source)?.toString();
if (!mediaID) {
return null;
}
const mxcURL = `mxc://${homeserver}/${mediaID}`;
return
;
};
export const ReportMediaContent = ({ source }) => {
const record = useRecordContext();
if (!record) {
return null;
}
const mxcURL = get(record, source)?.toString();
if (!mxcURL) {
return null;
}
return ;
};