feat: support set payload from file when publishing
This commit is contained in:
@@ -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 { promises as fsPromise } from 'fs'
|
||||||
|
import { MqttMessage, makePublishEvent, rendererEvents, rendererRpc } from '../../../events'
|
||||||
|
import { makeOpenDialogRpc } from '../../../events/OpenDialogRequest'
|
||||||
|
import { showError } from './Global'
|
||||||
|
|
||||||
export const setTopic = (topic?: string): Action => {
|
export const setTopic = (topic?: string): Action => {
|
||||||
return {
|
return {
|
||||||
@@ -11,6 +14,45 @@ export const setTopic = (topic?: string): Action => {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
export const openFile = () => async (dispatch: Dispatch<any>, getState: () => AppState) => {
|
||||||
|
try {
|
||||||
|
const file = await getFileContent()
|
||||||
|
dispatch(
|
||||||
|
setPayload(Base64Message.fromBuffer(file.data).toUnicodeString()
|
||||||
|
))
|
||||||
|
} catch (error) {
|
||||||
|
dispatch(showError(error))
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
type FileParameters = {
|
||||||
|
name: string,
|
||||||
|
data: Buffer
|
||||||
|
}
|
||||||
|
async function getFileContent(): Promise<FileParameters> {
|
||||||
|
const rejectReasons = {
|
||||||
|
noFileSelected: 'No file selected',
|
||||||
|
errorReadingFile: 'Error reading file'
|
||||||
|
}
|
||||||
|
|
||||||
|
const openDialogReturnValue = await rendererRpc.call(makeOpenDialogRpc(), {
|
||||||
|
properties: ['openFile'],
|
||||||
|
securityScopedBookmarks: true,
|
||||||
|
})
|
||||||
|
|
||||||
|
const selectedFile = openDialogReturnValue.filePaths && openDialogReturnValue.filePaths[0]
|
||||||
|
if (!selectedFile) {
|
||||||
|
throw rejectReasons.noFileSelected
|
||||||
|
}
|
||||||
|
try {
|
||||||
|
const data = await fsPromise.readFile(selectedFile)
|
||||||
|
return { name: selectedFile, data }
|
||||||
|
} 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) => {
|
||||||
|
|||||||
Reference in New Issue
Block a user