2022-03-27 15:10:47 +02:00
|
|
|
import * as React from 'react';
|
2022-03-30 20:11:18 +02:00
|
|
|
import {useEffect, useRef, useState} from 'react';
|
2022-03-27 15:10:47 +02:00
|
|
|
import {NotificationItem} from "./Notifications";
|
|
|
|
import theme from "./theme";
|
2022-04-01 17:34:53 +02:00
|
|
|
import {Checkbox, Chip, FormControl, FormControlLabel, InputLabel, Link, Select, useMediaQuery} from "@mui/material";
|
2022-03-27 15:10:47 +02:00
|
|
|
import TextField from "@mui/material/TextField";
|
|
|
|
import priority1 from "../img/priority-1.svg";
|
|
|
|
import priority2 from "../img/priority-2.svg";
|
|
|
|
import priority3 from "../img/priority-3.svg";
|
|
|
|
import priority4 from "../img/priority-4.svg";
|
|
|
|
import priority5 from "../img/priority-5.svg";
|
|
|
|
import Dialog from "@mui/material/Dialog";
|
|
|
|
import DialogTitle from "@mui/material/DialogTitle";
|
|
|
|
import DialogContent from "@mui/material/DialogContent";
|
|
|
|
import Button from "@mui/material/Button";
|
|
|
|
import Typography from "@mui/material/Typography";
|
2022-03-29 04:54:27 +02:00
|
|
|
import IconButton from "@mui/material/IconButton";
|
|
|
|
import InsertEmoticonIcon from '@mui/icons-material/InsertEmoticon';
|
|
|
|
import {Close} from "@mui/icons-material";
|
|
|
|
import MenuItem from "@mui/material/MenuItem";
|
2022-04-07 02:04:27 +02:00
|
|
|
import {basicAuth, formatBytes, maybeWithBasicAuth, topicShortUrl, topicUrl, validTopic, validUrl} from "../app/utils";
|
2022-03-29 21:22:26 +02:00
|
|
|
import Box from "@mui/material/Box";
|
2022-04-04 14:40:54 +02:00
|
|
|
import AttachmentIcon from "./AttachmentIcon";
|
2022-03-29 21:22:26 +02:00
|
|
|
import DialogFooter from "./DialogFooter";
|
|
|
|
import api from "../app/Api";
|
2022-04-01 14:41:45 +02:00
|
|
|
import userManager from "../app/UserManager";
|
2022-04-04 16:04:01 +02:00
|
|
|
import EmojiPicker from "./EmojiPicker";
|
2022-04-08 16:44:35 +02:00
|
|
|
import {Trans, useTranslation} from "react-i18next";
|
2022-03-27 15:10:47 +02:00
|
|
|
|
2022-04-08 16:44:35 +02:00
|
|
|
const PublishDialog = (props) => {
|
|
|
|
const { t } = useTranslation();
|
2022-04-06 05:33:07 +02:00
|
|
|
const [baseUrl, setBaseUrl] = useState("");
|
|
|
|
const [topic, setTopic] = useState("");
|
2022-04-04 01:51:32 +02:00
|
|
|
const [message, setMessage] = useState("");
|
2022-04-06 05:33:07 +02:00
|
|
|
const [messageFocused, setMessageFocused] = useState(true);
|
2022-03-27 15:10:47 +02:00
|
|
|
const [title, setTitle] = useState("");
|
|
|
|
const [tags, setTags] = useState("");
|
2022-03-29 04:54:27 +02:00
|
|
|
const [priority, setPriority] = useState(3);
|
|
|
|
const [clickUrl, setClickUrl] = useState("");
|
|
|
|
const [attachUrl, setAttachUrl] = useState("");
|
2022-03-29 21:22:26 +02:00
|
|
|
const [attachFile, setAttachFile] = useState(null);
|
2022-03-29 04:54:27 +02:00
|
|
|
const [filename, setFilename] = useState("");
|
2022-03-31 18:03:36 +02:00
|
|
|
const [filenameEdited, setFilenameEdited] = useState(false);
|
2022-03-27 15:10:47 +02:00
|
|
|
const [email, setEmail] = useState("");
|
2022-03-29 04:54:27 +02:00
|
|
|
const [delay, setDelay] = useState("");
|
2022-04-01 17:34:53 +02:00
|
|
|
const [publishAnother, setPublishAnother] = useState(false);
|
2022-03-29 04:54:27 +02:00
|
|
|
|
2022-04-03 18:39:52 +02:00
|
|
|
const [showTopicUrl, setShowTopicUrl] = useState("");
|
2022-03-29 04:54:27 +02:00
|
|
|
const [showClickUrl, setShowClickUrl] = useState(false);
|
|
|
|
const [showAttachUrl, setShowAttachUrl] = useState(false);
|
|
|
|
const [showEmail, setShowEmail] = useState(false);
|
|
|
|
const [showDelay, setShowDelay] = useState(false);
|
|
|
|
|
2022-03-30 15:57:22 +02:00
|
|
|
const showAttachFile = !!attachFile && !showAttachUrl;
|
2022-03-29 21:22:26 +02:00
|
|
|
const attachFileInput = useRef();
|
2022-04-03 18:39:52 +02:00
|
|
|
const [attachFileError, setAttachFileError] = useState("");
|
2022-03-30 15:57:22 +02:00
|
|
|
|
2022-04-02 23:06:26 +02:00
|
|
|
const [activeRequest, setActiveRequest] = useState(null);
|
2022-04-04 01:51:32 +02:00
|
|
|
const [status, setStatus] = useState("");
|
2022-04-02 23:06:26 +02:00
|
|
|
const disabled = !!activeRequest;
|
|
|
|
|
2022-04-04 16:04:01 +02:00
|
|
|
const [emojiPickerAnchorEl, setEmojiPickerAnchorEl] = useState(null);
|
|
|
|
|
2022-04-04 04:42:56 +02:00
|
|
|
const [dropZone, setDropZone] = useState(false);
|
2022-04-03 18:39:52 +02:00
|
|
|
const [sendButtonEnabled, setSendButtonEnabled] = useState(true);
|
2022-03-29 21:22:26 +02:00
|
|
|
|
2022-04-04 04:42:56 +02:00
|
|
|
const open = !!props.openMode;
|
2022-03-27 15:10:47 +02:00
|
|
|
const fullScreen = useMediaQuery(theme.breakpoints.down('sm'));
|
2022-04-02 23:06:26 +02:00
|
|
|
|
2022-04-04 04:42:56 +02:00
|
|
|
useEffect(() => {
|
|
|
|
window.addEventListener('dragenter', () => {
|
|
|
|
props.onDragEnter();
|
|
|
|
setDropZone(true);
|
|
|
|
});
|
|
|
|
}, []);
|
|
|
|
|
2022-04-03 18:39:52 +02:00
|
|
|
useEffect(() => {
|
2022-04-06 05:33:07 +02:00
|
|
|
setBaseUrl(props.baseUrl);
|
|
|
|
setTopic(props.topic);
|
|
|
|
setShowTopicUrl(!props.baseUrl || !props.topic);
|
|
|
|
setMessageFocused(!!props.topic); // Focus message only if topic is set
|
|
|
|
}, [props.baseUrl, props.topic]);
|
2022-04-03 18:39:52 +02:00
|
|
|
|
|
|
|
useEffect(() => {
|
2022-04-06 19:27:32 +02:00
|
|
|
const valid = validUrl(baseUrl) && validTopic(topic) && !attachFileError;
|
2022-04-04 01:51:32 +02:00
|
|
|
setSendButtonEnabled(valid);
|
2022-04-06 05:33:07 +02:00
|
|
|
}, [baseUrl, topic, attachFileError]);
|
2022-04-02 23:06:26 +02:00
|
|
|
|
2022-04-04 01:51:32 +02:00
|
|
|
useEffect(() => {
|
|
|
|
setMessage(props.message);
|
|
|
|
}, [props.message]);
|
|
|
|
|
2022-03-27 15:10:47 +02:00
|
|
|
const handleSubmit = async () => {
|
2022-04-07 02:04:27 +02:00
|
|
|
const url = new URL(topicUrl(baseUrl, topic));
|
2022-03-29 21:22:26 +02:00
|
|
|
if (title.trim()) {
|
2022-04-07 02:04:27 +02:00
|
|
|
url.searchParams.append("title", title.trim());
|
2022-03-29 21:22:26 +02:00
|
|
|
}
|
|
|
|
if (tags.trim()) {
|
2022-04-07 02:04:27 +02:00
|
|
|
url.searchParams.append("tags", tags.trim());
|
2022-03-29 21:22:26 +02:00
|
|
|
}
|
|
|
|
if (priority && priority !== 3) {
|
2022-04-07 02:04:27 +02:00
|
|
|
url.searchParams.append("priority", priority.toString());
|
2022-03-29 21:22:26 +02:00
|
|
|
}
|
|
|
|
if (clickUrl.trim()) {
|
2022-04-07 02:04:27 +02:00
|
|
|
url.searchParams.append("click", clickUrl.trim());
|
2022-03-29 21:22:26 +02:00
|
|
|
}
|
|
|
|
if (attachUrl.trim()) {
|
2022-04-07 02:04:27 +02:00
|
|
|
url.searchParams.append("attach", attachUrl.trim());
|
2022-03-29 21:22:26 +02:00
|
|
|
}
|
|
|
|
if (filename.trim()) {
|
2022-04-07 02:04:27 +02:00
|
|
|
url.searchParams.append("filename", filename.trim());
|
2022-03-29 21:22:26 +02:00
|
|
|
}
|
|
|
|
if (email.trim()) {
|
2022-04-07 02:04:27 +02:00
|
|
|
url.searchParams.append("email", email.trim());
|
2022-03-29 21:22:26 +02:00
|
|
|
}
|
|
|
|
if (delay.trim()) {
|
2022-04-07 02:04:27 +02:00
|
|
|
url.searchParams.append("delay", delay.trim());
|
2022-03-29 21:22:26 +02:00
|
|
|
}
|
2022-04-01 14:41:45 +02:00
|
|
|
if (attachFile && message.trim()) {
|
2022-04-07 02:04:27 +02:00
|
|
|
url.searchParams.append("message", message.replaceAll("\n", "\\n").trim());
|
2022-04-01 14:41:45 +02:00
|
|
|
}
|
|
|
|
const body = (attachFile) ? attachFile : message;
|
2022-03-29 21:22:26 +02:00
|
|
|
try {
|
2022-04-01 14:41:45 +02:00
|
|
|
const user = await userManager.get(baseUrl);
|
2022-04-07 02:04:27 +02:00
|
|
|
const headers = maybeWithBasicAuth({}, user);
|
2022-04-01 14:41:45 +02:00
|
|
|
const progressFn = (ev) => {
|
|
|
|
if (ev.loaded > 0 && ev.total > 0) {
|
2022-04-08 16:44:35 +02:00
|
|
|
setStatus(t("publish_dialog_progress_uploading_detail", {
|
|
|
|
loaded: formatBytes(ev.loaded),
|
|
|
|
total: formatBytes(ev.total),
|
|
|
|
percent: Math.round(ev.loaded * 100.0 / ev.total)
|
|
|
|
}));
|
2022-04-01 14:41:45 +02:00
|
|
|
} else {
|
2022-04-08 16:44:35 +02:00
|
|
|
setStatus(t("publish_dialog_progress_uploading"));
|
2022-04-01 14:41:45 +02:00
|
|
|
}
|
|
|
|
};
|
2022-04-07 02:04:27 +02:00
|
|
|
const request = api.publishXHR(url, body, headers, progressFn);
|
2022-04-02 23:06:26 +02:00
|
|
|
setActiveRequest(request);
|
2022-04-01 14:41:45 +02:00
|
|
|
await request;
|
2022-04-01 17:34:53 +02:00
|
|
|
if (!publishAnother) {
|
|
|
|
props.onClose();
|
|
|
|
} else {
|
2022-04-08 16:44:35 +02:00
|
|
|
setStatus(t("publish_dialog_message_published"));
|
2022-04-04 01:51:32 +02:00
|
|
|
setActiveRequest(null);
|
2022-04-01 17:34:53 +02:00
|
|
|
}
|
2022-03-29 21:22:26 +02:00
|
|
|
} catch (e) {
|
2022-04-04 01:51:32 +02:00
|
|
|
setStatus(<Typography sx={{color: 'error.main', maxWidth: "400px"}}>{e}</Typography>);
|
|
|
|
setActiveRequest(null);
|
2022-03-29 21:22:26 +02:00
|
|
|
}
|
|
|
|
};
|
2022-04-02 23:06:26 +02:00
|
|
|
|
2022-04-03 18:39:52 +02:00
|
|
|
const checkAttachmentLimits = async (file) => {
|
|
|
|
try {
|
|
|
|
const stats = await api.userStats(baseUrl);
|
|
|
|
const fileSizeLimit = stats.attachmentFileSizeLimit ?? 0;
|
|
|
|
const remainingBytes = stats.visitorAttachmentBytesRemaining ?? 0;
|
2022-04-04 01:51:32 +02:00
|
|
|
const fileSizeLimitReached = fileSizeLimit > 0 && file.size > fileSizeLimit;
|
|
|
|
const quotaReached = remainingBytes > 0 && file.size > remainingBytes;
|
|
|
|
if (fileSizeLimitReached && quotaReached) {
|
2022-04-08 16:44:35 +02:00
|
|
|
return setAttachFileError(t("publish_dialog_attachment_limits_file_and_quota_reached", {
|
|
|
|
fileSizeLimit: formatBytes(fileSizeLimit),
|
|
|
|
remainingBytes: formatBytes(remainingBytes)
|
|
|
|
}));
|
2022-04-04 01:51:32 +02:00
|
|
|
} else if (fileSizeLimitReached) {
|
2022-04-08 16:44:35 +02:00
|
|
|
return setAttachFileError(t("publish_dialog_attachment_limits_file_reached", { fileSizeLimit: formatBytes(fileSizeLimit) }));
|
2022-04-04 01:51:32 +02:00
|
|
|
} else if (quotaReached) {
|
2022-04-08 16:44:35 +02:00
|
|
|
return setAttachFileError(t("publish_dialog_attachment_limits_quota_reached", { remainingBytes: formatBytes(remainingBytes) }));
|
2022-04-03 18:39:52 +02:00
|
|
|
}
|
|
|
|
setAttachFileError("");
|
|
|
|
} catch (e) {
|
|
|
|
console.log(`[SendDialog] Retrieving attachment limits failed`, e);
|
|
|
|
setAttachFileError(""); // Reset error (rely on server-side checking)
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2022-03-29 21:22:26 +02:00
|
|
|
const handleAttachFileClick = () => {
|
|
|
|
attachFileInput.current.click();
|
|
|
|
};
|
2022-04-02 23:06:26 +02:00
|
|
|
|
2022-04-03 18:39:52 +02:00
|
|
|
const handleAttachFileChanged = async (ev) => {
|
|
|
|
await updateAttachFile(ev.target.files[0]);
|
2022-03-27 15:10:47 +02:00
|
|
|
};
|
2022-04-02 23:06:26 +02:00
|
|
|
|
2022-04-03 18:39:52 +02:00
|
|
|
const handleAttachFileDrop = async (ev) => {
|
2022-04-02 23:06:26 +02:00
|
|
|
ev.preventDefault();
|
2022-04-04 04:42:56 +02:00
|
|
|
setDropZone(false);
|
2022-04-03 18:39:52 +02:00
|
|
|
await updateAttachFile(ev.dataTransfer.files[0]);
|
|
|
|
};
|
|
|
|
|
|
|
|
const updateAttachFile = async (file) => {
|
2022-04-02 23:06:26 +02:00
|
|
|
setAttachFile(file);
|
|
|
|
setFilename(file.name);
|
2022-04-04 01:51:32 +02:00
|
|
|
props.onResetOpenMode();
|
2022-04-03 18:39:52 +02:00
|
|
|
await checkAttachmentLimits(file);
|
2022-04-02 23:06:26 +02:00
|
|
|
};
|
|
|
|
|
2022-04-04 01:51:32 +02:00
|
|
|
const handleAttachFileDragLeave = () => {
|
2022-04-04 04:42:56 +02:00
|
|
|
setDropZone(false);
|
2022-04-08 16:44:35 +02:00
|
|
|
if (props.openMode === PublishDialog.OPEN_MODE_DRAG) {
|
2022-04-04 04:42:56 +02:00
|
|
|
props.onClose(); // Only close dialog if it was not open before dragging file in
|
2022-04-02 23:06:26 +02:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2022-04-04 16:04:01 +02:00
|
|
|
const handleEmojiClick = (ev) => {
|
|
|
|
setEmojiPickerAnchorEl(ev.currentTarget);
|
|
|
|
};
|
|
|
|
|
|
|
|
const handleEmojiPick = (emoji) => {
|
|
|
|
setTags(tags => (tags.trim()) ? `${tags.trim()}, ${emoji}` : emoji);
|
|
|
|
};
|
|
|
|
|
|
|
|
const handleEmojiClose = () => {
|
|
|
|
setEmojiPickerAnchorEl(null);
|
|
|
|
};
|
|
|
|
|
2022-04-08 16:44:35 +02:00
|
|
|
const priorities = {
|
|
|
|
1: { label: t("publish_dialog_priority_min"), file: priority1 },
|
|
|
|
2: { label: t("publish_dialog_priority_low"), file: priority2 },
|
|
|
|
3: { label: t("publish_dialog_priority_default"), file: priority3 },
|
|
|
|
4: { label: t("publish_dialog_priority_high"), file: priority4 },
|
|
|
|
5: { label: t("publish_dialog_priority_max"), file: priority5 }
|
|
|
|
};
|
|
|
|
|
2022-03-27 15:10:47 +02:00
|
|
|
return (
|
2022-04-04 01:51:32 +02:00
|
|
|
<>
|
|
|
|
{dropZone && <DropArea
|
|
|
|
onDrop={handleAttachFileDrop}
|
|
|
|
onDragLeave={handleAttachFileDragLeave}/>
|
|
|
|
}
|
2022-04-04 04:42:56 +02:00
|
|
|
<Dialog maxWidth="md" open={open} onClose={props.onCancel} fullScreen={fullScreen}>
|
2022-04-08 16:44:35 +02:00
|
|
|
<DialogTitle>{(baseUrl && topic) ? t("publish_dialog_title_topic", { topic: topicShortUrl(baseUrl, topic) }) : t("publish_dialog_title_no_topic")}</DialogTitle>
|
2022-04-04 01:51:32 +02:00
|
|
|
<DialogContent>
|
|
|
|
{dropZone && <DropBox/>}
|
|
|
|
{showTopicUrl &&
|
2022-04-06 05:33:07 +02:00
|
|
|
<ClosableRow closable={!!props.baseUrl && !!props.topic} disabled={disabled} onClose={() => {
|
|
|
|
setBaseUrl(props.baseUrl);
|
|
|
|
setTopic(props.topic);
|
2022-04-04 01:51:32 +02:00
|
|
|
setShowTopicUrl(false);
|
|
|
|
}}>
|
|
|
|
<TextField
|
|
|
|
margin="dense"
|
2022-04-08 16:44:35 +02:00
|
|
|
label={t("publish_dialog_base_url_label")}
|
|
|
|
placeholder={t("publish_dialog_base_url_placeholder")}
|
2022-04-06 05:33:07 +02:00
|
|
|
value={baseUrl}
|
|
|
|
onChange={ev => setBaseUrl(ev.target.value)}
|
|
|
|
disabled={disabled}
|
|
|
|
type="url"
|
|
|
|
variant="standard"
|
|
|
|
sx={{flexGrow: 1, marginRight: 1}}
|
|
|
|
/>
|
|
|
|
<TextField
|
|
|
|
margin="dense"
|
2022-04-08 16:44:35 +02:00
|
|
|
label={t("publish_dialog_topic_label")}
|
|
|
|
placeholder={t("publish_dialog_topic_placeholder")}
|
2022-04-06 05:33:07 +02:00
|
|
|
value={topic}
|
|
|
|
onChange={ev => setTopic(ev.target.value)}
|
2022-04-04 01:51:32 +02:00
|
|
|
disabled={disabled}
|
|
|
|
type="text"
|
|
|
|
variant="standard"
|
2022-04-06 05:33:07 +02:00
|
|
|
autoFocus={!messageFocused}
|
|
|
|
sx={{flexGrow: 1}}
|
2022-04-04 01:51:32 +02:00
|
|
|
/>
|
|
|
|
</ClosableRow>
|
|
|
|
}
|
2022-03-29 04:54:27 +02:00
|
|
|
<TextField
|
|
|
|
margin="dense"
|
2022-04-08 16:44:35 +02:00
|
|
|
label={t("publish_dialog_title_label")}
|
|
|
|
placeholder={t("publish_dialog_title_placeholder")}
|
2022-04-04 01:51:32 +02:00
|
|
|
value={title}
|
|
|
|
onChange={ev => setTitle(ev.target.value)}
|
2022-04-01 14:41:45 +02:00
|
|
|
disabled={disabled}
|
2022-03-29 04:54:27 +02:00
|
|
|
type="text"
|
2022-04-04 01:51:32 +02:00
|
|
|
fullWidth
|
2022-03-29 04:54:27 +02:00
|
|
|
variant="standard"
|
|
|
|
/>
|
2022-04-04 01:51:32 +02:00
|
|
|
<TextField
|
2022-03-29 04:54:27 +02:00
|
|
|
margin="dense"
|
2022-04-08 16:44:35 +02:00
|
|
|
label={t("publish_dialog_message_label")}
|
|
|
|
placeholder={t("publish_dialog_message_placeholder")}
|
2022-04-04 01:51:32 +02:00
|
|
|
value={message}
|
|
|
|
onChange={ev => setMessage(ev.target.value)}
|
|
|
|
disabled={disabled}
|
|
|
|
type="text"
|
|
|
|
variant="standard"
|
|
|
|
rows={5}
|
2022-04-06 05:33:07 +02:00
|
|
|
autoFocus={messageFocused}
|
2022-04-04 01:51:32 +02:00
|
|
|
fullWidth
|
|
|
|
multiline
|
|
|
|
/>
|
|
|
|
<div style={{display: 'flex'}}>
|
2022-04-04 16:04:01 +02:00
|
|
|
<EmojiPicker
|
|
|
|
anchorEl={emojiPickerAnchorEl}
|
|
|
|
onEmojiPick={handleEmojiPick}
|
|
|
|
onClose={handleEmojiClose}
|
|
|
|
/>
|
|
|
|
<DialogIconButton disabled={disabled} onClick={handleEmojiClick}>
|
|
|
|
<InsertEmoticonIcon/>
|
|
|
|
</DialogIconButton>
|
2022-03-29 04:54:27 +02:00
|
|
|
<TextField
|
|
|
|
margin="dense"
|
2022-04-08 16:44:35 +02:00
|
|
|
label={t("publish_dialog_tags_label")}
|
|
|
|
placeholder={t("publish_dialog_tags_placeholder")}
|
2022-04-04 01:51:32 +02:00
|
|
|
value={tags}
|
|
|
|
onChange={ev => setTags(ev.target.value)}
|
2022-04-01 14:41:45 +02:00
|
|
|
disabled={disabled}
|
2022-04-04 01:51:32 +02:00
|
|
|
type="text"
|
2022-03-29 04:54:27 +02:00
|
|
|
variant="standard"
|
2022-04-04 01:51:32 +02:00
|
|
|
sx={{flexGrow: 1, marginRight: 1}}
|
2022-03-29 04:54:27 +02:00
|
|
|
/>
|
2022-04-04 01:51:32 +02:00
|
|
|
<FormControl
|
2022-03-31 18:03:36 +02:00
|
|
|
variant="standard"
|
2022-03-29 21:22:26 +02:00
|
|
|
margin="dense"
|
2022-04-08 21:21:22 +02:00
|
|
|
sx={{minWidth: 170, maxWidth: 300, flexGrow: 1}}
|
2022-04-04 01:51:32 +02:00
|
|
|
>
|
|
|
|
<InputLabel/>
|
|
|
|
<Select
|
2022-04-08 16:44:35 +02:00
|
|
|
label={t("publish_dialog_priority_label")}
|
2022-04-04 01:51:32 +02:00
|
|
|
margin="dense"
|
|
|
|
value={priority}
|
|
|
|
onChange={(ev) => setPriority(ev.target.value)}
|
|
|
|
disabled={disabled}
|
|
|
|
>
|
|
|
|
{[5,4,3,2,1].map(priority =>
|
|
|
|
<MenuItem key={`priorityMenuItem${priority}`} value={priority}>
|
|
|
|
<div style={{ display: 'flex', alignItems: 'center' }}>
|
|
|
|
<img src={priorities[priority].file} style={{marginRight: "8px"}}/>
|
|
|
|
<div>{priorities[priority].label}</div>
|
|
|
|
</div>
|
|
|
|
</MenuItem>
|
|
|
|
)}
|
|
|
|
</Select>
|
|
|
|
</FormControl>
|
|
|
|
</div>
|
|
|
|
{showClickUrl &&
|
|
|
|
<ClosableRow disabled={disabled} onClose={() => {
|
|
|
|
setClickUrl("");
|
|
|
|
setShowClickUrl(false);
|
|
|
|
}}>
|
|
|
|
<TextField
|
|
|
|
margin="dense"
|
2022-04-08 16:44:35 +02:00
|
|
|
label={t("publish_dialog_click_label")}
|
|
|
|
placeholder={t("publish_dialog_click_placeholder")}
|
2022-04-04 01:51:32 +02:00
|
|
|
value={clickUrl}
|
|
|
|
onChange={ev => setClickUrl(ev.target.value)}
|
|
|
|
disabled={disabled}
|
|
|
|
type="url"
|
|
|
|
fullWidth
|
|
|
|
variant="standard"
|
|
|
|
/>
|
|
|
|
</ClosableRow>
|
|
|
|
}
|
|
|
|
{showEmail &&
|
|
|
|
<ClosableRow disabled={disabled} onClose={() => {
|
|
|
|
setEmail("");
|
|
|
|
setShowEmail(false);
|
|
|
|
}}>
|
|
|
|
<TextField
|
|
|
|
margin="dense"
|
2022-04-08 16:44:35 +02:00
|
|
|
label={t("publish_dialog_email_label")}
|
|
|
|
placeholder={t("publish_dialog_email_placeholder")}
|
2022-04-04 01:51:32 +02:00
|
|
|
value={email}
|
|
|
|
onChange={ev => setEmail(ev.target.value)}
|
|
|
|
disabled={disabled}
|
|
|
|
type="email"
|
|
|
|
variant="standard"
|
|
|
|
fullWidth
|
|
|
|
/>
|
|
|
|
</ClosableRow>
|
|
|
|
}
|
|
|
|
{showAttachUrl &&
|
|
|
|
<ClosableRow disabled={disabled} onClose={() => {
|
|
|
|
setAttachUrl("");
|
|
|
|
setFilename("");
|
|
|
|
setFilenameEdited(false);
|
|
|
|
setShowAttachUrl(false);
|
|
|
|
}}>
|
|
|
|
<TextField
|
|
|
|
margin="dense"
|
2022-04-08 16:44:35 +02:00
|
|
|
label={t("publish_dialog_attach_label")}
|
|
|
|
placeholder={t("publish_dialog_attach_placeholder")}
|
2022-04-04 01:51:32 +02:00
|
|
|
value={attachUrl}
|
|
|
|
onChange={ev => {
|
|
|
|
const url = ev.target.value;
|
|
|
|
setAttachUrl(url);
|
|
|
|
if (!filenameEdited) {
|
|
|
|
try {
|
|
|
|
const u = new URL(url);
|
|
|
|
const parts = u.pathname.split("/");
|
|
|
|
if (parts.length > 0) {
|
|
|
|
setFilename(parts[parts.length-1]);
|
|
|
|
}
|
|
|
|
} catch (e) {
|
|
|
|
// Do nothing
|
2022-03-31 18:03:36 +02:00
|
|
|
}
|
|
|
|
}
|
2022-04-04 01:51:32 +02:00
|
|
|
}}
|
|
|
|
disabled={disabled}
|
|
|
|
type="url"
|
|
|
|
variant="standard"
|
|
|
|
sx={{flexGrow: 5, marginRight: 1}}
|
|
|
|
/>
|
|
|
|
<TextField
|
|
|
|
margin="dense"
|
2022-04-08 16:44:35 +02:00
|
|
|
label={t("publish_dialog_filename_label")}
|
|
|
|
placeholder={t("publish_dialog_filename_placeholder")}
|
2022-04-04 01:51:32 +02:00
|
|
|
value={filename}
|
|
|
|
onChange={ev => {
|
|
|
|
setFilename(ev.target.value);
|
|
|
|
setFilenameEdited(true);
|
|
|
|
}}
|
|
|
|
disabled={disabled}
|
|
|
|
type="text"
|
|
|
|
variant="standard"
|
|
|
|
sx={{flexGrow: 1}}
|
|
|
|
/>
|
|
|
|
</ClosableRow>
|
|
|
|
}
|
|
|
|
<input
|
|
|
|
type="file"
|
|
|
|
ref={attachFileInput}
|
|
|
|
onChange={handleAttachFileChanged}
|
|
|
|
style={{ display: 'none' }}
|
|
|
|
/>
|
|
|
|
{showAttachFile && <AttachmentBox
|
|
|
|
file={attachFile}
|
|
|
|
filename={filename}
|
|
|
|
disabled={disabled}
|
|
|
|
error={attachFileError}
|
|
|
|
onChangeFilename={(f) => setFilename(f)}
|
|
|
|
onClose={() => {
|
|
|
|
setAttachFile(null);
|
|
|
|
setAttachFileError("");
|
|
|
|
setFilename("");
|
|
|
|
}}
|
|
|
|
/>}
|
|
|
|
{showDelay &&
|
|
|
|
<ClosableRow disabled={disabled} onClose={() => {
|
|
|
|
setDelay("");
|
|
|
|
setShowDelay(false);
|
|
|
|
}}>
|
|
|
|
<TextField
|
|
|
|
margin="dense"
|
2022-04-08 16:44:35 +02:00
|
|
|
label={t("publish_dialog_delay_label")}
|
2022-04-08 21:21:22 +02:00
|
|
|
placeholder={t("publish_dialog_delay_placeholder", {
|
|
|
|
unixTimestamp: "1649029748",
|
|
|
|
relativeTime: "30m",
|
|
|
|
naturalLanguage: "tomorrow, 9am"
|
|
|
|
})}
|
2022-04-04 01:51:32 +02:00
|
|
|
value={delay}
|
|
|
|
onChange={ev => setDelay(ev.target.value)}
|
|
|
|
disabled={disabled}
|
|
|
|
type="text"
|
|
|
|
variant="standard"
|
|
|
|
fullWidth
|
|
|
|
/>
|
|
|
|
</ClosableRow>
|
|
|
|
}
|
|
|
|
<Typography variant="body1" sx={{marginTop: 2, marginBottom: 1}}>
|
2022-04-08 16:44:35 +02:00
|
|
|
{t("publish_dialog_other_features")}
|
2022-04-04 01:51:32 +02:00
|
|
|
</Typography>
|
|
|
|
<div>
|
2022-04-08 16:44:35 +02:00
|
|
|
{!showClickUrl && <Chip clickable disabled={disabled} label={t("publish_dialog_chip_click_label")} onClick={() => setShowClickUrl(true)} sx={{marginRight: 1, marginBottom: 1}}/>}
|
|
|
|
{!showEmail && <Chip clickable disabled={disabled} label={t("publish_dialog_chip_email_label")} onClick={() => setShowEmail(true)} sx={{marginRight: 1, marginBottom: 1}}/>}
|
|
|
|
{!showAttachUrl && !showAttachFile && <Chip clickable disabled={disabled} label={t("publish_dialog_chip_attach_url_label")} onClick={() => setShowAttachUrl(true)} sx={{marginRight: 1, marginBottom: 1}}/>}
|
|
|
|
{!showAttachFile && !showAttachUrl && <Chip clickable disabled={disabled} label={t("publish_dialog_chip_attach_file_label")} onClick={() => handleAttachFileClick()} sx={{marginRight: 1, marginBottom: 1}}/>}
|
|
|
|
{!showDelay && <Chip clickable disabled={disabled} label={t("publish_dialog_chip_delay_label")} onClick={() => setShowDelay(true)} sx={{marginRight: 1, marginBottom: 1}}/>}
|
|
|
|
{!showTopicUrl && <Chip clickable disabled={disabled} label={t("publish_dialog_chip_topic_label")} onClick={() => setShowTopicUrl(true)} sx={{marginRight: 1, marginBottom: 1}}/>}
|
2022-04-04 01:51:32 +02:00
|
|
|
</div>
|
|
|
|
<Typography variant="body1" sx={{marginTop: 1, marginBottom: 1}}>
|
2022-04-08 16:44:35 +02:00
|
|
|
<Trans
|
|
|
|
i18nKey="publish_dialog_details_examples_description"
|
|
|
|
components={{
|
|
|
|
docsLink: <Link href="https://ntfy.sh/docs" target="_blank" rel="noopener"/>
|
|
|
|
}}
|
|
|
|
/>
|
2022-04-04 01:51:32 +02:00
|
|
|
</Typography>
|
|
|
|
</DialogContent>
|
|
|
|
<DialogFooter status={status}>
|
2022-04-08 16:44:35 +02:00
|
|
|
{activeRequest && <Button onClick={() => activeRequest.abort()}>{t("publish_dialog_button_cancel_sending")}</Button>}
|
2022-04-04 01:51:32 +02:00
|
|
|
{!activeRequest &&
|
|
|
|
<>
|
|
|
|
<FormControlLabel
|
2022-04-08 16:44:35 +02:00
|
|
|
label={t("publish_dialog_checkbox_publish_another")}
|
2022-04-04 01:51:32 +02:00
|
|
|
sx={{marginRight: 2}}
|
|
|
|
control={
|
|
|
|
<Checkbox size="small" checked={publishAnother} onChange={(ev) => setPublishAnother(ev.target.checked)} />
|
|
|
|
} />
|
2022-04-08 16:44:35 +02:00
|
|
|
<Button onClick={props.onClose}>{t("publish_dialog_button_cancel")}</Button>
|
|
|
|
<Button onClick={handleSubmit} disabled={!sendButtonEnabled}>{t("publish_dialog_button_send")}</Button>
|
2022-04-04 01:51:32 +02:00
|
|
|
</>
|
|
|
|
}
|
|
|
|
</DialogFooter>
|
|
|
|
</Dialog>
|
|
|
|
</>
|
2022-03-27 15:10:47 +02:00
|
|
|
);
|
|
|
|
};
|
|
|
|
|
2022-03-29 04:54:27 +02:00
|
|
|
const Row = (props) => {
|
|
|
|
return (
|
|
|
|
<div style={{display: 'flex'}}>
|
|
|
|
{props.children}
|
|
|
|
</div>
|
|
|
|
);
|
|
|
|
};
|
|
|
|
|
|
|
|
const ClosableRow = (props) => {
|
2022-04-05 01:56:21 +02:00
|
|
|
const closable = (props.hasOwnProperty("closable")) ? props.closable : true;
|
2022-03-29 04:54:27 +02:00
|
|
|
return (
|
|
|
|
<Row>
|
|
|
|
{props.children}
|
2022-04-05 01:56:21 +02:00
|
|
|
{closable && <DialogIconButton disabled={props.disabled} onClick={props.onClose} sx={{marginLeft: "6px"}}><Close/></DialogIconButton>}
|
2022-03-29 04:54:27 +02:00
|
|
|
</Row>
|
|
|
|
);
|
|
|
|
};
|
|
|
|
|
|
|
|
const DialogIconButton = (props) => {
|
2022-03-29 21:22:26 +02:00
|
|
|
const sx = props.sx || {};
|
2022-03-29 04:54:27 +02:00
|
|
|
return (
|
|
|
|
<IconButton
|
|
|
|
color="inherit"
|
|
|
|
size="large"
|
|
|
|
edge="start"
|
2022-03-29 21:22:26 +02:00
|
|
|
sx={{height: "45px", marginTop: "17px", ...sx}}
|
2022-03-29 04:54:27 +02:00
|
|
|
onClick={props.onClick}
|
2022-04-01 14:41:45 +02:00
|
|
|
disabled={props.disabled}
|
2022-03-29 04:54:27 +02:00
|
|
|
>
|
|
|
|
{props.children}
|
|
|
|
</IconButton>
|
|
|
|
);
|
|
|
|
};
|
|
|
|
|
2022-03-29 21:22:26 +02:00
|
|
|
const AttachmentBox = (props) => {
|
2022-04-08 16:44:35 +02:00
|
|
|
const { t } = useTranslation();
|
2022-03-29 21:22:26 +02:00
|
|
|
const file = props.file;
|
|
|
|
return (
|
2022-03-30 15:57:22 +02:00
|
|
|
<>
|
|
|
|
<Typography variant="body1" sx={{marginTop: 2}}>
|
2022-04-08 16:44:35 +02:00
|
|
|
{t("publish_dialog_attached_file_title")}
|
2022-03-29 21:22:26 +02:00
|
|
|
</Typography>
|
2022-03-30 15:57:22 +02:00
|
|
|
<Box sx={{
|
|
|
|
display: 'flex',
|
|
|
|
alignItems: 'center',
|
2022-03-30 20:11:18 +02:00
|
|
|
padding: 0.5,
|
2022-03-30 15:57:22 +02:00
|
|
|
borderRadius: '4px',
|
|
|
|
}}>
|
2022-04-04 14:40:54 +02:00
|
|
|
<AttachmentIcon type={file.type}/>
|
2022-04-03 18:39:52 +02:00
|
|
|
<Box sx={{ marginLeft: 1, textAlign: 'left' }}>
|
2022-03-31 18:03:36 +02:00
|
|
|
<ExpandingTextField
|
|
|
|
minWidth={140}
|
|
|
|
variant="body2"
|
2022-04-08 16:44:35 +02:00
|
|
|
placeholder={t("publish_dialog_attached_file_filename_placeholder")}
|
2022-03-30 20:11:18 +02:00
|
|
|
value={props.filename}
|
2022-03-31 18:03:36 +02:00
|
|
|
onChange={(ev) => props.onChangeFilename(ev.target.value)}
|
2022-04-01 14:41:45 +02:00
|
|
|
disabled={props.disabled}
|
2022-03-30 20:11:18 +02:00
|
|
|
/>
|
2022-03-30 15:57:22 +02:00
|
|
|
<br/>
|
2022-04-03 18:39:52 +02:00
|
|
|
<Typography variant="body2" sx={{ color: 'text.primary' }}>
|
|
|
|
{formatBytes(file.size)}
|
|
|
|
{props.error &&
|
|
|
|
<Typography component="span" sx={{ color: 'error.main' }}>
|
|
|
|
{" "}({props.error})
|
|
|
|
</Typography>
|
|
|
|
}
|
|
|
|
</Typography>
|
|
|
|
</Box>
|
2022-04-01 14:41:45 +02:00
|
|
|
<DialogIconButton disabled={props.disabled} onClick={props.onClose} sx={{marginLeft: "6px"}}><Close/></DialogIconButton>
|
2022-03-30 15:57:22 +02:00
|
|
|
</Box>
|
|
|
|
</>
|
2022-03-29 21:22:26 +02:00
|
|
|
);
|
|
|
|
};
|
|
|
|
|
2022-03-31 18:03:36 +02:00
|
|
|
const ExpandingTextField = (props) => {
|
|
|
|
const invisibleFieldRef = useRef();
|
|
|
|
const [textWidth, setTextWidth] = useState(props.minWidth);
|
|
|
|
const determineTextWidth = () => {
|
|
|
|
const boundingRect = invisibleFieldRef?.current?.getBoundingClientRect();
|
|
|
|
if (!boundingRect) {
|
|
|
|
return props.minWidth;
|
|
|
|
}
|
|
|
|
return (boundingRect.width >= props.minWidth) ? Math.round(boundingRect.width) : props.minWidth;
|
|
|
|
};
|
|
|
|
useEffect(() => {
|
|
|
|
setTextWidth(determineTextWidth() + 5);
|
|
|
|
}, [props.value]);
|
|
|
|
return (
|
|
|
|
<>
|
|
|
|
<Typography
|
|
|
|
ref={invisibleFieldRef}
|
|
|
|
component="span"
|
|
|
|
variant={props.variant}
|
2022-04-04 01:51:32 +02:00
|
|
|
sx={{position: "absolute", left: "-200%"}}
|
2022-03-31 18:03:36 +02:00
|
|
|
>
|
|
|
|
{props.value}
|
|
|
|
</Typography>
|
|
|
|
<TextField
|
|
|
|
margin="dense"
|
2022-04-08 16:44:35 +02:00
|
|
|
placeholder={props.placeholder}
|
2022-03-31 18:03:36 +02:00
|
|
|
value={props.value}
|
|
|
|
onChange={props.onChange}
|
|
|
|
type="text"
|
|
|
|
variant="standard"
|
|
|
|
sx={{ width: `${textWidth}px`, borderBottom: "none" }}
|
|
|
|
InputProps={{ style: { fontSize: theme.typography[props.variant].fontSize } }}
|
|
|
|
inputProps={{ style: { paddingBottom: 0, paddingTop: 0 } }}
|
2022-04-01 14:41:45 +02:00
|
|
|
disabled={props.disabled}
|
2022-03-31 18:03:36 +02:00
|
|
|
/>
|
|
|
|
</>
|
|
|
|
)
|
|
|
|
};
|
|
|
|
|
2022-04-04 01:51:32 +02:00
|
|
|
const DropArea = (props) => {
|
|
|
|
const allowDrag = (ev) => {
|
|
|
|
// This is where we could disallow certain files to be dragged in.
|
|
|
|
// For now we allow all files.
|
|
|
|
|
|
|
|
ev.dataTransfer.dropEffect = 'copy';
|
|
|
|
ev.preventDefault();
|
|
|
|
};
|
|
|
|
|
|
|
|
return (
|
|
|
|
<Box
|
|
|
|
sx={{
|
|
|
|
position: 'absolute',
|
|
|
|
left: 0,
|
|
|
|
top: 0,
|
|
|
|
right: 0,
|
|
|
|
bottom: 0,
|
|
|
|
zIndex: 10002,
|
|
|
|
}}
|
|
|
|
onDrop={props.onDrop}
|
|
|
|
onDragEnter={allowDrag}
|
|
|
|
onDragOver={allowDrag}
|
|
|
|
onDragLeave={props.onDragLeave}
|
|
|
|
/>
|
|
|
|
);
|
|
|
|
};
|
|
|
|
|
|
|
|
const DropBox = () => {
|
2022-04-08 16:44:35 +02:00
|
|
|
const { t } = useTranslation();
|
2022-04-04 01:51:32 +02:00
|
|
|
return (
|
|
|
|
<Box sx={{
|
|
|
|
position: 'absolute',
|
|
|
|
left: 0,
|
|
|
|
top: 0,
|
|
|
|
right: 0,
|
|
|
|
bottom: 0,
|
|
|
|
zIndex: 10000,
|
|
|
|
backgroundColor: "#ffffffbb"
|
|
|
|
}}>
|
|
|
|
<Box
|
|
|
|
sx={{
|
|
|
|
position: 'absolute',
|
|
|
|
border: '3px dashed #ccc',
|
|
|
|
borderRadius: '5px',
|
|
|
|
left: "40px",
|
|
|
|
top: "40px",
|
|
|
|
right: "40px",
|
|
|
|
bottom: "40px",
|
|
|
|
zIndex: 10001,
|
|
|
|
display: 'flex',
|
|
|
|
justifyContent: "center",
|
|
|
|
alignItems: "center",
|
|
|
|
}}
|
|
|
|
>
|
2022-04-08 16:44:35 +02:00
|
|
|
<Typography variant="h5">{t("publish_dialog_drop_file_here")}</Typography>
|
2022-04-04 01:51:32 +02:00
|
|
|
</Box>
|
|
|
|
</Box>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2022-04-08 16:44:35 +02:00
|
|
|
PublishDialog.OPEN_MODE_DEFAULT = "default";
|
|
|
|
PublishDialog.OPEN_MODE_DRAG = "drag";
|
2022-04-04 01:51:32 +02:00
|
|
|
|
2022-04-08 16:44:35 +02:00
|
|
|
export default PublishDialog;
|