From 83bb9951b00f0fd4d4d4b9ba768ce670a83288b4 Mon Sep 17 00:00:00 2001 From: Philipp Heckel Date: Tue, 5 Apr 2022 23:33:07 -0400 Subject: [PATCH] Split baseUrl and topic --- web/src/app/Api.js | 2 +- web/src/components/Messaging.js | 5 ++-- web/src/components/SendDialog.js | 51 +++++++++++++++++++++----------- 3 files changed, 36 insertions(+), 22 deletions(-) diff --git a/web/src/app/Api.js b/web/src/app/Api.js index a5e5bec3..10e8311b 100644 --- a/web/src/app/Api.js +++ b/web/src/app/Api.js @@ -127,7 +127,7 @@ class Api { if (response.status !== 200) { throw new Error(`Unexpected server response ${response.status}`); } - const stats = response.json(); + const stats = await response.json(); console.log(`[Api] Stats`, stats); return stats; } diff --git a/web/src/components/Messaging.js b/web/src/components/Messaging.js index aa1d4740..d4435f30 100644 --- a/web/src/components/Messaging.js +++ b/web/src/components/Messaging.js @@ -9,7 +9,6 @@ import SendIcon from "@mui/icons-material/Send"; import api from "../app/Api"; import SendDialog from "./SendDialog"; import KeyboardArrowUpIcon from '@mui/icons-material/KeyboardArrowUp'; -import EmojiPicker from "./EmojiPicker"; import {Portal, Snackbar} from "@mui/material"; const Messaging = (props) => { @@ -18,7 +17,6 @@ const Messaging = (props) => { const dialogOpenMode = props.dialogOpenMode; const subscription = props.selected; - const selectedTopicUrl = (subscription) ? topicUrl(subscription.baseUrl, subscription.topic) : ""; const handleOpenDialogClick = () => { props.onDialogOpenModeChange(SendDialog.OPEN_MODE_DEFAULT); @@ -40,7 +38,8 @@ const Messaging = (props) => { props.onDialogOpenModeChange(prev => (prev) ? prev : SendDialog.OPEN_MODE_DRAG)} // Only update if not already open diff --git a/web/src/components/SendDialog.js b/web/src/components/SendDialog.js index 7d6f6522..fce39290 100644 --- a/web/src/components/SendDialog.js +++ b/web/src/components/SendDialog.js @@ -18,7 +18,7 @@ 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 {basicAuth, formatBytes, topicShortUrl, topicUrl, validTopicUrl} from "../app/utils"; import Box from "@mui/material/Box"; import AttachmentIcon from "./AttachmentIcon"; import DialogFooter from "./DialogFooter"; @@ -27,8 +27,10 @@ import userManager from "../app/UserManager"; import EmojiPicker from "./EmojiPicker"; const SendDialog = (props) => { - const [topicUrl, setTopicUrl] = useState(""); + const [baseUrl, setBaseUrl] = useState(""); + const [topic, setTopic] = useState(""); const [message, setMessage] = useState(""); + const [messageFocused, setMessageFocused] = useState(true); const [title, setTitle] = useState(""); const [tags, setTags] = useState(""); const [priority, setPriority] = useState(3); @@ -71,21 +73,22 @@ const SendDialog = (props) => { }, []); useEffect(() => { - setTopicUrl(props.topicUrl); - setShowTopicUrl(props.topicUrl === "") - }, [props.topicUrl]); + setBaseUrl(props.baseUrl); + setTopic(props.topic); + setShowTopicUrl(!props.baseUrl || !props.topic); + setMessageFocused(!!props.topic); // Focus message only if topic is set + }, [props.baseUrl, props.topic]); useEffect(() => { - const valid = validTopicUrl(topicUrl) && !attachFileError; + const valid = validTopicUrl(topicUrl(baseUrl, topic)) && !attachFileError; setSendButtonEnabled(valid); - }, [topicUrl, attachFileError]); + }, [baseUrl, topic, attachFileError]); useEffect(() => { setMessage(props.message); }, [props.message]); const handleSubmit = async () => { - const { baseUrl, topic } = splitTopicUrl(topicUrl); const headers = {}; if (title.trim()) { headers["X-Title"] = title.trim(); @@ -145,7 +148,6 @@ const SendDialog = (props) => { const checkAttachmentLimits = async (file) => { try { - const { baseUrl } = splitTopicUrl(topicUrl); const stats = await api.userStats(baseUrl); const fileSizeLimit = stats.attachmentFileSizeLimit ?? 0; const remainingBytes = stats.visitorAttachmentBytesRemaining ?? 0; @@ -212,24 +214,37 @@ const SendDialog = (props) => { onDragLeave={handleAttachFileDragLeave}/> } - {topicUrl ? `Publish to ${shortUrl(topicUrl)}` : "Publish message"} + {(baseUrl && topic) ? `Publish to ${topicShortUrl(baseUrl, topic)}` : "Publish message"} {dropZone && } {showTopicUrl && - { - setTopicUrl(props.topicUrl); + { + setBaseUrl(props.baseUrl); + setTopic(props.topic); setShowTopicUrl(false); }}> setTopicUrl(ev.target.value)} + label="Server URL" + placeholder="Server URL, e.g. https://example.com" + value={baseUrl} + onChange={ev => setBaseUrl(ev.target.value)} + disabled={disabled} + type="url" + variant="standard" + sx={{flexGrow: 1, marginRight: 1}} + /> + setTopic(ev.target.value)} disabled={disabled} type="text" variant="standard" - fullWidth - required + autoFocus={!messageFocused} + sx={{flexGrow: 1}} /> } @@ -254,8 +269,8 @@ const SendDialog = (props) => { type="text" variant="standard" rows={5} + autoFocus={messageFocused} fullWidth - autoFocus multiline />