Merge pull request #801 from thomasnordquist/feat/set-payload-from-file
feat: support save and load payload from file
This commit is contained in:
@@ -9,12 +9,11 @@ import {
|
|||||||
import { default as persistentStorage, StorageIdentifier } from '../utils/PersistentStorage'
|
import { default as persistentStorage, StorageIdentifier } from '../utils/PersistentStorage'
|
||||||
import { Dispatch } from 'redux'
|
import { Dispatch } from 'redux'
|
||||||
import { showError } from './Global'
|
import { showError } from './Global'
|
||||||
import { promises as fsPromise } from 'fs'
|
|
||||||
import * as path from 'path'
|
import * as path from 'path'
|
||||||
import { ActionTypes, Action } from '../reducers/ConnectionManager'
|
import { ActionTypes, Action } from '../reducers/ConnectionManager'
|
||||||
import { Subscription } from '../../../backend/src/DataSource/MqttSource'
|
import { Subscription } from '../../../backend/src/DataSource/MqttSource'
|
||||||
import { connectionsMigrator } from './migrations/Connection'
|
import { connectionsMigrator } from './migrations/Connection'
|
||||||
import { rendererRpc } from '../../../events'
|
import { rendererRpc, readFromFile } from '../../../events'
|
||||||
import { makeOpenDialogRpc } from '../../../events/OpenDialogRequest'
|
import { makeOpenDialogRpc } from '../../../events/OpenDialogRequest'
|
||||||
|
|
||||||
export interface ConnectionDictionary {
|
export interface ConnectionDictionary {
|
||||||
@@ -81,7 +80,7 @@ async function openCertificate(): Promise<CertificateParameters> {
|
|||||||
throw rejectReasons.noCertificateSelected
|
throw rejectReasons.noCertificateSelected
|
||||||
}
|
}
|
||||||
|
|
||||||
const data = await fsPromise.readFile(selectedFile)
|
const data = await rendererRpc.call(readFromFile, { filePath: selectedFile })
|
||||||
if (data.length > 16_384 || data.length < 64) {
|
if (data.length > 16_384 || data.length < 64) {
|
||||||
throw rejectReasons.certificateSizeDoesNotMatch
|
throw rejectReasons.certificateSizeDoesNotMatch
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -2,7 +2,10 @@ import { Action, ActionTypes } from '../reducers/Publish'
|
|||||||
import { AppState } from '../reducers'
|
import { AppState } from '../reducers'
|
||||||
import { Base64Message } from '../../../backend/src/Model/Base64Message'
|
import { Base64Message } from '../../../backend/src/Model/Base64Message'
|
||||||
import { Dispatch } from 'redux'
|
import { Dispatch } from 'redux'
|
||||||
import { MqttMessage, makePublishEvent, rendererEvents } from '../../../events'
|
import { MqttMessage, makePublishEvent, rendererEvents, rendererRpc, readFromFile } from '../../../events'
|
||||||
|
import { makeOpenDialogRpc } from '../../../events/OpenDialogRequest'
|
||||||
|
import { showError } from './Global'
|
||||||
|
import { Base64 } from 'js-base64'
|
||||||
|
|
||||||
export const setTopic = (topic?: string): Action => {
|
export const setTopic = (topic?: string): Action => {
|
||||||
return {
|
return {
|
||||||
@@ -11,6 +14,49 @@ export const setTopic = (topic?: string): Action => {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
export const openFile = (encoding: 'utf8' = 'utf8') => async (dispatch: Dispatch<any>, getState: () => AppState) => {
|
||||||
|
try {
|
||||||
|
const file = await getFileContent(encoding)
|
||||||
|
if (file) {
|
||||||
|
dispatch(
|
||||||
|
setPayload(file.data))
|
||||||
|
}
|
||||||
|
} catch (error) {
|
||||||
|
dispatch(showError(error))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
type FileParameters = {
|
||||||
|
name: string,
|
||||||
|
data: string
|
||||||
|
}
|
||||||
|
async function getFileContent(encoding: string): Promise<FileParameters | undefined> {
|
||||||
|
const rejectReasons = {
|
||||||
|
noFileSelected: 'No file selected',
|
||||||
|
errorReadingFile: 'Error reading file'
|
||||||
|
}
|
||||||
|
|
||||||
|
const { canceled, filePaths } = await rendererRpc.call(makeOpenDialogRpc(), {
|
||||||
|
properties: ['openFile'],
|
||||||
|
securityScopedBookmarks: true,
|
||||||
|
})
|
||||||
|
|
||||||
|
if (canceled) {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
const selectedFile = filePaths[0]
|
||||||
|
if (!selectedFile) {
|
||||||
|
throw rejectReasons.noFileSelected
|
||||||
|
}
|
||||||
|
try {
|
||||||
|
const data = await rendererRpc.call(readFromFile, { filePath: selectedFile, encoding })
|
||||||
|
return { name: selectedFile, data: data.toString(encoding) }
|
||||||
|
} catch (error) {
|
||||||
|
throw rejectReasons.errorReadingFile
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
export const setPayload = (payload?: string): Action => {
|
export const setPayload = (payload?: string): Action => {
|
||||||
return {
|
return {
|
||||||
payload,
|
payload,
|
||||||
|
|||||||
@@ -1,5 +1,5 @@
|
|||||||
import Editor from './Editor'
|
import Editor from './Editor'
|
||||||
import FormatAlignLeft from '@material-ui/icons/FormatAlignLeft'
|
import { AttachFileOutlined, FormatAlignLeft } from '@material-ui/icons'
|
||||||
import Message from './Model/Message'
|
import Message from './Model/Message'
|
||||||
import Navigation from '@material-ui/icons/Navigation'
|
import Navigation from '@material-ui/icons/Navigation'
|
||||||
import PublishHistory from './PublishHistory'
|
import PublishHistory from './PublishHistory'
|
||||||
@@ -116,6 +116,10 @@ const EditorMode = memo(function EditorMode(props: {
|
|||||||
props.actions.setEditorMode(value)
|
props.actions.setEditorMode(value)
|
||||||
}, [])
|
}, [])
|
||||||
|
|
||||||
|
const openFile = useCallback(() => {
|
||||||
|
props.actions.openFile()
|
||||||
|
}, [])
|
||||||
|
|
||||||
const formatJson = useCallback(() => {
|
const formatJson = useCallback(() => {
|
||||||
if (props.payload) {
|
if (props.payload) {
|
||||||
try {
|
try {
|
||||||
@@ -132,6 +136,7 @@ const EditorMode = memo(function EditorMode(props: {
|
|||||||
<div style={{ width: '100%', lineHeight: '64px', textAlign: 'center' }}>
|
<div style={{ width: '100%', lineHeight: '64px', textAlign: 'center' }}>
|
||||||
<EditorModeSelect value={props.editorMode} onChange={updateMode} focusEditor={props.focusEditor} />
|
<EditorModeSelect value={props.editorMode} onChange={updateMode} focusEditor={props.focusEditor} />
|
||||||
<FormatJsonButton editorMode={props.editorMode} focusEditor={props.focusEditor} formatJson={formatJson} />
|
<FormatJsonButton editorMode={props.editorMode} focusEditor={props.focusEditor} formatJson={formatJson} />
|
||||||
|
<OpenFileButton editorMode={props.editorMode} openFile={openFile} />
|
||||||
<div style={{ float: 'right' }}>
|
<div style={{ float: 'right' }}>
|
||||||
<PublishButton publish={props.publish} focusEditor={props.focusEditor} />
|
<PublishButton publish={props.publish} focusEditor={props.focusEditor} />
|
||||||
</div>
|
</div>
|
||||||
@@ -163,6 +168,20 @@ const FormatJsonButton = React.memo(function FormatJsonButton(props: {
|
|||||||
)
|
)
|
||||||
})
|
})
|
||||||
|
|
||||||
|
const OpenFileButton = React.memo(function OpenFileButton(props: { editorMode: string; openFile: () => void }) {
|
||||||
|
return (
|
||||||
|
<Tooltip title="Open file">
|
||||||
|
<Fab
|
||||||
|
style={{ width: '36px', height: '36px', margin: '0 8px' }}
|
||||||
|
onClick={props.openFile}
|
||||||
|
id="sidebar-publish-open-file"
|
||||||
|
>
|
||||||
|
<AttachFileOutlined style={{ fontSize: '20px' }} />
|
||||||
|
</Fab>
|
||||||
|
</Tooltip>
|
||||||
|
)
|
||||||
|
})
|
||||||
|
|
||||||
const PublishButton = memo(function PublishButton(props: { publish: () => void; focusEditor: () => void }) {
|
const PublishButton = memo(function PublishButton(props: { publish: () => void; focusEditor: () => void }) {
|
||||||
const handleClickPublish = useCallback(
|
const handleClickPublish = useCallback(
|
||||||
(e: React.MouseEvent) => {
|
(e: React.MouseEvent) => {
|
||||||
|
|||||||
@@ -1,6 +1,7 @@
|
|||||||
import * as q from '../../../../../backend/src/Model'
|
import * as q from '../../../../../backend/src/Model'
|
||||||
import ActionButtons from './ActionButtons'
|
import ActionButtons from './ActionButtons'
|
||||||
import Copy from '../../helper/Copy'
|
import Copy from '../../helper/Copy'
|
||||||
|
import Save from '../../helper/Save'
|
||||||
import DateFormatter from '../../helper/DateFormatter'
|
import DateFormatter from '../../helper/DateFormatter'
|
||||||
import MessageHistory from './MessageHistory'
|
import MessageHistory from './MessageHistory'
|
||||||
import Panel from '../Panel'
|
import Panel from '../Panel'
|
||||||
@@ -59,6 +60,12 @@ function ValuePanel(props: Props) {
|
|||||||
return node?.message && decodeMessage(node.message)?.message?.toUnicodeString()
|
return node?.message && decodeMessage(node.message)?.message?.toUnicodeString()
|
||||||
}, [node, decodeMessage])
|
}, [node, decodeMessage])
|
||||||
|
|
||||||
|
const getData = () => {
|
||||||
|
if (node?.message && node.message.payload) {
|
||||||
|
return node.message.payload.base64Message
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
function messageMetaInfo() {
|
function messageMetaInfo() {
|
||||||
if (!props.node || !props.node.message) {
|
if (!props.node || !props.node.message) {
|
||||||
return null
|
return null
|
||||||
@@ -93,10 +100,13 @@ function ValuePanel(props: Props) {
|
|||||||
const [value] =
|
const [value] =
|
||||||
node && node.message && node.message.payload ? node.message.payload?.format(node.type) : [null, undefined]
|
node && node.message && node.message.payload ? node.message.payload?.format(node.type) : [null, undefined]
|
||||||
const copyValue = value ? <Copy getValue={getDecodedValue} /> : null
|
const copyValue = value ? <Copy getValue={getDecodedValue} /> : null
|
||||||
|
const saveValue = value ? <Save getData={getData} /> : null
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<Panel>
|
<Panel>
|
||||||
<span>Value {copyValue}</span>
|
<span>
|
||||||
|
Value {copyValue} {saveValue}
|
||||||
|
</span>
|
||||||
<span style={{ width: '100%' }}>
|
<span style={{ width: '100%' }}>
|
||||||
{renderViewOptions()}
|
{renderViewOptions()}
|
||||||
<div style={{ marginBottom: '-8px', marginTop: '8px' }}>
|
<div style={{ marginBottom: '-8px', marginTop: '8px' }}>
|
||||||
|
|||||||
85
app/src/components/helper/Save.tsx
Normal file
85
app/src/components/helper/Save.tsx
Normal file
@@ -0,0 +1,85 @@
|
|||||||
|
import * as React from 'react'
|
||||||
|
import { connect } from 'react-redux'
|
||||||
|
import Check from '@material-ui/icons/Check'
|
||||||
|
import CustomIconButton from './CustomIconButton'
|
||||||
|
|
||||||
|
import { SaveAlt } from '@material-ui/icons'
|
||||||
|
import { bindActionCreators } from 'redux'
|
||||||
|
import { rendererRpc, writeToFile } from '../../../../events'
|
||||||
|
import { makeSaveDialogRpc } from '../../../../events/OpenDialogRequest'
|
||||||
|
|
||||||
|
import { globalActions } from '../../actions'
|
||||||
|
|
||||||
|
export async function saveToFile(data: string): Promise<string | undefined> {
|
||||||
|
const rejectReasons = {
|
||||||
|
errorWritingFile: 'Error writing file',
|
||||||
|
}
|
||||||
|
|
||||||
|
const { canceled, filePath } = await rendererRpc.call(makeSaveDialogRpc(), {
|
||||||
|
securityScopedBookmarks: true,
|
||||||
|
})
|
||||||
|
|
||||||
|
if (!canceled && filePath !== undefined) {
|
||||||
|
try {
|
||||||
|
const filename = await rendererRpc.call(writeToFile, { filePath, data })
|
||||||
|
return filePath
|
||||||
|
} catch (error) {
|
||||||
|
throw rejectReasons.errorWritingFile
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
interface Props {
|
||||||
|
getData: () => string | undefined
|
||||||
|
actions: {
|
||||||
|
global: typeof globalActions
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
interface State {
|
||||||
|
didSave: boolean
|
||||||
|
}
|
||||||
|
|
||||||
|
class Save extends React.PureComponent<Props, State> {
|
||||||
|
constructor(props: Props) {
|
||||||
|
super(props)
|
||||||
|
this.state = { didSave: false }
|
||||||
|
}
|
||||||
|
|
||||||
|
private handleClick = async (event: React.MouseEvent) => {
|
||||||
|
event.stopPropagation()
|
||||||
|
const data = this.props.getData()
|
||||||
|
if (data != undefined) {
|
||||||
|
const filename = await saveToFile(data)
|
||||||
|
this.props.actions.global.showNotification(`Saved to ${filename}`)
|
||||||
|
this.setState({ didSave: true })
|
||||||
|
setTimeout(() => {
|
||||||
|
this.setState({ didSave: false })
|
||||||
|
}, 1500)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public render() {
|
||||||
|
const icon = !this.state.didSave ? (
|
||||||
|
<SaveAlt fontSize="inherit" />
|
||||||
|
) : (
|
||||||
|
<Check fontSize="inherit" style={{ cursor: 'default' }} />
|
||||||
|
)
|
||||||
|
|
||||||
|
return (
|
||||||
|
<CustomIconButton onClick={this.handleClick} tooltip="Save to file">
|
||||||
|
<div style={{ marginTop: '2px' }}>{icon}</div>
|
||||||
|
</CustomIconButton>
|
||||||
|
)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
const mapDispatchToProps = (dispatch: any) => {
|
||||||
|
return {
|
||||||
|
actions: {
|
||||||
|
global: bindActionCreators(globalActions, dispatch),
|
||||||
|
},
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
export default connect(undefined, mapDispatchToProps)(Save)
|
||||||
@@ -54,3 +54,11 @@ export function makeConnectionMessageEvent(connectionId: string): Event<MqttMess
|
|||||||
export const getAppVersion: RpcEvent<void, string> = {
|
export const getAppVersion: RpcEvent<void, string> = {
|
||||||
topic: 'getAppVersion',
|
topic: 'getAppVersion',
|
||||||
}
|
}
|
||||||
|
|
||||||
|
export const writeToFile: RpcEvent<{ filePath: string, data: string, encoding?: string }, void> = {
|
||||||
|
topic: 'writeFile',
|
||||||
|
}
|
||||||
|
|
||||||
|
export const readFromFile: RpcEvent<{ filePath: string, encoding?: string }, Buffer> = {
|
||||||
|
topic: 'readFromFile',
|
||||||
|
}
|
||||||
@@ -1,4 +1,4 @@
|
|||||||
import { OpenDialogOptions, OpenDialogReturnValue } from 'electron'
|
import { OpenDialogOptions, OpenDialogReturnValue, SaveDialogOptions, SaveDialogReturnValue } from 'electron'
|
||||||
import { RpcEvent } from './EventSystem/Rpc'
|
import { RpcEvent } from './EventSystem/Rpc'
|
||||||
|
|
||||||
export function makeOpenDialogRpc(): RpcEvent<OpenDialogOptions, OpenDialogReturnValue> {
|
export function makeOpenDialogRpc(): RpcEvent<OpenDialogOptions, OpenDialogReturnValue> {
|
||||||
@@ -6,3 +6,9 @@ export function makeOpenDialogRpc(): RpcEvent<OpenDialogOptions, OpenDialogRetur
|
|||||||
topic: 'openDialog',
|
topic: 'openDialog',
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
export function makeSaveDialogRpc(): RpcEvent<SaveDialogOptions, SaveDialogReturnValue> {
|
||||||
|
return {
|
||||||
|
topic: 'saveDialog',
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -4,14 +4,15 @@ import ConfigStorage from '../backend/src/ConfigStorage'
|
|||||||
import { app, BrowserWindow, Menu, dialog } from 'electron'
|
import { app, BrowserWindow, Menu, dialog } from 'electron'
|
||||||
import { autoUpdater } from 'electron-updater'
|
import { autoUpdater } from 'electron-updater'
|
||||||
import { ConnectionManager } from '../backend/src/index'
|
import { ConnectionManager } from '../backend/src/index'
|
||||||
|
import { promises as fsPromise } from 'fs'
|
||||||
// import { electronTelemetryFactory } from 'electron-telemetry'
|
// import { electronTelemetryFactory } from 'electron-telemetry'
|
||||||
import { menuTemplate } from './MenuTemplate'
|
import { menuTemplate } from './MenuTemplate'
|
||||||
import buildOptions from './buildOptions'
|
import buildOptions from './buildOptions'
|
||||||
import { waitForDevServer, isDev, runningUiTestOnCi, loadDevTools } from './development'
|
import { waitForDevServer, isDev, runningUiTestOnCi, loadDevTools } from './development'
|
||||||
import { shouldAutoUpdate, handleAutoUpdate } from './autoUpdater'
|
import { shouldAutoUpdate, handleAutoUpdate } from './autoUpdater'
|
||||||
import { registerCrashReporter } from './registerCrashReporter'
|
import { registerCrashReporter } from './registerCrashReporter'
|
||||||
import { makeOpenDialogRpc } from '../events/OpenDialogRequest'
|
import { makeOpenDialogRpc, makeSaveDialogRpc } from '../events/OpenDialogRequest'
|
||||||
import { backendRpc, getAppVersion } from '../events'
|
import { backendRpc, getAppVersion, writeToFile, readFromFile } from '../events'
|
||||||
|
|
||||||
registerCrashReporter()
|
registerCrashReporter()
|
||||||
|
|
||||||
@@ -25,7 +26,20 @@ app.whenReady().then(() => {
|
|||||||
backendRpc.on(makeOpenDialogRpc(), async request => {
|
backendRpc.on(makeOpenDialogRpc(), async request => {
|
||||||
return dialog.showOpenDialog(BrowserWindow.getFocusedWindow() ?? BrowserWindow.getAllWindows()[0], request)
|
return dialog.showOpenDialog(BrowserWindow.getFocusedWindow() ?? BrowserWindow.getAllWindows()[0], request)
|
||||||
})
|
})
|
||||||
|
|
||||||
|
backendRpc.on(makeSaveDialogRpc(), async request => {
|
||||||
|
return dialog.showSaveDialog(BrowserWindow.getFocusedWindow() ?? BrowserWindow.getAllWindows()[0], request)
|
||||||
|
})
|
||||||
|
|
||||||
backendRpc.on(getAppVersion, async () => app.getVersion())
|
backendRpc.on(getAppVersion, async () => app.getVersion())
|
||||||
|
|
||||||
|
backendRpc.on(writeToFile, async ({ filePath, data, encoding }) => {
|
||||||
|
await fsPromise.writeFile(filePath, Buffer.from(data, 'base64'), { encoding })
|
||||||
|
})
|
||||||
|
|
||||||
|
backendRpc.on(readFromFile, async ({ filePath, encoding }) => {
|
||||||
|
return fsPromise.readFile(filePath, { encoding })
|
||||||
|
})
|
||||||
})
|
})
|
||||||
|
|
||||||
autoUpdater.logger = log
|
autoUpdater.logger = log
|
||||||
|
|||||||
Reference in New Issue
Block a user