Skip to content

Commit

Permalink
Merge pull request #293 from justinlampley/SaveBuildLogButton
Browse files Browse the repository at this point in the history
Add save build log
  • Loading branch information
jurgelenas authored Mar 17, 2022
2 parents 4d0570c + 9f1e423 commit ff78a89
Show file tree
Hide file tree
Showing 3 changed files with 71 additions and 2 deletions.
11 changes: 11 additions & 0 deletions src/ipc/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ export enum IpcRequest {
OpenLogsFolder = 'OPEN_LOGS_FOLDER',
UpdateBuildStatus = 'UPDATE_BUILD_STATUS',
ChooseFolder = 'CHOOSE_FOLDER',
SaveFile = 'SAVE_FILE',
}

export interface OpenFileLocationRequestBody {
Expand All @@ -17,3 +18,13 @@ export interface ChooseFolderResponseBody {
success: boolean;
directoryPath: string;
}

export interface SaveFileRequestBody {
defaultPath?: string;
data: string | Uint8Array;
}

export interface SaveFileResponseBody {
success: boolean;
path: string;
}
24 changes: 24 additions & 0 deletions src/main.dev.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,15 @@ import { autoUpdater } from 'electron-updater';
import log from 'electron-log';
import mkdirp from 'mkdirp';
import winston from 'winston';
import fs from 'fs';
import MenuBuilder from './menu';
import ApiServer from './api';
import {
ChooseFolderResponseBody,
IpcRequest,
OpenFileLocationRequestBody,
SaveFileRequestBody,
SaveFileResponseBody,
UpdateBuildStatusRequestBody,
} from './ipc';
import WinstonLoggerService from './api/src/logger/WinstonLogger';
Expand Down Expand Up @@ -500,3 +503,24 @@ ipcMain.on(
});
}
);

ipcMain.handle(
IpcRequest.SaveFile,
async (_, arg: SaveFileRequestBody): Promise<SaveFileResponseBody> => {
const result = await dialog.showSaveDialog({
title: 'Save File',
defaultPath: arg.defaultPath,
});
if (result.canceled || !result.filePath || result.filePath.length === 0) {
return {
success: false,
path: '',
};
}
await fs.promises.writeFile(result.filePath, arg.data);
return {
success: true,
path: result.filePath,
};
}
);
38 changes: 36 additions & 2 deletions src/ui/views/ConfiguratorView/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ import {
} from '@mui/material';
import SettingsIcon from '@mui/icons-material/Settings';
import { ipcRenderer } from 'electron';
import { ContentCopy, NetworkWifi } from '@mui/icons-material';
import { ContentCopy, NetworkWifi, Save } from '@mui/icons-material';
import FirmwareVersionForm from '../../components/FirmwareVersionForm';
import DeviceTargetForm from '../../components/DeviceTargetForm';
import DeviceOptionsForm, {
Expand Down Expand Up @@ -64,6 +64,8 @@ import BuildResponse from '../../components/BuildResponse';
import {
IpcRequest,
OpenFileLocationRequestBody,
SaveFileRequestBody,
SaveFileResponseBody,
UpdateBuildStatusRequestBody,
} from '../../../ipc';
import UserDefinesValidator from './UserDefinesValidator';
Expand Down Expand Up @@ -773,6 +775,30 @@ const ConfiguratorView: FunctionComponent<ConfiguratorViewProps> = (props) => {
setDeviceSelectErrorDialogOpen(false);
}, []);

const saveBuildLogToFile = useCallback(async () => {
const saveFileRequestBody: SaveFileRequestBody = {
data: logs,
defaultPath: `ExpressLRSBuildLog_${new Date()
.toISOString()
.replace(/[^0-9]/gi, '')}.txt`,
};

const result: SaveFileResponseBody = await ipcRenderer.invoke(
IpcRequest.SaveFile,
saveFileRequestBody
);

if (result.success) {
const openFileLocationRequestBody: OpenFileLocationRequestBody = {
path: result.path,
};
ipcRenderer.send(
IpcRequest.OpenFileLocation,
openFileLocationRequestBody
);
}
}, [logs]);

return (
<MainLayout>
{viewState === ViewState.Configuration && (
Expand Down Expand Up @@ -1012,13 +1038,21 @@ const ConfiguratorView: FunctionComponent<ConfiguratorViewProps> = (props) => {
<Box>Logs</Box>
<Box>
<IconButton
aria-label="Copy the logs"
aria-label="Copy log to clipboard"
title="Copy log to clipboard"
onClick={async () => {
await navigator.clipboard.writeText(logs);
}}
>
<ContentCopy />
</IconButton>
<IconButton
aria-label="Save log to file"
title="Save log to file"
onClick={saveBuildLogToFile}
>
<Save />
</IconButton>
</Box>
</Box>
}
Expand Down

0 comments on commit ff78a89

Please sign in to comment.