import * as React from 'react'; import {useEffect, useRef, useState} from 'react'; import {NotificationItem} from "./Notifications"; import theme from "./theme"; 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"; 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 {basicAuth, formatBytes, shortUrl, splitTopicUrl, validTopicUrl} from "../app/utils"; import Box from "@mui/material/Box"; import Icon from "./Icon"; import DialogFooter from "./DialogFooter"; import api from "../app/Api"; import userManager from "../app/UserManager"; const SendDialog = (props) => { const [topicUrl, setTopicUrl] = useState(""); const [message, setMessage] = useState(props.message || ""); const [title, setTitle] = useState(""); const [tags, setTags] = useState(""); const [priority, setPriority] = useState(3); const [clickUrl, setClickUrl] = useState(""); const [attachUrl, setAttachUrl] = useState(""); const [attachFile, setAttachFile] = useState(null); const [filename, setFilename] = useState(""); const [filenameEdited, setFilenameEdited] = useState(false); const [email, setEmail] = useState(""); const [delay, setDelay] = useState(""); const [publishAnother, setPublishAnother] = useState(false); const [showTopicUrl, setShowTopicUrl] = useState(""); const [showClickUrl, setShowClickUrl] = useState(false); const [showAttachUrl, setShowAttachUrl] = useState(false); const [showEmail, setShowEmail] = useState(false); const [showDelay, setShowDelay] = useState(false); const showAttachFile = !!attachFile && !showAttachUrl; const attachFileInput = useRef(); const [attachFileError, setAttachFileError] = useState(""); const [activeRequest, setActiveRequest] = useState(null); const [statusText, setStatusText] = useState(""); const disabled = !!activeRequest; const [sendButtonEnabled, setSendButtonEnabled] = useState(true); const dropZone = props.dropZone; const fullScreen = useMediaQuery(theme.breakpoints.down('sm')); useEffect(() => { setTopicUrl(props.topicUrl); setShowTopicUrl(props.topicUrl === "") }, [props.topicUrl]); useEffect(() => { setSendButtonEnabled(validTopicUrl(topicUrl) && !attachFileError); }, [topicUrl, attachFileError]); const handleSubmit = async () => { const { baseUrl, topic } = splitTopicUrl(topicUrl); const headers = {}; if (title.trim()) { headers["X-Title"] = title.trim(); } if (tags.trim()) { headers["X-Tags"] = tags.trim(); } if (priority && priority !== 3) { headers["X-Priority"] = priority.toString(); } if (clickUrl.trim()) { headers["X-Click"] = clickUrl.trim(); } if (attachUrl.trim()) { headers["X-Attach"] = attachUrl.trim(); } if (filename.trim()) { headers["X-Filename"] = filename.trim(); } if (email.trim()) { headers["X-Email"] = email.trim(); } if (delay.trim()) { headers["X-Delay"] = delay.trim(); } if (attachFile && message.trim()) { headers["X-Message"] = message.replaceAll("\n", "\\n").trim(); } const body = (attachFile) ? attachFile : message; try { const user = await userManager.get(baseUrl); if (user) { headers["Authorization"] = basicAuth(user.username, user.password); } const progressFn = (ev) => { console.log(ev); if (ev.loaded > 0 && ev.total > 0) { const percent = Math.round(ev.loaded * 100.0 / ev.total); setStatusText(`Uploading ${formatBytes(ev.loaded)}/${formatBytes(ev.total)} (${percent}%) ...`); } else { setStatusText(`Uploading ...`); } }; const request = api.publishXHR(baseUrl, topic, body, headers, progressFn); setActiveRequest(request); await request; if (!publishAnother) { props.onClose(); } else { setStatusText("Message published"); } } catch (e) { console.log("error", e); setStatusText("An error occurred"); } setActiveRequest(null); }; const checkAttachmentLimits = async (file) => { try { const { baseUrl } = splitTopicUrl(topicUrl); const stats = await api.userStats(baseUrl); console.log(`[SendDialog] Visitor attachment limits`, stats); const fileSizeLimit = stats.attachmentFileSizeLimit ?? 0; if (fileSizeLimit > 0 && file.size > fileSizeLimit) { return setAttachFileError(`exceeds ${formatBytes(fileSizeLimit)} limit`); } const remainingBytes = stats.visitorAttachmentBytesRemaining ?? 0; if (remainingBytes > 0 && file.size > remainingBytes) { return setAttachFileError(`quota reached, only ${formatBytes(remainingBytes)} remaining`); } setAttachFileError(""); } catch (e) { console.log(`[SendDialog] Retrieving attachment limits failed`, e); setAttachFileError(""); // Reset error (rely on server-side checking) } }; const handleAttachFileClick = () => { attachFileInput.current.click(); }; const handleAttachFileChanged = async (ev) => { await updateAttachFile(ev.target.files[0]); }; const handleAttachFileDrop = async (ev) => { ev.preventDefault(); props.onDrop(); await updateAttachFile(ev.dataTransfer.files[0]); }; const updateAttachFile = async (file) => { setAttachFile(file); setFilename(file.name); await checkAttachmentLimits(file); }; const allowDrag = (ev) => { if (true /* allowSubmit */) { ev.dataTransfer.dropEffect = 'copy'; ev.preventDefault(); } }; return ( Publish to {shortUrl(topicUrl)} {dropZone && Drop file here } {showTopicUrl && { setTopicUrl(props.topicUrl); setShowTopicUrl(false); }}> setTopicUrl(ev.target.value)} disabled={disabled} type="text" variant="standard" fullWidth required /> } setTitle(ev.target.value)} disabled={disabled} type="text" fullWidth variant="standard" placeholder="Notification title, e.g. Disk space alert" /> setMessage(ev.target.value)} disabled={disabled} type="text" variant="standard" rows={5} fullWidth autoFocus multiline />
null}> setTags(ev.target.value)} disabled={disabled} type="text" variant="standard" sx={{flexGrow: 1, marginRight: 1}} />
{showClickUrl && { setClickUrl(""); setShowClickUrl(false); }}> setClickUrl(ev.target.value)} disabled={disabled} type="url" fullWidth variant="standard" /> } {showEmail && { setEmail(""); setShowEmail(false); }}> setEmail(ev.target.value)} disabled={disabled} type="email" variant="standard" fullWidth /> } {showAttachUrl && { setAttachUrl(""); setFilename(""); setFilenameEdited(false); setShowAttachUrl(false); }}> { 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 } } }} disabled={disabled} type="url" variant="standard" sx={{flexGrow: 5, marginRight: 1}} /> { setFilename(ev.target.value); setFilenameEdited(true); }} disabled={disabled} type="text" variant="standard" sx={{flexGrow: 1}} /> } {showAttachFile && setFilename(f)} onClose={() => { setAttachFile(null); setAttachFileError(""); setFilename(""); }} />} {showDelay && { setDelay(""); setShowDelay(false); }}> setDelay(ev.target.value)} disabled={disabled} type="text" variant="standard" fullWidth /> } Other features:
{!showClickUrl && setShowClickUrl(true)} sx={{marginRight: 1, marginBottom: 1}}/>} {!showEmail && setShowEmail(true)} sx={{marginRight: 1, marginBottom: 1}}/>} {!showAttachUrl && !showAttachFile && setShowAttachUrl(true)} sx={{marginRight: 1, marginBottom: 1}}/>} {!showAttachFile && !showAttachUrl && handleAttachFileClick()} sx={{marginRight: 1, marginBottom: 1}}/>} {!showDelay && setShowDelay(true)} sx={{marginRight: 1, marginBottom: 1}}/>} {!showTopicUrl && setShowTopicUrl(true)} sx={{marginRight: 1, marginBottom: 1}}/>}
For examples and a detailed description of all send features, please refer to the documentation.
{activeRequest && } {!activeRequest && <> setPublishAnother(ev.target.checked)} /> } /> }
); }; const Row = (props) => { return (
{props.children}
); }; const ClosableRow = (props) => { return ( {props.children} ); }; const DialogIconButton = (props) => { const sx = props.sx || {}; return ( {props.children} ); }; const AttachmentBox = (props) => { const file = props.file; return ( <> Attached file: props.onChangeFilename(ev.target.value)} disabled={props.disabled} />
{formatBytes(file.size)} {props.error && {" "}({props.error}) }
); }; 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 ( <> {props.value} ) }; const priorities = { 1: { label: "Minimum priority", file: priority1 }, 2: { label: "Low priority", file: priority2 }, 3: { label: "Default priority", file: priority3 }, 4: { label: "High priority", file: priority4 }, 5: { label: "Maximum priority", file: priority5 } }; export default SendDialog;