settings: convert flashCurrentProgram to react hook

This commit is contained in:
David Lechner
2022-03-15 16:54:49 -05:00
parent d18ad41e39
commit 861ea34d86
13 changed files with 100 additions and 217 deletions
+8 -2
View File
@@ -7,6 +7,7 @@ import { BleConnectionState } from '../ble/reducers';
import { BootloaderConnectionState } from '../lwp3-bootloader/reducers';
import * as notificationActions from '../notifications/actions';
import { useSelector } from '../reducers';
import { useSettingFlashCurrentProgram } from '../settings/hooks';
import OpenFileButton, { OpenFileButtonProps } from '../toolbar/OpenFileButton';
import { TooltipId } from '../toolbar/i18n';
import { flashFirmware } from './actions';
@@ -19,6 +20,7 @@ const FlashButton: React.VoidFunctionComponent<FlashButtonProps> = ({ id }) => {
const bleConnection = useSelector((s) => s.ble.connection);
const flashing = useSelector((s) => s.firmware.flashing);
const progress = useSelector((s) => s.firmware.progress);
const [isSettingFlashCurrentProgramEnabled] = useSettingFlashCurrentProgram();
const dispatch = useDispatch();
@@ -34,7 +36,9 @@ const FlashButton: React.VoidFunctionComponent<FlashButtonProps> = ({ id }) => {
}
showProgress={flashing}
progress={progress === null ? undefined : progress}
onFile={(data) => dispatch(flashFirmware(data))}
onFile={(data) =>
dispatch(flashFirmware(data, isSettingFlashCurrentProgramEnabled))
}
onReject={(file) =>
dispatch(
notificationActions.add(
@@ -43,7 +47,9 @@ const FlashButton: React.VoidFunctionComponent<FlashButtonProps> = ({ id }) => {
),
)
}
onClick={() => dispatch(flashFirmware(null))}
onClick={() =>
dispatch(flashFirmware(null, isSettingFlashCurrentProgramEnabled))
}
/>
);
};
+9 -4
View File
@@ -120,11 +120,16 @@ export type FailToFinishReason =
/**
* Creates a new action to flash firmware to a hub.
* @param data The firmware zip file data or `null` to get firmware later.
* @param flashCurrentProgram If true, flash the current program from the editor,
* otherwise use the program from firmware.zip.
*/
export const flashFirmware = createAction((data: ArrayBuffer | null) => ({
type: 'flashFirmware.action.flashFirmware',
data,
}));
export const flashFirmware = createAction(
(data: ArrayBuffer | null, flashCurrentProgram: boolean) => ({
type: 'flashFirmware.action.flashFirmware',
data,
flashCurrentProgram,
}),
);
/**
* Action that indicates flashing firmware started.
+42 -21
View File
@@ -86,7 +86,7 @@ describe('flashFirmware', () => {
// saga is triggered by this action
saga.put(flashFirmwareAction(null));
saga.put(flashFirmwareAction(null, false));
// first step is to connect to the hub bootloader
@@ -231,7 +231,7 @@ describe('flashFirmware', () => {
// saga is triggered by this action
saga.put(flashFirmwareAction(null));
saga.put(flashFirmwareAction(null, false));
// first step is to connect to the hub bootloader
@@ -280,7 +280,7 @@ describe('flashFirmware', () => {
// saga is triggered by this action
saga.put(flashFirmwareAction(null));
saga.put(flashFirmwareAction(null, false));
// first step is to connect to the hub bootloader
@@ -347,7 +347,7 @@ describe('flashFirmware', () => {
// saga is triggered by this action
saga.put(flashFirmwareAction(null));
saga.put(flashFirmwareAction(null, false));
// first step is to connect to the hub bootloader
@@ -410,7 +410,7 @@ describe('flashFirmware', () => {
// saga is triggered by this action
saga.put(flashFirmwareAction(null));
saga.put(flashFirmwareAction(null, false));
// first step is to connect to the hub bootloader
@@ -476,7 +476,7 @@ describe('flashFirmware', () => {
// saga is triggered by this action
saga.put(flashFirmwareAction(null));
saga.put(flashFirmwareAction(null, false));
// first step is to connect to the hub bootloader
@@ -535,7 +535,7 @@ describe('flashFirmware', () => {
// saga is triggered by this action
saga.put(flashFirmwareAction(null));
saga.put(flashFirmwareAction(null, false));
// first step is to connect to the hub bootloader
@@ -600,7 +600,7 @@ describe('flashFirmware', () => {
// saga is triggered by this action
saga.put(flashFirmwareAction(null));
saga.put(flashFirmwareAction(null, false));
// first step is to connect to the hub bootloader
@@ -681,7 +681,7 @@ describe('flashFirmware', () => {
// saga is triggered by this action
saga.put(flashFirmwareAction(null));
saga.put(flashFirmwareAction(null, false));
// first step is to connect to the hub bootloader
@@ -746,7 +746,7 @@ describe('flashFirmware', () => {
// saga is triggered by this action
saga.put(flashFirmwareAction(null));
saga.put(flashFirmwareAction(null, false));
// first step is to connect to the hub bootloader
@@ -840,7 +840,7 @@ describe('flashFirmware', () => {
// saga is triggered by this action
saga.put(flashFirmwareAction(null));
saga.put(flashFirmwareAction(null, false));
// first step is to connect to the hub bootloader
@@ -943,7 +943,7 @@ describe('flashFirmware', () => {
// saga is triggered by this action
saga.put(flashFirmwareAction(null));
saga.put(flashFirmwareAction(null, false));
// first step is to connect to the hub bootloader
@@ -1086,7 +1086,7 @@ describe('flashFirmware', () => {
// saga is triggered by this action
saga.put(flashFirmwareAction(null));
saga.put(flashFirmwareAction(null, false));
// first step is to connect to the hub bootloader
@@ -1228,7 +1228,10 @@ describe('flashFirmware', () => {
// saga is triggered by this action
saga.put(
flashFirmwareAction(await zip.generateAsync({ type: 'arraybuffer' })),
flashFirmwareAction(
await zip.generateAsync({ type: 'arraybuffer' }),
false,
),
);
// the first step is to compile main.py to .mpy
@@ -1372,7 +1375,10 @@ describe('flashFirmware', () => {
// saga is triggered by this action
saga.put(
flashFirmwareAction(await zip.generateAsync({ type: 'arraybuffer' })),
flashFirmwareAction(
await zip.generateAsync({ type: 'arraybuffer' }),
false,
),
);
// should get failure due to missing file
@@ -1415,7 +1421,10 @@ describe('flashFirmware', () => {
// saga is triggered by this action
saga.put(
flashFirmwareAction(await zip.generateAsync({ type: 'arraybuffer' })),
flashFirmwareAction(
await zip.generateAsync({ type: 'arraybuffer' }),
false,
),
);
// should get failure due to unsupported mpy-cross version
@@ -1457,7 +1466,10 @@ describe('flashFirmware', () => {
// saga is triggered by this action
saga.put(
flashFirmwareAction(await zip.generateAsync({ type: 'arraybuffer' })),
flashFirmwareAction(
await zip.generateAsync({ type: 'arraybuffer' }),
false,
),
);
// the first step is to compile main.py to .mpy
@@ -1512,7 +1524,10 @@ describe('flashFirmware', () => {
// saga is triggered by this action
saga.put(
flashFirmwareAction(await zip.generateAsync({ type: 'arraybuffer' })),
flashFirmwareAction(
await zip.generateAsync({ type: 'arraybuffer' }),
false,
),
);
// the first step is to compile main.py to .mpy
@@ -1568,7 +1583,10 @@ describe('flashFirmware', () => {
// saga is triggered by this action
saga.put(
flashFirmwareAction(await zip.generateAsync({ type: 'arraybuffer' })),
flashFirmwareAction(
await zip.generateAsync({ type: 'arraybuffer' }),
false,
),
);
// the first step is to compile main.py to .mpy
@@ -1627,7 +1645,10 @@ describe('flashFirmware', () => {
// saga is triggered by this action
saga.put(
flashFirmwareAction(await zip.generateAsync({ type: 'arraybuffer' })),
flashFirmwareAction(
await zip.generateAsync({ type: 'arraybuffer' }),
false,
),
);
// the first step is to compile main.py to .mpy
@@ -1715,7 +1736,7 @@ describe('flashFirmware', () => {
// saga is triggered by this action
saga.put(flashFirmwareAction(null));
saga.put(flashFirmwareAction(null, false));
// first step is to connect to the hub bootloader
+1 -5
View File
@@ -286,11 +286,7 @@ function* handleFlashFirmware(action: ReturnType<typeof flashFirmware>): Generat
let program: string | undefined = undefined;
const flashCurrentProgram = yield* select(
(s: RootState) => s.settings.flashCurrentProgram,
);
if (flashCurrentProgram) {
if (action.flashCurrentProgram) {
const editor = yield* getContext<EditorType>('editor');
// istanbul ignore if: it is a bug to dispatch this action with no current editor