mirror of
https://github.com/pybricks/pybricks-code.git
synced 2026-09-12 17:45:22 +00:00
settings: convert hubName to react hook
This commit is contained in:
@@ -7,7 +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 { useSettingFlashCurrentProgram, useSettingHubName } from '../settings/hooks';
|
||||
import OpenFileButton, { OpenFileButtonProps } from '../toolbar/OpenFileButton';
|
||||
import { TooltipId } from '../toolbar/i18n';
|
||||
import { flashFirmware } from './actions';
|
||||
@@ -21,6 +21,7 @@ const FlashButton: React.VoidFunctionComponent<FlashButtonProps> = ({ id }) => {
|
||||
const flashing = useSelector((s) => s.firmware.flashing);
|
||||
const progress = useSelector((s) => s.firmware.progress);
|
||||
const [isSettingFlashCurrentProgramEnabled] = useSettingFlashCurrentProgram();
|
||||
const { hubName } = useSettingHubName();
|
||||
|
||||
const dispatch = useDispatch();
|
||||
|
||||
@@ -37,7 +38,9 @@ const FlashButton: React.VoidFunctionComponent<FlashButtonProps> = ({ id }) => {
|
||||
showProgress={flashing}
|
||||
progress={progress === null ? undefined : progress}
|
||||
onFile={(data) =>
|
||||
dispatch(flashFirmware(data, isSettingFlashCurrentProgramEnabled))
|
||||
dispatch(
|
||||
flashFirmware(data, isSettingFlashCurrentProgramEnabled, hubName),
|
||||
)
|
||||
}
|
||||
onReject={(file) =>
|
||||
dispatch(
|
||||
@@ -48,7 +51,9 @@ const FlashButton: React.VoidFunctionComponent<FlashButtonProps> = ({ id }) => {
|
||||
)
|
||||
}
|
||||
onClick={() =>
|
||||
dispatch(flashFirmware(null, isSettingFlashCurrentProgramEnabled))
|
||||
dispatch(
|
||||
flashFirmware(null, isSettingFlashCurrentProgramEnabled, hubName),
|
||||
)
|
||||
}
|
||||
/>
|
||||
);
|
||||
|
||||
@@ -122,12 +122,14 @@ export type FailToFinishReason =
|
||||
* @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.
|
||||
* @param hubName A custom hub name or an empty string to use the default name.
|
||||
*/
|
||||
export const flashFirmware = createAction(
|
||||
(data: ArrayBuffer | null, flashCurrentProgram: boolean) => ({
|
||||
(data: ArrayBuffer | null, flashCurrentProgram: boolean, hubName: string) => ({
|
||||
type: 'flashFirmware.action.flashFirmware',
|
||||
data,
|
||||
flashCurrentProgram,
|
||||
hubName,
|
||||
}),
|
||||
);
|
||||
|
||||
|
||||
+21
-16
@@ -82,11 +82,9 @@ describe('flashFirmware', () => {
|
||||
nextMessageId: createCountFunc(),
|
||||
});
|
||||
|
||||
saga.updateState({ settings: { hubName: 'test name' } });
|
||||
|
||||
// saga is triggered by this action
|
||||
|
||||
saga.put(flashFirmwareAction(null, false));
|
||||
saga.put(flashFirmwareAction(null, false, 'test name'));
|
||||
|
||||
// first step is to connect to the hub bootloader
|
||||
|
||||
@@ -231,7 +229,7 @@ describe('flashFirmware', () => {
|
||||
|
||||
// saga is triggered by this action
|
||||
|
||||
saga.put(flashFirmwareAction(null, false));
|
||||
saga.put(flashFirmwareAction(null, false, ''));
|
||||
|
||||
// first step is to connect to the hub bootloader
|
||||
|
||||
@@ -280,7 +278,7 @@ describe('flashFirmware', () => {
|
||||
|
||||
// saga is triggered by this action
|
||||
|
||||
saga.put(flashFirmwareAction(null, false));
|
||||
saga.put(flashFirmwareAction(null, false, ''));
|
||||
|
||||
// first step is to connect to the hub bootloader
|
||||
|
||||
@@ -347,7 +345,7 @@ describe('flashFirmware', () => {
|
||||
|
||||
// saga is triggered by this action
|
||||
|
||||
saga.put(flashFirmwareAction(null, false));
|
||||
saga.put(flashFirmwareAction(null, false, ''));
|
||||
|
||||
// first step is to connect to the hub bootloader
|
||||
|
||||
@@ -410,7 +408,7 @@ describe('flashFirmware', () => {
|
||||
|
||||
// saga is triggered by this action
|
||||
|
||||
saga.put(flashFirmwareAction(null, false));
|
||||
saga.put(flashFirmwareAction(null, false, ''));
|
||||
|
||||
// first step is to connect to the hub bootloader
|
||||
|
||||
@@ -476,7 +474,7 @@ describe('flashFirmware', () => {
|
||||
|
||||
// saga is triggered by this action
|
||||
|
||||
saga.put(flashFirmwareAction(null, false));
|
||||
saga.put(flashFirmwareAction(null, false, ''));
|
||||
|
||||
// first step is to connect to the hub bootloader
|
||||
|
||||
@@ -535,7 +533,7 @@ describe('flashFirmware', () => {
|
||||
|
||||
// saga is triggered by this action
|
||||
|
||||
saga.put(flashFirmwareAction(null, false));
|
||||
saga.put(flashFirmwareAction(null, false, ''));
|
||||
|
||||
// first step is to connect to the hub bootloader
|
||||
|
||||
@@ -600,7 +598,7 @@ describe('flashFirmware', () => {
|
||||
|
||||
// saga is triggered by this action
|
||||
|
||||
saga.put(flashFirmwareAction(null, false));
|
||||
saga.put(flashFirmwareAction(null, false, ''));
|
||||
|
||||
// first step is to connect to the hub bootloader
|
||||
|
||||
@@ -681,7 +679,7 @@ describe('flashFirmware', () => {
|
||||
|
||||
// saga is triggered by this action
|
||||
|
||||
saga.put(flashFirmwareAction(null, false));
|
||||
saga.put(flashFirmwareAction(null, false, ''));
|
||||
|
||||
// first step is to connect to the hub bootloader
|
||||
|
||||
@@ -746,7 +744,7 @@ describe('flashFirmware', () => {
|
||||
|
||||
// saga is triggered by this action
|
||||
|
||||
saga.put(flashFirmwareAction(null, false));
|
||||
saga.put(flashFirmwareAction(null, false, ''));
|
||||
|
||||
// first step is to connect to the hub bootloader
|
||||
|
||||
@@ -840,7 +838,7 @@ describe('flashFirmware', () => {
|
||||
|
||||
// saga is triggered by this action
|
||||
|
||||
saga.put(flashFirmwareAction(null, false));
|
||||
saga.put(flashFirmwareAction(null, false, ''));
|
||||
|
||||
// first step is to connect to the hub bootloader
|
||||
|
||||
@@ -943,7 +941,7 @@ describe('flashFirmware', () => {
|
||||
|
||||
// saga is triggered by this action
|
||||
|
||||
saga.put(flashFirmwareAction(null, false));
|
||||
saga.put(flashFirmwareAction(null, false, ''));
|
||||
|
||||
// first step is to connect to the hub bootloader
|
||||
|
||||
@@ -1086,7 +1084,7 @@ describe('flashFirmware', () => {
|
||||
|
||||
// saga is triggered by this action
|
||||
|
||||
saga.put(flashFirmwareAction(null, false));
|
||||
saga.put(flashFirmwareAction(null, false, ''));
|
||||
|
||||
// first step is to connect to the hub bootloader
|
||||
|
||||
@@ -1231,6 +1229,7 @@ describe('flashFirmware', () => {
|
||||
flashFirmwareAction(
|
||||
await zip.generateAsync({ type: 'arraybuffer' }),
|
||||
false,
|
||||
'',
|
||||
),
|
||||
);
|
||||
|
||||
@@ -1378,6 +1377,7 @@ describe('flashFirmware', () => {
|
||||
flashFirmwareAction(
|
||||
await zip.generateAsync({ type: 'arraybuffer' }),
|
||||
false,
|
||||
'',
|
||||
),
|
||||
);
|
||||
|
||||
@@ -1424,6 +1424,7 @@ describe('flashFirmware', () => {
|
||||
flashFirmwareAction(
|
||||
await zip.generateAsync({ type: 'arraybuffer' }),
|
||||
false,
|
||||
'',
|
||||
),
|
||||
);
|
||||
|
||||
@@ -1469,6 +1470,7 @@ describe('flashFirmware', () => {
|
||||
flashFirmwareAction(
|
||||
await zip.generateAsync({ type: 'arraybuffer' }),
|
||||
false,
|
||||
'',
|
||||
),
|
||||
);
|
||||
|
||||
@@ -1527,6 +1529,7 @@ describe('flashFirmware', () => {
|
||||
flashFirmwareAction(
|
||||
await zip.generateAsync({ type: 'arraybuffer' }),
|
||||
false,
|
||||
'',
|
||||
),
|
||||
);
|
||||
|
||||
@@ -1586,6 +1589,7 @@ describe('flashFirmware', () => {
|
||||
flashFirmwareAction(
|
||||
await zip.generateAsync({ type: 'arraybuffer' }),
|
||||
false,
|
||||
'',
|
||||
),
|
||||
);
|
||||
|
||||
@@ -1648,6 +1652,7 @@ describe('flashFirmware', () => {
|
||||
flashFirmwareAction(
|
||||
await zip.generateAsync({ type: 'arraybuffer' }),
|
||||
false,
|
||||
'',
|
||||
),
|
||||
);
|
||||
|
||||
@@ -1736,7 +1741,7 @@ describe('flashFirmware', () => {
|
||||
|
||||
// saga is triggered by this action
|
||||
|
||||
saga.put(flashFirmwareAction(null, false));
|
||||
saga.put(flashFirmwareAction(null, false, ''));
|
||||
|
||||
// first step is to connect to the hub bootloader
|
||||
|
||||
|
||||
+11
-4
@@ -182,6 +182,7 @@ function* firmwareIterator(data: DataView, maxSize: number): Generator<number> {
|
||||
function* loadFirmware(
|
||||
data: ArrayBuffer,
|
||||
program: string | undefined,
|
||||
hubName: string,
|
||||
): SagaGenerator<{ firmware: Uint8Array; deviceId: HubType }> {
|
||||
const [reader, readerErr] = yield* call(() => maybe(FirmwareReader.load(data)));
|
||||
|
||||
@@ -247,8 +248,6 @@ function* loadFirmware(
|
||||
|
||||
// if the firmware supports it, we can set a custom hub name
|
||||
if (metadata['max-hub-name-size']) {
|
||||
const hubName = yield* select((s: RootState) => s.settings.hubName);
|
||||
|
||||
// empty string means use default name (don't write over firmware)
|
||||
if (hubName) {
|
||||
firmware.set(encodeHubName(hubName, metadata), metadata['hub-name-offset']);
|
||||
@@ -299,7 +298,11 @@ function* handleFlashFirmware(action: ReturnType<typeof flashFirmware>): Generat
|
||||
}
|
||||
|
||||
if (action.data !== null) {
|
||||
({ firmware, deviceId } = yield* loadFirmware(action.data, program));
|
||||
({ firmware, deviceId } = yield* loadFirmware(
|
||||
action.data,
|
||||
program,
|
||||
action.hubName,
|
||||
));
|
||||
}
|
||||
|
||||
yield* put(connect());
|
||||
@@ -341,7 +344,11 @@ function* handleFlashFirmware(action: ReturnType<typeof flashFirmware>): Generat
|
||||
}
|
||||
|
||||
const data = yield* call(() => response.arrayBuffer());
|
||||
({ firmware, deviceId } = yield* loadFirmware(data, program));
|
||||
({ firmware, deviceId } = yield* loadFirmware(
|
||||
data,
|
||||
program,
|
||||
action.hubName,
|
||||
));
|
||||
|
||||
if (deviceId !== undefined && info.hubType !== deviceId) {
|
||||
yield* put(didFailToFinish(FailToFinishReasonType.DeviceMismatch));
|
||||
|
||||
Reference in New Issue
Block a user