import * as React from 'react'; import {useRef, useState} from 'react'; import {NotificationItem} from "./Notifications"; import theme from "./theme"; import {Chip, FormControl, 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 {formatBytes, shortUrl, splitNoEmpty, 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 Divider from "@mui/material/Divider"; const SendDialog = (props) => { const [topicUrl, setTopicUrl] = useState(props.topicUrl); 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 [email, setEmail] = useState(""); const [delay, setDelay] = useState(""); const [showTopicUrl, setShowTopicUrl] = useState(props.topicUrl === ""); 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 [errorText, setErrorText] = useState(""); const fullScreen = useMediaQuery(theme.breakpoints.down('sm')); const sendButtonEnabled = (() => { if (!validTopicUrl(topicUrl)) { return false; } return true; })(); const handleSubmit = async () => { const { baseUrl, topic } = splitTopicUrl(topicUrl); const options = {}; if (title.trim()) { options["title"] = title.trim(); } if (tags.trim()) { options["tags"] = splitNoEmpty(tags, ","); } if (priority && priority !== 3) { options["priority"] = priority; } if (clickUrl.trim()) { options["click"] = clickUrl.trim(); } if (attachUrl.trim()) { options["attach"] = attachUrl.trim(); } if (filename.trim()) { options["filename"] = filename.trim(); } if (email.trim()) { options["email"] = email.trim(); } if (delay.trim()) { options["delay"] = delay.trim(); } try { const response = await api.publish(baseUrl, topic, message, options); console.log(response); props.onClose(); } catch (e) { setErrorText(e); } }; const handleAttachFileClick = () => { attachFileInput.current.click(); }; const handleAttachFileChanged = (ev) => { const file = ev.target.files[0]; setAttachFile(file); setFilename(file.name); console.log(ev.target.files[0]); console.log(URL.createObjectURL(ev.target.files[0])); }; return ( Publish to {shortUrl(topicUrl)} {showTopicUrl && { setTopicUrl(props.topicUrl); setShowTopicUrl(false); }}> setTopicUrl(ev.target.value)} type="text" variant="standard" fullWidth required /> } setTitle(ev.target.value)} type="text" fullWidth variant="standard" placeholder="Notification title, e.g. Disk space alert" /> setMessage(ev.target.value)} type="text" variant="standard" rows={5} fullWidth autoFocus multiline />
null}> setTags(ev.target.value)} type="text" variant="standard" sx={{flexGrow: 1, marginRight: 1}} />
{showClickUrl && { setClickUrl(""); setShowClickUrl(false); }}> setClickUrl(ev.target.value)} type="url" fullWidth variant="standard" /> } {showEmail && { setEmail(""); setShowEmail(false); }}> setEmail(ev.target.value)} type="email" variant="standard" fullWidth /> } {showAttachUrl && { setAttachUrl(""); setShowAttachUrl(false); }}> setAttachUrl(ev.target.value)} type="url" variant="standard" fullWidth /> } {showAttachFile && setFilename(f)} onClose={() => setAttachFile(null)} />} {showAttachUrl && setFilename(ev.target.value)} type="text" variant="standard" fullWidth />} {showDelay && { setDelay(""); setShowDelay(false); }}> setDelay(ev.target.value)} type="text" variant="standard" fullWidth /> } Other features:
{!showClickUrl && setShowClickUrl(true)} sx={{marginRight: 1}}/>} {!showEmail && setShowEmail(true)} sx={{marginRight: 1}}/>} {!showAttachUrl && !showAttachFile && setShowAttachUrl(true)} sx={{marginRight: 1}}/>} {!showAttachFile && !showAttachUrl && handleAttachFileClick()} sx={{marginRight: 1}}/>} {!showDelay && setShowDelay(true)} sx={{marginRight: 1}}/>} {!showTopicUrl && setShowTopicUrl(true)} sx={{marginRight: 1}}/>}
For examples and a detailed description of all send features, please refer to the documentation.
); }; 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; const maybeInfoText = formatBytes(file.size); const [editFilename, setEditFilename] = useState(false); return ( <> Attached file: props.onChangeFilename(ev.target.value)} fullWidth /> {editFilename ? : {props.filename} } setEditFilename(true)}>
{maybeInfoText}
); }; 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;