feat: save value to file

This commit is contained in:
Björn Dalfors
2024-05-27 14:38:17 +02:00
parent 1ba0d07757
commit f17640c9db
5 changed files with 121 additions and 10 deletions

View File

@@ -17,31 +17,36 @@ 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()
))
if (file) {
dispatch(
setPayload(Base64Message.fromBuffer(file.data).toUnicodeString()
))
}
} catch (error) {
dispatch(showError(error))
}
}
type FileParameters = {
name: string,
data: Buffer
}
async function getFileContent(): Promise<FileParameters> {
async function getFileContent(): Promise<FileParameters | undefined> {
const rejectReasons = {
noFileSelected: 'No file selected',
errorReadingFile: 'Error reading file'
}
const openDialogReturnValue = await rendererRpc.call(makeOpenDialogRpc(), {
const { canceled, filePaths } = await rendererRpc.call(makeOpenDialogRpc(), {
properties: ['openFile'],
securityScopedBookmarks: true,
})
const selectedFile = openDialogReturnValue.filePaths && openDialogReturnValue.filePaths[0]
if (canceled) {
return
}
const selectedFile = filePaths[0]
if (!selectedFile) {
throw rejectReasons.noFileSelected
}