1
0
Fork 0
mirror of https://github.com/binwiederhier/ntfy.git synced 2024-07-22 04:57:30 +02:00
ntfy/web/src/components/Messaging.jsx

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

109 lines
3.5 KiB
React
Raw Normal View History

2022-04-04 16:04:01 +02:00
import * as React from "react";
import { useState } from "react";
import { Paper, IconButton, TextField, Portal, Snackbar } from "@mui/material";
2022-04-04 16:04:01 +02:00
import SendIcon from "@mui/icons-material/Send";
import KeyboardArrowUpIcon from "@mui/icons-material/KeyboardArrowUp";
2022-04-08 16:44:35 +02:00
import { useTranslation } from "react-i18next";
import PublishDialog from "./PublishDialog";
2022-04-04 16:04:01 +02:00
import api from "../app/Api";
import Navigation from "./Navigation";
const Messaging = (props) => {
const [message, setMessage] = useState("");
const [dialogKey, setDialogKey] = useState(0);
const { dialogOpenMode } = props;
const subscription = props.selected;
const handleOpenDialogClick = () => {
2022-04-08 16:44:35 +02:00
props.onDialogOpenModeChange(PublishDialog.OPEN_MODE_DEFAULT);
2022-04-04 16:04:01 +02:00
};
2022-04-08 16:44:35 +02:00
const handleDialogClose = () => {
2022-04-04 16:04:01 +02:00
props.onDialogOpenModeChange("");
setDialogKey((prev) => prev + 1);
};
return (
<>
{subscription && (
<MessageBar subscription={subscription} message={message} onMessageChange={setMessage} onOpenDialogClick={handleOpenDialogClick} />
)}
2022-04-08 16:44:35 +02:00
<PublishDialog
key={`publishDialog${dialogKey}`} // Resets dialog when canceled/closed
2022-04-04 16:04:01 +02:00
openMode={dialogOpenMode}
2023-01-05 04:47:12 +01:00
baseUrl={subscription?.baseUrl ?? config.base_url}
2022-04-06 05:33:07 +02:00
topic={subscription?.topic ?? ""}
2022-04-04 16:04:01 +02:00
message={message}
2022-04-08 16:44:35 +02:00
onClose={handleDialogClose}
onDragEnter={() => props.onDialogOpenModeChange((prev) => prev || PublishDialog.OPEN_MODE_DRAG)} // Only update if not already open
onResetOpenMode={() => props.onDialogOpenModeChange(PublishDialog.OPEN_MODE_DEFAULT)}
2022-04-04 16:04:01 +02:00
/>
</>
);
};
const MessageBar = (props) => {
2022-04-08 16:44:35 +02:00
const { t } = useTranslation();
2022-04-04 16:04:01 +02:00
const { subscription } = props;
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-04 16:04:01 +02: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-04 16:04:01 +02:00
backgroundColor: (theme) => (theme.palette.mode === "light" ? theme.palette.grey[100] : theme.palette.grey[900]),
}}
>
2022-05-03 01:30:29 +02:00
<IconButton color="inherit" size="large" edge="start" onClick={props.onOpenDialogClick} aria-label={t("message_bar_show_dialog")}>
2022-04-04 16:04:01 +02:00
<KeyboardArrowUpIcon />
</IconButton>
<TextField
autoFocus
margin="dense"
2022-04-08 16:44:35 +02:00
placeholder={t("message_bar_type_message")}
2022-05-03 01:30:29 +02:00
aria-label={t("message_bar_type_message")}
2022-12-21 19:19:07 +01:00
role="textbox"
2022-04-04 16:04:01 +02: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 01:30:29 +02:00
<IconButton color="inherit" size="large" edge="end" onClick={handleSendClick} aria-label={t("message_bar_publish")}>
2022-04-04 16:04:01 +02:00
<SendIcon />
</IconButton>
<Portal>
2022-04-08 16:44:35 +02:00
<Snackbar
open={snackOpen}
autoHideDuration={3000}
onClose={() => setSnackOpen(false)}
message={t("message_bar_error_publishing")}
/>
</Portal>
2022-04-04 16:04:01 +02:00
</Paper>
);
};
export default Messaging;