ntfy/web/src/components/Messaging.js

115 lines
4.1 KiB
JavaScript
Raw Normal View History

2022-04-05 02:04:01 +12:00
import * as React from 'react';
import {useState} from 'react';
import Navigation from "./Navigation";
import Paper from "@mui/material/Paper";
import IconButton from "@mui/material/IconButton";
import TextField from "@mui/material/TextField";
import SendIcon from "@mui/icons-material/Send";
import api from "../app/Api";
2022-04-09 02:44:35 +12:00
import PublishDialog from "./PublishDialog";
2022-04-05 02:04:01 +12:00
import KeyboardArrowUpIcon from '@mui/icons-material/KeyboardArrowUp';
import {Portal, Snackbar} from "@mui/material";
2022-04-09 02:44:35 +12:00
import {useTranslation} from "react-i18next";
2022-04-05 02:04:01 +12:00
const Messaging = (props) => {
const [message, setMessage] = useState("");
const [dialogKey, setDialogKey] = useState(0);
const dialogOpenMode = props.dialogOpenMode;
const subscription = props.selected;
const handleOpenDialogClick = () => {
2022-04-09 02:44:35 +12:00
props.onDialogOpenModeChange(PublishDialog.OPEN_MODE_DEFAULT);
2022-04-05 02:04:01 +12:00
};
2022-04-09 02:44:35 +12:00
const handleDialogClose = () => {
2022-04-05 02:04:01 +12:00
props.onDialogOpenModeChange("");
setDialogKey(prev => prev+1);
};
return (
<>
{subscription && <MessageBar
subscription={subscription}
message={message}
onMessageChange={setMessage}
onOpenDialogClick={handleOpenDialogClick}
/>}
2022-04-09 02:44:35 +12:00
<PublishDialog
key={`publishDialog${dialogKey}`} // Resets dialog when canceled/closed
2022-04-05 02:04:01 +12:00
openMode={dialogOpenMode}
2023-01-05 16:47:12 +13:00
baseUrl={subscription?.baseUrl ?? config.base_url}
2022-04-06 15:33:07 +12:00
topic={subscription?.topic ?? ""}
2022-04-05 02:04:01 +12:00
message={message}
2022-04-09 02:44:35 +12:00
onClose={handleDialogClose}
onDragEnter={() => props.onDialogOpenModeChange(prev => (prev) ? prev : PublishDialog.OPEN_MODE_DRAG)} // Only update if not already open
onResetOpenMode={() => props.onDialogOpenModeChange(PublishDialog.OPEN_MODE_DEFAULT)}
2022-04-05 02:04:01 +12:00
/>
</>
);
}
const MessageBar = (props) => {
2022-04-09 02:44:35 +12:00
const { t } = useTranslation();
2022-04-05 02:04:01 +12:00
const subscription = props.subscription;
const [snackOpen, setSnackOpen] = useState(false);
const handleSendClick = async () => {
try {
await api.publish(subscription.baseUrl, subscription.topic, props.message);
} catch (e) {
console.log(`[MessageBar] Error publishing message`, e);
setSnackOpen(true);
}
2022-04-05 02:04:01 +12:00
props.onMessageChange("");
};
return (
<Paper
elevation={3}
sx={{
display: "flex",
position: 'fixed',
bottom: 0,
right: 0,
padding: 2,
width: { xs: "100%", sm: `calc(100% - ${Navigation.width}px)` },
2022-04-05 02:04:01 +12:00
backgroundColor: (theme) => theme.palette.mode === 'light' ? theme.palette.grey[100] : theme.palette.grey[900]
}}
>
2022-05-03 11:30:29 +12:00
<IconButton color="inherit" size="large" edge="start" onClick={props.onOpenDialogClick} aria-label={t("message_bar_show_dialog")}>
2022-04-05 02:04:01 +12:00
<KeyboardArrowUpIcon/>
</IconButton>
<TextField
autoFocus
margin="dense"
2022-04-09 02:44:35 +12:00
placeholder={t("message_bar_type_message")}
2022-05-03 11:30:29 +12:00
aria-label={t("message_bar_type_message")}
2022-12-22 07:19:07 +13:00
role="textbox"
2022-04-05 02:04:01 +12:00
type="text"
fullWidth
variant="standard"
value={props.message}
onChange={ev => props.onMessageChange(ev.target.value)}
onKeyPress={(ev) => {
if (ev.key === 'Enter') {
ev.preventDefault();
handleSendClick();
}
}}
/>
2022-05-03 11:30:29 +12:00
<IconButton color="inherit" size="large" edge="end" onClick={handleSendClick} aria-label={t("message_bar_publish")}>
2022-04-05 02:04:01 +12:00
<SendIcon/>
</IconButton>
<Portal>
<Snackbar
open={snackOpen}
autoHideDuration={3000}
onClose={() => setSnackOpen(false)}
2022-04-09 02:44:35 +12:00
message={t("message_bar_error_publishing")}
/>
</Portal>
2022-04-05 02:04:01 +12:00
</Paper>
);
};
export default Messaging;