diff --git a/app/src/actions/Publish.ts b/app/src/actions/Publish.ts index a752251..be161c1 100644 --- a/app/src/actions/Publish.ts +++ b/app/src/actions/Publish.ts @@ -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 { @@ -11,6 +14,45 @@ export const setTopic = (topic?: string): Action => { } } +export const openFile = () => async (dispatch: Dispatch, 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 { + 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, diff --git a/app/src/components/Sidebar/Publish/Publish.tsx b/app/src/components/Sidebar/Publish/Publish.tsx index 20c080a..e9f96e4 100644 --- a/app/src/components/Sidebar/Publish/Publish.tsx +++ b/app/src/components/Sidebar/Publish/Publish.tsx @@ -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' @@ -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 { @@ -132,6 +136,7 @@ const EditorMode = memo(function EditorMode(props: {
+
@@ -163,6 +168,20 @@ const FormatJsonButton = React.memo(function FormatJsonButton(props: { ) }) +const OpenFileButton = React.memo(function OpenFileButton(props: { editorMode: string; openFile: () => void }) { + return ( + + + + + + ) +}) + const PublishButton = memo(function PublishButton(props: { publish: () => void; focusEditor: () => void }) { const handleClickPublish = useCallback( (e: React.MouseEvent) => {