Skip to content

Commit

Permalink
feat: support set payload from file when publishing
Browse files Browse the repository at this point in the history
  • Loading branch information
bj00rn committed May 27, 2024
1 parent 8b43e20 commit 349c3b9
Show file tree
Hide file tree
Showing 2 changed files with 63 additions and 2 deletions.
44 changes: 43 additions & 1 deletion app/src/actions/Publish.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,10 @@ import { Action, ActionTypes } from '../reducers/Publish'
import { AppState } from '../reducers'
import { Base64Message } from '../../../backend/src/Model/Base64Message'
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 => {
return {
Expand All @@ -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 => {
return {
payload,
Expand Down
21 changes: 20 additions & 1 deletion app/src/components/Sidebar/Publish/Publish.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import Editor from './Editor'
import FormatAlignLeft from '@material-ui/icons/FormatAlignLeft'
import { AttachFileOutlined, FormatAlignLeft } from '@material-ui/icons'
import Message from './Model/Message'
import Navigation from '@material-ui/icons/Navigation'
import PublishHistory from './PublishHistory'
Expand Down Expand Up @@ -116,6 +116,10 @@ const EditorMode = memo(function EditorMode(props: {
props.actions.setEditorMode(value)
}, [])

const openFile = useCallback(() => {
props.actions.openFile()
}, [])

const formatJson = useCallback(() => {
if (props.payload) {
try {
Expand All @@ -132,6 +136,7 @@ const EditorMode = memo(function EditorMode(props: {
<div style={{ width: '100%', lineHeight: '64px', textAlign: 'center' }}>
<EditorModeSelect value={props.editorMode} onChange={updateMode} focusEditor={props.focusEditor} />
<FormatJsonButton editorMode={props.editorMode} focusEditor={props.focusEditor} formatJson={formatJson} />
<OpenFileButton editorMode={props.editorMode} openFile={openFile} />
<div style={{ float: 'right' }}>
<PublishButton publish={props.publish} focusEditor={props.focusEditor} />
</div>
Expand Down Expand Up @@ -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 handleClickPublish = useCallback(
(e: React.MouseEvent) => {
Expand Down

0 comments on commit 349c3b9

Please sign in to comment.