Debian Codegraph @

web/src/components/Messaging.jsx

raw
1 import * as React from "react";
2 import { useState } from "react";
3 import { Paper, IconButton, TextField, Portal, Snackbar } from "@mui/material";
4 import SendIcon from "@mui/icons-material/Send";
5 import KeyboardArrowUpIcon from "@mui/icons-material/KeyboardArrowUp";
6 import { useTranslation } from "react-i18next";
7 import PublishDialog from "./PublishDialog";
8 import api from "../app/Api";
9 import Navigation from "./Navigation";
10
11 const Messaging = (props) => {
12 const [message, setMessage] = useState("");
13 const [attachFile, setAttachFile] = useState(null);
14 const [dialogKey, setDialogKey] = useState(0);
15
16 const { dialogOpenMode } = props;
17 const subscription = props.selected;
18
19 const handleOpenDialogClick = () => {
20 props.onDialogOpenModeChange(PublishDialog.OPEN_MODE_DEFAULT);
21 };
22
23 const handleDialogClose = () => {
24 props.onDialogOpenModeChange("");
25 setDialogKey((prev) => prev + 1);
26 setAttachFile(null);
27 };
28
29 const getPastedImage = (ev) => {
30 const { items } = ev.clipboardData;
31 for (let i = 0; i < items.length; i += 1) {
32 if (items[i].type.indexOf("image") !== -1) {
33 return items[i].getAsFile();
34 }
35 }
36 return null;
37 };
38
39 return (
40 <>
41 {subscription && (
42 <MessageBar
43 subscription={subscription}
44 message={message}
45 onMessageChange={setMessage}
46 onFilePasted={setAttachFile}
47 onOpenDialogClick={handleOpenDialogClick}
48 getPastedImage={getPastedImage}
49 />
50 )}
51 <PublishDialog
52 key={`publishDialog${dialogKey}`} // Resets dialog when canceled/closed
53 openMode={dialogOpenMode}
54 baseUrl={subscription?.baseUrl ?? config.base_url}
55 topic={subscription?.topic ?? ""}
56 message={message}
57 attachFile={attachFile}
58 getPastedImage={getPastedImage}
59 onClose={handleDialogClose}
60 onDragEnter={() => props.onDialogOpenModeChange((prev) => prev || PublishDialog.OPEN_MODE_DRAG)} // Only update if not already open
61 onResetOpenMode={() => props.onDialogOpenModeChange(PublishDialog.OPEN_MODE_DEFAULT)}
62 />
63 </>
64 );
65 };
66
67 const MessageBar = (props) => {
68 const { t } = useTranslation();
69 const { subscription } = props;
70 const [snackOpen, setSnackOpen] = useState(false);
71 const handleSendClick = async () => {
72 try {
73 await api.publish(subscription.baseUrl, subscription.topic, props.message);
74 } catch (e) {
75 console.log(`[MessageBar] Error publishing message`, e);
76 setSnackOpen(true);
77 }
78 props.onMessageChange("");
79 };
80
81 const handlePaste = (ev) => {
82 const blob = props.getPastedImage(ev);
83 if (blob) {
84 props.onFilePasted(blob);
85 props.onOpenDialogClick();
86 }
87 };
88
89 return (
90 <Paper
91 elevation={3}
92 sx={{
93 display: "flex",
94 position: "fixed",
95 bottom: 0,
96 right: 0,
97 padding: 2,
98 width: { xs: "100%", sm: `calc(100% - ${Navigation.width}px)` },
99 backgroundColor: (theme) => (theme.palette.mode === "light" ? theme.palette.grey[100] : theme.palette.grey[900]),
100 }}
101 >
102 <IconButton color="inherit" size="large" edge="start" onClick={props.onOpenDialogClick} aria-label={t("message_bar_show_dialog")}>
103 <KeyboardArrowUpIcon />
104 </IconButton>
105 <TextField
106 autoFocus
107 margin="dense"
108 placeholder={t("message_bar_type_message")}
109 aria-label={t("message_bar_type_message")}
110 role="textbox"
111 type="text"
112 fullWidth
113 variant="standard"
114 value={props.message}
115 onChange={(ev) => props.onMessageChange(ev.target.value)}
116 onKeyPress={(ev) => {
117 if (ev.key === "Enter") {
118 ev.preventDefault();
119 handleSendClick();
120 }
121 }}
122 onPaste={handlePaste}
123 />
124 <IconButton color="inherit" size="large" edge="end" onClick={handleSendClick} aria-label={t("message_bar_publish")}>
125 <SendIcon />
126 </IconButton>
127 <Portal>
128 <Snackbar
129 open={snackOpen}
130 autoHideDuration={3000}
131 onClose={() => setSnackOpen(false)}
132 message={t("message_bar_error_publishing")}
133 />
134 </Portal>
135 </Paper>
136 );
137 };
138
139 export default Messaging;