|
| 1 | +import * as React from 'react'; |
| 2 | +import { inject, injectable } from 'inversify'; |
| 3 | +import { |
| 4 | + AbstractDialog, |
| 5 | + DialogError, |
| 6 | + DialogProps, |
| 7 | +} from '@theia/core/lib/browser/dialogs'; |
| 8 | +import { Widget } from '@phosphor/widgets'; |
| 9 | +import { Message } from '@phosphor/messaging'; |
| 10 | +import { ReactWidget } from '@theia/core/lib/browser/widgets/react-widget'; |
| 11 | +import { BoardsServiceProvider } from '../boards/boards-service-provider'; |
| 12 | +import { ArduinoSelect } from '../widgets/arduino-select'; |
| 13 | + |
| 14 | +export const FirmwareUploaderComponent = ({ |
| 15 | + boardsServiceClient, |
| 16 | +}: { |
| 17 | + boardsServiceClient: BoardsServiceProvider; |
| 18 | +}): React.ReactElement => { |
| 19 | + const [defaultValue, setdefaultValue] = React.useState<{ |
| 20 | + label: string; |
| 21 | + value: string; |
| 22 | + } | null>(null); |
| 23 | + |
| 24 | + const [disabled, setdisabled] = React.useState(true); |
| 25 | + |
| 26 | + const [portsboards, setportsboards] = React.useState< |
| 27 | + { |
| 28 | + label: string; |
| 29 | + value: string; |
| 30 | + }[] |
| 31 | + >([]); |
| 32 | + |
| 33 | + React.useEffect(() => { |
| 34 | + boardsServiceClient.onAvailableBoardsChanged((availableBoards) => { |
| 35 | + let disabled = false; |
| 36 | + let selectedBoard = -1; |
| 37 | + let boardsList = availableBoards |
| 38 | + .filter((board) => !!board.port) |
| 39 | + .map((board, i) => { |
| 40 | + if (board.selected) { |
| 41 | + selectedBoard = i; |
| 42 | + } |
| 43 | + return { |
| 44 | + label: board.name, |
| 45 | + value: board.port?.address || '', |
| 46 | + }; |
| 47 | + }); |
| 48 | + |
| 49 | + if (boardsList.length === 0) { |
| 50 | + disabled = true; |
| 51 | + boardsList = [ |
| 52 | + { label: 'No board connected to serial port', value: '' }, |
| 53 | + ]; |
| 54 | + selectedBoard = 0; |
| 55 | + } |
| 56 | + |
| 57 | + setdisabled(disabled); |
| 58 | + setportsboards(boardsList); |
| 59 | + setdefaultValue(boardsList[selectedBoard] || null); |
| 60 | + }); |
| 61 | + }, [boardsServiceClient]); |
| 62 | + return ( |
| 63 | + <div id="widget-container firmware-uploader"> |
| 64 | + <ArduinoSelect |
| 65 | + isDisabled={disabled} |
| 66 | + options={portsboards} |
| 67 | + value={defaultValue} |
| 68 | + /> |
| 69 | + </div> |
| 70 | + ); |
| 71 | +}; |
| 72 | + |
| 73 | +@injectable() |
| 74 | +export class UploadFirmwareDialogWidget extends ReactWidget { |
| 75 | + @inject(BoardsServiceProvider) |
| 76 | + protected readonly boardsServiceClient: BoardsServiceProvider; |
| 77 | + |
| 78 | + constructor() { |
| 79 | + super(); |
| 80 | + } |
| 81 | + |
| 82 | + protected render(): React.ReactNode { |
| 83 | + return ( |
| 84 | + <div className="selectBoardContainer"> |
| 85 | + <FirmwareUploaderComponent |
| 86 | + boardsServiceClient={this.boardsServiceClient} |
| 87 | + /> |
| 88 | + </div> |
| 89 | + ); |
| 90 | + } |
| 91 | +} |
| 92 | + |
| 93 | +@injectable() |
| 94 | +export class UploadFirmwareDialogProps extends DialogProps {} |
| 95 | + |
| 96 | +@injectable() |
| 97 | +export class UploadFirmwareDialog extends AbstractDialog<void> { |
| 98 | + @inject(UploadFirmwareDialogWidget) |
| 99 | + protected readonly widget: UploadFirmwareDialogWidget; |
| 100 | + |
| 101 | + constructor( |
| 102 | + @inject(UploadFirmwareDialogProps) |
| 103 | + protected readonly props: UploadFirmwareDialogProps |
| 104 | + ) { |
| 105 | + super({ title: 'Connectivity Firmware Updater' }); |
| 106 | + } |
| 107 | + |
| 108 | + protected setErrorMessage(error: DialogError): void { |
| 109 | + if (this.acceptButton) { |
| 110 | + this.acceptButton.disabled = !DialogError.getResult(error); |
| 111 | + } |
| 112 | + } |
| 113 | + |
| 114 | + get value(): void { |
| 115 | + return; |
| 116 | + } |
| 117 | + |
| 118 | + protected onAfterAttach(msg: Message): void { |
| 119 | + if (this.widget.isAttached) { |
| 120 | + Widget.detach(this.widget); |
| 121 | + } |
| 122 | + Widget.attach(this.widget, this.contentNode); |
| 123 | + super.onAfterAttach(msg); |
| 124 | + this.update(); |
| 125 | + } |
| 126 | + |
| 127 | + protected onUpdateRequest(msg: Message): void { |
| 128 | + super.onUpdateRequest(msg); |
| 129 | + this.widget.update(); |
| 130 | + } |
| 131 | + |
| 132 | + protected onActivateRequest(msg: Message): void { |
| 133 | + super.onActivateRequest(msg); |
| 134 | + this.widget.activate(); |
| 135 | + } |
| 136 | +} |
0 commit comments