From f7843bc3480950a3a4da9cfd2351dff32698a6f4 Mon Sep 17 00:00:00 2001 From: Thomas Nordquist Date: Fri, 11 Jan 2019 10:17:02 +0100 Subject: [PATCH] Add publish history & fix topic input --- app/src/components/Sidebar/Publisher.tsx | 66 ++++++++++++++++++++---- 1 file changed, 57 insertions(+), 9 deletions(-) diff --git a/app/src/components/Sidebar/Publisher.tsx b/app/src/components/Sidebar/Publisher.tsx index 7ee36fa..88555d3 100644 --- a/app/src/components/Sidebar/Publisher.tsx +++ b/app/src/components/Sidebar/Publisher.tsx @@ -21,16 +21,23 @@ interface Props { connectionId?: string } +interface Message { + topic: string + payload?: string + sent: Date +} + interface State { customTopic?: string payload?: string mode: string + history: Message[] } class Publisher extends React.Component { constructor(props: any) { super(props) - this.state = { mode: 'json' } + this.state = { mode: 'json', history: [] } } private updatePayload = (value: string, event?: any) => { @@ -47,14 +54,21 @@ class Publisher extends React.Component { private publish = (e: React.MouseEvent) => { e.stopPropagation() - if (!this.props.connectionId || !this.state.customTopic) { - return - } + const topic = this.state.customTopic + const payload = this.state.payload - rendererEvents.emit(makePublishEvent(this.props.connectionId), { - topic: this.state.customTopic, - payload: this.state.payload, - }) + if (this.props.connectionId && topic) { + rendererEvents.emit(makePublishEvent(this.props.connectionId), { topic, payload }) + this.addMessageToHistory(topic, payload) + } + } + + private addMessageToHistory(topic: string, payload?: string) { + // Remove duplicates + let filteredHistory = this.state.history.filter(e => e.payload !== payload || e.topic !== topic) + filteredHistory = filteredHistory.slice(-7) + const history: Message[] = [...filteredHistory, { topic, payload, sent: new Date() }] + this.setState({ history }) } public render() { @@ -62,13 +76,15 @@ class Publisher extends React.Component {
{this.topic()} {this.editor()} + {this.history()}
) } private topic() { const { node } = this.props - const topicStr = this.state.customTopic || (node ? node.path() : '') + const topicStr = (this.state.customTopic !== undefined) ? this.state.customTopic : (node ? node.path() : '') + return (
Topic @@ -79,11 +95,18 @@ class Publisher extends React.Component { value={topicStr} multiline={true} onChange={this.updateTopic} + InputProps={{ onBlur: this.onTopicBlur }} />
) } + private onTopicBlur = (e: React.FocusEvent) => { + if (!e.target.value) { + this.setState({ customTopic: undefined }) + } + } + private editorOptions = { enableBasicAutocompletion: true, enableLiveAutocompletion: true, @@ -146,6 +169,31 @@ class Publisher extends React.Component { ) } + private loadHistoryEntry(entry: Message) { + this.setState({ + customTopic: entry.topic, + payload: entry.payload, + }) + } + + private history() { + const entries = this.state.history.map(message => ( + this.loadHistoryEntry(message)}> +
+
{message.topic}
+
{message.payload}
+
+
+ )) + + return ( +
+ History + {entries} +
+ ) + } + private editor() { return (