ntfy/web/src/components/PublishDialog.js

748 lines
32 KiB
JavaScript
Raw Normal View History

import * as React from 'react';
2022-03-31 07:11:18 +13:00
import {useEffect, useRef, useState} from 'react';
import theme from "./theme";
2022-04-02 04:34:53 +13:00
import {Checkbox, Chip, FormControl, FormControlLabel, InputLabel, Link, Select, useMediaQuery} from "@mui/material";
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 15:54:27 +13: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";
import {
formatBytes,
maybeWithAuth,
withBasicAuth,
topicShortUrl,
topicUrl,
validTopic,
validUrl
} from "../app/utils";
2022-03-30 08:22:26 +13:00
import Box from "@mui/material/Box";
2022-04-05 00:40:54 +12:00
import AttachmentIcon from "./AttachmentIcon";
2022-03-30 08:22:26 +13:00
import DialogFooter from "./DialogFooter";
2022-12-26 05:59:44 +13:00
import api from "../app/Api";
2022-04-02 01:41:45 +13:00
import userManager from "../app/UserManager";
2022-04-05 02:04:01 +12:00
import EmojiPicker from "./EmojiPicker";
2022-04-09 02:44:35 +12:00
import {Trans, useTranslation} from "react-i18next";
2022-12-20 03:59:32 +13:00
import session from "../app/Session";
import routes from "./routes";
2022-12-26 05:59:44 +13:00
import accountApi, {UnauthorizedError} from "../app/AccountApi";
2022-04-09 02:44:35 +12:00
const PublishDialog = (props) => {
const { t } = useTranslation();
2022-04-06 15:33:07 +12:00
const [baseUrl, setBaseUrl] = useState("");
const [topic, setTopic] = useState("");
2022-04-04 11:51:32 +12:00
const [message, setMessage] = useState("");
2022-04-06 15:33:07 +12:00
const [messageFocused, setMessageFocused] = useState(true);
const [title, setTitle] = useState("");
const [tags, setTags] = useState("");
2022-03-29 15:54:27 +13:00
const [priority, setPriority] = useState(3);
const [clickUrl, setClickUrl] = useState("");
const [attachUrl, setAttachUrl] = useState("");
2022-03-30 08:22:26 +13:00
const [attachFile, setAttachFile] = useState(null);
2022-03-29 15:54:27 +13:00
const [filename, setFilename] = useState("");
2022-04-01 05:03:36 +13:00
const [filenameEdited, setFilenameEdited] = useState(false);
const [email, setEmail] = useState("");
2022-03-29 15:54:27 +13:00
const [delay, setDelay] = useState("");
2022-04-02 04:34:53 +13:00
const [publishAnother, setPublishAnother] = useState(false);
2022-03-29 15:54:27 +13:00
2022-04-04 04:39:52 +12:00
const [showTopicUrl, setShowTopicUrl] = useState("");
2022-03-29 15:54:27 +13:00
const [showClickUrl, setShowClickUrl] = useState(false);
const [showAttachUrl, setShowAttachUrl] = useState(false);
const [showEmail, setShowEmail] = useState(false);
const [showDelay, setShowDelay] = useState(false);
2022-03-31 02:57:22 +13:00
const showAttachFile = !!attachFile && !showAttachUrl;
2022-03-30 08:22:26 +13:00
const attachFileInput = useRef();
2022-04-04 04:39:52 +12:00
const [attachFileError, setAttachFileError] = useState("");
2022-03-31 02:57:22 +13:00
const [activeRequest, setActiveRequest] = useState(null);
2022-04-04 11:51:32 +12:00
const [status, setStatus] = useState("");
const disabled = !!activeRequest;
2022-04-05 02:04:01 +12:00
const [emojiPickerAnchorEl, setEmojiPickerAnchorEl] = useState(null);
2022-04-04 14:42:56 +12:00
const [dropZone, setDropZone] = useState(false);
2022-04-04 04:39:52 +12:00
const [sendButtonEnabled, setSendButtonEnabled] = useState(true);
2022-03-30 08:22:26 +13:00
2022-04-04 14:42:56 +12:00
const open = !!props.openMode;
const fullScreen = useMediaQuery(theme.breakpoints.down('sm'));
2022-04-04 14:42:56 +12:00
useEffect(() => {
window.addEventListener('dragenter', () => {
props.onDragEnter();
setDropZone(true);
});
}, []);
2022-04-04 04:39:52 +12:00
useEffect(() => {
2022-04-06 15:33:07 +12: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-04 04:39:52 +12:00
useEffect(() => {
const valid = validUrl(baseUrl) && validTopic(topic) && !attachFileError;
2022-04-04 11:51:32 +12:00
setSendButtonEnabled(valid);
2022-04-06 15:33:07 +12:00
}, [baseUrl, topic, attachFileError]);
2022-04-04 11:51:32 +12:00
useEffect(() => {
setMessage(props.message);
}, [props.message]);
const updateBaseUrl = (newVal) => {
if (validUrl(newVal)) {
setBaseUrl(newVal.replace(/\/$/, '')); // strip traililng slash after https?://
} else {
setBaseUrl(newVal);
}
};
const handleSubmit = async () => {
const url = new URL(topicUrl(baseUrl, topic));
2022-03-30 08:22:26 +13:00
if (title.trim()) {
url.searchParams.append("title", title.trim());
2022-03-30 08:22:26 +13:00
}
if (tags.trim()) {
url.searchParams.append("tags", tags.trim());
2022-03-30 08:22:26 +13:00
}
if (priority && priority !== 3) {
url.searchParams.append("priority", priority.toString());
2022-03-30 08:22:26 +13:00
}
if (clickUrl.trim()) {
url.searchParams.append("click", clickUrl.trim());
2022-03-30 08:22:26 +13:00
}
if (attachUrl.trim()) {
url.searchParams.append("attach", attachUrl.trim());
2022-03-30 08:22:26 +13:00
}
if (filename.trim()) {
url.searchParams.append("filename", filename.trim());
2022-03-30 08:22:26 +13:00
}
if (email.trim()) {
url.searchParams.append("email", email.trim());
2022-03-30 08:22:26 +13:00
}
if (delay.trim()) {
url.searchParams.append("delay", delay.trim());
2022-03-30 08:22:26 +13:00
}
2022-04-02 01:41:45 +13:00
if (attachFile && message.trim()) {
url.searchParams.append("message", message.replaceAll("\n", "\\n").trim());
2022-04-02 01:41:45 +13:00
}
const body = (attachFile) ? attachFile : message;
2022-03-30 08:22:26 +13:00
try {
2022-04-02 01:41:45 +13:00
const user = await userManager.get(baseUrl);
const headers = maybeWithAuth({}, user);
2022-04-02 01:41:45 +13:00
const progressFn = (ev) => {
if (ev.loaded > 0 && ev.total > 0) {
2022-04-09 02:44:35 +12: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-02 01:41:45 +13:00
} else {
2022-04-09 02:44:35 +12:00
setStatus(t("publish_dialog_progress_uploading"));
2022-04-02 01:41:45 +13:00
}
};
const request = api.publishXHR(url, body, headers, progressFn);
setActiveRequest(request);
2022-04-02 01:41:45 +13:00
await request;
2022-04-02 04:34:53 +13:00
if (!publishAnother) {
props.onClose();
} else {
2022-04-09 02:44:35 +12:00
setStatus(t("publish_dialog_message_published"));
2022-04-04 11:51:32 +12:00
setActiveRequest(null);
2022-04-02 04:34:53 +13:00
}
2022-03-30 08:22:26 +13:00
} catch (e) {
2022-04-04 11:51:32 +12:00
setStatus(<Typography sx={{color: 'error.main', maxWidth: "400px"}}>{e}</Typography>);
setActiveRequest(null);
2022-03-30 08:22:26 +13:00
}
};
2022-04-04 04:39:52 +12:00
const checkAttachmentLimits = async (file) => {
try {
2022-12-26 05:59:44 +13:00
const account = await accountApi.get();
2022-12-20 03:59:32 +13:00
const fileSizeLimit = account.limits.attachment_file_size ?? 0;
const remainingBytes = account.stats.attachment_total_size_remaining;
2022-04-04 11:51:32 +12:00
const fileSizeLimitReached = fileSizeLimit > 0 && file.size > fileSizeLimit;
const quotaReached = remainingBytes > 0 && file.size > remainingBytes;
if (fileSizeLimitReached && quotaReached) {
2022-04-09 02:44:35 +12:00
return setAttachFileError(t("publish_dialog_attachment_limits_file_and_quota_reached", {
fileSizeLimit: formatBytes(fileSizeLimit),
remainingBytes: formatBytes(remainingBytes)
}));
2022-04-04 11:51:32 +12:00
} else if (fileSizeLimitReached) {
2022-04-09 02:44:35 +12:00
return setAttachFileError(t("publish_dialog_attachment_limits_file_reached", { fileSizeLimit: formatBytes(fileSizeLimit) }));
2022-04-04 11:51:32 +12:00
} else if (quotaReached) {
2022-04-09 02:44:35 +12:00
return setAttachFileError(t("publish_dialog_attachment_limits_quota_reached", { remainingBytes: formatBytes(remainingBytes) }));
2022-04-04 04:39:52 +12:00
}
setAttachFileError("");
} catch (e) {
console.log(`[PublishDialog] Retrieving attachment limits failed`, e);
if ((e instanceof UnauthorizedError)) {
session.resetAndRedirect(routes.login);
} else {
setAttachFileError(""); // Reset error (rely on server-side checking)
}
2022-04-04 04:39:52 +12:00
}
};
2022-03-30 08:22:26 +13:00
const handleAttachFileClick = () => {
attachFileInput.current.click();
};
2022-04-04 04:39:52 +12:00
const handleAttachFileChanged = async (ev) => {
await updateAttachFile(ev.target.files[0]);
};
2022-04-04 04:39:52 +12:00
const handleAttachFileDrop = async (ev) => {
ev.preventDefault();
2022-04-04 14:42:56 +12:00
setDropZone(false);
2022-04-04 04:39:52 +12:00
await updateAttachFile(ev.dataTransfer.files[0]);
};
const updateAttachFile = async (file) => {
setAttachFile(file);
setFilename(file.name);
2022-04-04 11:51:32 +12:00
props.onResetOpenMode();
2022-04-04 04:39:52 +12:00
await checkAttachmentLimits(file);
};
2022-04-04 11:51:32 +12:00
const handleAttachFileDragLeave = () => {
2022-04-04 14:42:56 +12:00
setDropZone(false);
2022-04-09 02:44:35 +12:00
if (props.openMode === PublishDialog.OPEN_MODE_DRAG) {
2022-04-04 14:42:56 +12:00
props.onClose(); // Only close dialog if it was not open before dragging file in
}
};
2022-04-05 02:04:01 +12:00
const handleEmojiClick = (ev) => {
setEmojiPickerAnchorEl(ev.currentTarget);
};
const handleEmojiPick = (emoji) => {
setTags(tags => (tags.trim()) ? `${tags.trim()}, ${emoji}` : emoji);
};
const handleEmojiClose = () => {
setEmojiPickerAnchorEl(null);
};
2022-04-09 02:44:35 +12: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 }
};
return (
2022-04-04 11:51:32 +12:00
<>
{dropZone && <DropArea
onDrop={handleAttachFileDrop}
onDragLeave={handleAttachFileDragLeave}/>
}
2022-04-04 14:42:56 +12:00
<Dialog maxWidth="md" open={open} onClose={props.onCancel} fullScreen={fullScreen}>
2022-04-09 02:44:35 +12:00
<DialogTitle>{(baseUrl && topic) ? t("publish_dialog_title_topic", { topic: topicShortUrl(baseUrl, topic) }) : t("publish_dialog_title_no_topic")}</DialogTitle>
2022-04-04 11:51:32 +12:00
<DialogContent>
{dropZone && <DropBox/>}
{showTopicUrl &&
2022-05-03 12:02:21 +12:00
<ClosableRow closable={!!props.baseUrl && !!props.topic} disabled={disabled} closeLabel={t("publish_dialog_topic_reset")} onClose={() => {
2022-04-06 15:33:07 +12:00
setBaseUrl(props.baseUrl);
setTopic(props.topic);
2022-04-04 11:51:32 +12:00
setShowTopicUrl(false);
}}>
<TextField
margin="dense"
2022-04-09 02:44:35 +12:00
label={t("publish_dialog_base_url_label")}
placeholder={t("publish_dialog_base_url_placeholder")}
2022-04-06 15:33:07 +12:00
value={baseUrl}
onChange={ev => updateBaseUrl(ev.target.value)}
2022-04-06 15:33:07 +12:00
disabled={disabled}
type="url"
variant="standard"
sx={{flexGrow: 1, marginRight: 1}}
2022-05-04 06:53:07 +12:00
inputProps={{
"aria-label": t("publish_dialog_base_url_label")
}}
2022-04-06 15:33:07 +12:00
/>
<TextField
margin="dense"
2022-04-09 02:44:35 +12:00
label={t("publish_dialog_topic_label")}
placeholder={t("publish_dialog_topic_placeholder")}
2022-04-06 15:33:07 +12:00
value={topic}
onChange={ev => setTopic(ev.target.value)}
2022-04-04 11:51:32 +12:00
disabled={disabled}
type="text"
variant="standard"
2022-04-06 15:33:07 +12:00
autoFocus={!messageFocused}
sx={{flexGrow: 1}}
2022-05-04 06:53:07 +12:00
inputProps={{
"aria-label": t("publish_dialog_topic_label")
}}
2022-04-04 11:51:32 +12:00
/>
</ClosableRow>
}
2022-03-29 15:54:27 +13:00
<TextField
margin="dense"
2022-04-09 02:44:35 +12:00
label={t("publish_dialog_title_label")}
placeholder={t("publish_dialog_title_placeholder")}
2022-04-04 11:51:32 +12:00
value={title}
onChange={ev => setTitle(ev.target.value)}
2022-04-02 01:41:45 +13:00
disabled={disabled}
2022-03-29 15:54:27 +13:00
type="text"
2022-04-04 11:51:32 +12:00
fullWidth
2022-03-29 15:54:27 +13:00
variant="standard"
2022-05-04 06:53:07 +12:00
inputProps={{
"aria-label": t("publish_dialog_title_label")
}}
2022-03-29 15:54:27 +13:00
/>
2022-04-04 11:51:32 +12:00
<TextField
2022-03-29 15:54:27 +13:00
margin="dense"
2022-04-09 02:44:35 +12:00
label={t("publish_dialog_message_label")}
placeholder={t("publish_dialog_message_placeholder")}
2022-04-04 11:51:32 +12:00
value={message}
onChange={ev => setMessage(ev.target.value)}
disabled={disabled}
type="text"
variant="standard"
rows={5}
2022-04-06 15:33:07 +12:00
autoFocus={messageFocused}
2022-04-04 11:51:32 +12:00
fullWidth
multiline
2022-05-04 06:53:07 +12:00
inputProps={{
"aria-label": t("publish_dialog_message_label")
}}
2022-04-04 11:51:32 +12:00
/>
<div style={{display: 'flex'}}>
2022-04-05 02:04:01 +12:00
<EmojiPicker
anchorEl={emojiPickerAnchorEl}
onEmojiPick={handleEmojiPick}
onClose={handleEmojiClose}
/>
2022-05-03 11:30:29 +12:00
<DialogIconButton disabled={disabled} onClick={handleEmojiClick} aria-label={t("publish_dialog_emoji_picker_show")}>
2022-04-05 02:04:01 +12:00
<InsertEmoticonIcon/>
</DialogIconButton>
2022-03-29 15:54:27 +13:00
<TextField
margin="dense"
2022-04-09 02:44:35 +12:00
label={t("publish_dialog_tags_label")}
placeholder={t("publish_dialog_tags_placeholder")}
2022-04-04 11:51:32 +12:00
value={tags}
onChange={ev => setTags(ev.target.value)}
2022-04-02 01:41:45 +13:00
disabled={disabled}
2022-04-04 11:51:32 +12:00
type="text"
2022-03-29 15:54:27 +13:00
variant="standard"
2022-04-04 11:51:32 +12:00
sx={{flexGrow: 1, marginRight: 1}}
2022-05-04 06:53:07 +12:00
inputProps={{
"aria-label": t("publish_dialog_tags_label")
}}
2022-03-29 15:54:27 +13:00
/>
2022-04-04 11:51:32 +12:00
<FormControl
2022-04-01 05:03:36 +13:00
variant="standard"
2022-03-30 08:22:26 +13:00
margin="dense"
sx={{minWidth: 170, maxWidth: 300, flexGrow: 1}}
2022-04-04 11:51:32 +12:00
>
<InputLabel/>
<Select
2022-04-09 02:44:35 +12:00
label={t("publish_dialog_priority_label")}
2022-04-04 11:51:32 +12:00
margin="dense"
value={priority}
onChange={(ev) => setPriority(ev.target.value)}
disabled={disabled}
2022-05-04 06:53:07 +12:00
inputProps={{
"aria-label": t("publish_dialog_priority_label")
}}
2022-04-04 11:51:32 +12:00
>
{[5,4,3,2,1].map(priority =>
2022-05-03 11:30:29 +12:00
<MenuItem key={`priorityMenuItem${priority}`} value={priority} aria-label={t("notifications_priority_x", { priority: priority })}>
2022-04-04 11:51:32 +12:00
<div style={{ display: 'flex', alignItems: 'center' }}>
2022-05-03 11:30:29 +12:00
<img src={priorities[priority].file} style={{marginRight: "8px"}} alt={t("notifications_priority_x", { priority: priority })}/>
2022-04-04 11:51:32 +12:00
<div>{priorities[priority].label}</div>
</div>
</MenuItem>
)}
</Select>
</FormControl>
</div>
{showClickUrl &&
2022-05-03 12:02:21 +12:00
<ClosableRow disabled={disabled} closeLabel={t("publish_dialog_click_reset")} onClose={() => {
2022-04-04 11:51:32 +12:00
setClickUrl("");
setShowClickUrl(false);
}}>
<TextField
margin="dense"
2022-04-09 02:44:35 +12:00
label={t("publish_dialog_click_label")}
placeholder={t("publish_dialog_click_placeholder")}
2022-04-04 11:51:32 +12:00
value={clickUrl}
onChange={ev => setClickUrl(ev.target.value)}
disabled={disabled}
type="url"
fullWidth
variant="standard"
2022-05-04 06:53:07 +12:00
inputProps={{
"aria-label": t("publish_dialog_click_label")
}}
2022-04-04 11:51:32 +12:00
/>
</ClosableRow>
}
{showEmail &&
2022-05-03 12:02:21 +12:00
<ClosableRow disabled={disabled} closeLabel={t("publish_dialog_email_reset")} onClose={() => {
2022-04-04 11:51:32 +12:00
setEmail("");
setShowEmail(false);
}}>
<TextField
margin="dense"
2022-04-09 02:44:35 +12:00
label={t("publish_dialog_email_label")}
placeholder={t("publish_dialog_email_placeholder")}
2022-04-04 11:51:32 +12:00
value={email}
onChange={ev => setEmail(ev.target.value)}
disabled={disabled}
type="email"
variant="standard"
fullWidth
2022-05-04 06:53:07 +12:00
inputProps={{
"aria-label": t("publish_dialog_email_label")
}}
2022-04-04 11:51:32 +12:00
/>
</ClosableRow>
}
{showAttachUrl &&
2022-05-03 12:02:21 +12:00
<ClosableRow disabled={disabled} closeLabel={t("publish_dialog_attach_reset")} onClose={() => {
2022-04-04 11:51:32 +12:00
setAttachUrl("");
setFilename("");
setFilenameEdited(false);
setShowAttachUrl(false);
}}>
<TextField
margin="dense"
2022-04-09 02:44:35 +12:00
label={t("publish_dialog_attach_label")}
placeholder={t("publish_dialog_attach_placeholder")}
2022-04-04 11:51:32 +12: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-04-01 05:03:36 +13:00
}
}
2022-04-04 11:51:32 +12:00
}}
disabled={disabled}
type="url"
variant="standard"
sx={{flexGrow: 5, marginRight: 1}}
2022-05-04 06:53:07 +12:00
inputProps={{
"aria-label": t("publish_dialog_attach_label")
}}
2022-04-04 11:51:32 +12:00
/>
<TextField
margin="dense"
2022-04-09 02:44:35 +12:00
label={t("publish_dialog_filename_label")}
placeholder={t("publish_dialog_filename_placeholder")}
2022-04-04 11:51:32 +12:00
value={filename}
onChange={ev => {
setFilename(ev.target.value);
setFilenameEdited(true);
}}
disabled={disabled}
type="text"
variant="standard"
sx={{flexGrow: 1}}
2022-05-04 06:53:07 +12:00
inputProps={{
"aria-label": t("publish_dialog_filename_label")
}}
2022-04-04 11:51:32 +12:00
/>
</ClosableRow>
}
<input
type="file"
ref={attachFileInput}
onChange={handleAttachFileChanged}
style={{ display: 'none' }}
2022-05-04 06:53:07 +12:00
aria-hidden={true}
2022-04-04 11:51:32 +12:00
/>
{showAttachFile && <AttachmentBox
file={attachFile}
filename={filename}
disabled={disabled}
error={attachFileError}
onChangeFilename={(f) => setFilename(f)}
onClose={() => {
setAttachFile(null);
setAttachFileError("");
setFilename("");
}}
/>}
{showDelay &&
2022-05-03 12:02:21 +12:00
<ClosableRow disabled={disabled} closeLabel={t("publish_dialog_delay_reset")} onClose={() => {
2022-04-04 11:51:32 +12:00
setDelay("");
setShowDelay(false);
}}>
<TextField
margin="dense"
2022-04-09 02:44:35 +12:00
label={t("publish_dialog_delay_label")}
placeholder={t("publish_dialog_delay_placeholder", {
unixTimestamp: "1649029748",
relativeTime: "30m",
naturalLanguage: "tomorrow, 9am"
})}
2022-04-04 11:51:32 +12:00
value={delay}
onChange={ev => setDelay(ev.target.value)}
disabled={disabled}
type="text"
variant="standard"
fullWidth
2022-05-04 06:53:07 +12:00
inputProps={{
"aria-label": t("publish_dialog_delay_label")
}}
2022-04-04 11:51:32 +12:00
/>
</ClosableRow>
}
<Typography variant="body1" sx={{marginTop: 2, marginBottom: 1}}>
2022-04-09 02:44:35 +12:00
{t("publish_dialog_other_features")}
2022-04-04 11:51:32 +12:00
</Typography>
<div>
2022-05-03 11:30:29 +12:00
{!showClickUrl && <Chip clickable disabled={disabled} label={t("publish_dialog_chip_click_label")} aria-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")} aria-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")} aria-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")} aria-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")} aria-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")} aria-label={t("publish_dialog_chip_topic_label")} onClick={() => setShowTopicUrl(true)} sx={{marginRight: 1, marginBottom: 1}}/>}
2022-04-04 11:51:32 +12:00
</div>
<Typography variant="body1" sx={{marginTop: 1, marginBottom: 1}}>
2022-04-09 02:44:35 +12:00
<Trans
i18nKey="publish_dialog_details_examples_description"
components={{
docsLink: <Link href="https://ntfy.sh/docs" target="_blank" rel="noopener"/>
}}
/>
2022-04-04 11:51:32 +12:00
</Typography>
</DialogContent>
<DialogFooter status={status}>
2022-04-09 02:44:35 +12:00
{activeRequest && <Button onClick={() => activeRequest.abort()}>{t("publish_dialog_button_cancel_sending")}</Button>}
2022-04-04 11:51:32 +12:00
{!activeRequest &&
<>
<FormControlLabel
2022-04-09 02:44:35 +12:00
label={t("publish_dialog_checkbox_publish_another")}
2022-04-04 11:51:32 +12:00
sx={{marginRight: 2}}
control={
2022-05-03 11:30:29 +12:00
<Checkbox
size="small"
checked={publishAnother}
onChange={(ev) => setPublishAnother(ev.target.checked)}
inputProps={{
"aria-label": t("publish_dialog_checkbox_publish_another")
}} />
2022-04-04 11:51:32 +12:00
} />
2022-04-09 02:44:35 +12: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 11:51:32 +12:00
</>
}
</DialogFooter>
</Dialog>
</>
);
};
2022-03-29 15:54:27 +13:00
const Row = (props) => {
return (
2022-05-03 11:30:29 +12:00
<div style={{display: 'flex'}} role="row">
2022-03-29 15:54:27 +13:00
{props.children}
</div>
);
};
const ClosableRow = (props) => {
2022-04-05 11:56:21 +12:00
const closable = (props.hasOwnProperty("closable")) ? props.closable : true;
2022-03-29 15:54:27 +13:00
return (
<Row>
{props.children}
2022-05-03 12:02:21 +12:00
{closable &&
<DialogIconButton disabled={props.disabled} onClick={props.onClose} sx={{marginLeft: "6px"}} aria-label={props.closeLabel}>
<Close/>
</DialogIconButton>
}
2022-03-29 15:54:27 +13:00
</Row>
);
};
const DialogIconButton = (props) => {
2022-03-30 08:22:26 +13:00
const sx = props.sx || {};
2022-03-29 15:54:27 +13:00
return (
<IconButton
color="inherit"
size="large"
edge="start"
2022-03-30 08:22:26 +13:00
sx={{height: "45px", marginTop: "17px", ...sx}}
2022-03-29 15:54:27 +13:00
onClick={props.onClick}
2022-04-02 01:41:45 +13:00
disabled={props.disabled}
2022-05-03 12:02:21 +12:00
aria-label={props["aria-label"]}
2022-03-29 15:54:27 +13:00
>
{props.children}
</IconButton>
);
};
2022-03-30 08:22:26 +13:00
const AttachmentBox = (props) => {
2022-04-09 02:44:35 +12:00
const { t } = useTranslation();
2022-03-30 08:22:26 +13:00
const file = props.file;
return (
2022-03-31 02:57:22 +13:00
<>
<Typography variant="body1" sx={{marginTop: 2}}>
2022-04-09 02:44:35 +12:00
{t("publish_dialog_attached_file_title")}
2022-03-30 08:22:26 +13:00
</Typography>
2022-03-31 02:57:22 +13:00
<Box sx={{
display: 'flex',
alignItems: 'center',
2022-03-31 07:11:18 +13:00
padding: 0.5,
2022-03-31 02:57:22 +13:00
borderRadius: '4px',
}}>
2022-04-05 00:40:54 +12:00
<AttachmentIcon type={file.type}/>
2022-04-04 04:39:52 +12:00
<Box sx={{ marginLeft: 1, textAlign: 'left' }}>
2022-04-01 05:03:36 +13:00
<ExpandingTextField
minWidth={140}
variant="body2"
2022-04-09 02:44:35 +12:00
placeholder={t("publish_dialog_attached_file_filename_placeholder")}
2022-03-31 07:11:18 +13:00
value={props.filename}
2022-04-01 05:03:36 +13:00
onChange={(ev) => props.onChangeFilename(ev.target.value)}
2022-04-02 01:41:45 +13:00
disabled={props.disabled}
2022-03-31 07:11:18 +13:00
/>
2022-03-31 02:57:22 +13:00
<br/>
2022-04-04 04:39:52 +12:00
<Typography variant="body2" sx={{ color: 'text.primary' }}>
{formatBytes(file.size)}
{props.error &&
2022-05-04 06:53:07 +12:00
<Typography component="span" sx={{ color: 'error.main' }} aria-live="polite">
2022-04-04 04:39:52 +12:00
{" "}({props.error})
</Typography>
}
</Typography>
</Box>
2022-05-03 12:02:21 +12:00
<DialogIconButton disabled={props.disabled} onClick={props.onClose} sx={{marginLeft: "6px"}} aria-label={t("publish_dialog_attached_file_remove")}>
<Close/>
</DialogIconButton>
2022-03-31 02:57:22 +13:00
</Box>
</>
2022-03-30 08:22:26 +13:00
);
};
2022-04-01 05:03:36 +13: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-05-03 12:02:21 +12:00
aria-hidden={true}
2022-04-04 11:51:32 +12:00
sx={{position: "absolute", left: "-200%"}}
2022-04-01 05:03:36 +13:00
>
{props.value}
</Typography>
<TextField
margin="dense"
2022-04-09 02:44:35 +12:00
placeholder={props.placeholder}
2022-04-01 05:03:36 +13: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 } }}
2022-05-04 06:53:07 +12:00
inputProps={{
style: { paddingBottom: 0, paddingTop: 0 },
"aria-label": props.placeholder
}}
2022-04-02 01:41:45 +13:00
disabled={props.disabled}
2022-04-01 05:03:36 +13:00
/>
</>
)
};
2022-04-04 11:51:32 +12: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-09 02:44:35 +12:00
const { t } = useTranslation();
2022-04-04 11:51:32 +12: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-09 02:44:35 +12:00
<Typography variant="h5">{t("publish_dialog_drop_file_here")}</Typography>
2022-04-04 11:51:32 +12:00
</Box>
</Box>
);
}
2022-04-09 02:44:35 +12:00
PublishDialog.OPEN_MODE_DEFAULT = "default";
PublishDialog.OPEN_MODE_DRAG = "drag";
2022-04-04 11:51:32 +12:00
2022-04-09 02:44:35 +12:00
export default PublishDialog;