Skip to content

Commit 894d0af

Browse files
author
Akos Kitta
committed
fix: clean up typings for the UI
1 parent 033ed4e commit 894d0af

File tree

3 files changed

+39
-43
lines changed

3 files changed

+39
-43
lines changed

arduino-ide-extension/src/browser/boards/boards-service-provider.ts

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -89,18 +89,19 @@ export interface EditBoardsConfigAction {
8989
(params?: EditBoardsConfigActionParams): void;
9090
}
9191

92-
export interface BoardListUI extends BoardList {
92+
export interface BoardListUIActions {
9393
/**
9494
* Sets the frontend's port and board configuration according to the params.
9595
*/
96-
onSelect: SelectBoardsConfigAction;
96+
readonly select: SelectBoardsConfigAction;
9797
/**
9898
* Opens up the boards config dialog with the port and (optional) board to select in the dialog.
9999
* Calling this function does not immediately change the frontend's port and board config, but
100100
* preselects items in the dialog.
101101
*/
102-
onEdit: EditBoardsConfigAction;
102+
readonly edit: EditBoardsConfigAction;
103103
}
104+
export type BoardListUI = BoardList & BoardListUIActions;
104105

105106
@injectable()
106107
export class BoardListDumper implements Disposable {
@@ -195,6 +196,10 @@ export class BoardsServiceProvider
195196
.finally(() => this._ready.resolve());
196197
}
197198

199+
onStop(): void {
200+
this.boardListDumper?.dispose();
201+
}
202+
198203
private async maybeUpdateSelectedBoard(platformDidInstallEvent: {
199204
item: BoardsPackage;
200205
}): Promise<void> {
@@ -237,10 +242,6 @@ export class BoardsServiceProvider
237242
}
238243
}
239244

240-
onStop(): void {
241-
this.boardListDumper?.dispose();
242-
}
243-
244245
registerCommands(registry: CommandRegistry): void {
245246
registry.registerCommand(USE_INHERITED_CONFIG, {
246247
execute: (
@@ -305,8 +306,8 @@ export class BoardsServiceProvider
305306

306307
private createBoardListUI(boardList: BoardList): BoardListUI {
307308
return Object.assign(boardList, {
308-
onSelect: this.onBoardsConfigSelect.bind(this),
309-
onEdit: this.onBoardsConfigEdit.bind(this),
309+
select: this.onBoardsConfigSelect.bind(this),
310+
edit: this.onBoardsConfigEdit.bind(this),
310311
});
311312
}
312313

arduino-ide-extension/src/browser/boards/boards-toolbar-item.tsx

Lines changed: 25 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -10,10 +10,7 @@ import React from '@theia/core/shared/react';
1010
import ReactDOM from '@theia/core/shared/react-dom';
1111
import classNames from 'classnames';
1212
import { boardIdentifierLabel, Port } from '../../common/protocol';
13-
import {
14-
BoardListItem,
15-
BoardListItemUI,
16-
} from '../../common/protocol/board-list';
13+
import { BoardListItemUI } from '../../common/protocol/board-list';
1714
import { assertUnreachable } from '../../common/utils';
1815
import { BoardListUI, BoardsServiceProvider } from './boards-service-provider';
1916

@@ -29,13 +26,11 @@ export namespace BoardsDropDown {
2926
readonly coords: BoardsDropDownListCoords | 'hidden';
3027
readonly boardList: BoardListUI;
3128
readonly openBoardsConfig: () => void;
29+
readonly hide: () => void;
3230
}
3331
}
3432

35-
export class BoardListDropDown extends React.Component<
36-
BoardsDropDown.Props,
37-
{ expandedItems: BoardListItem[] }
38-
> {
33+
export class BoardListDropDown extends React.Component<BoardsDropDown.Props> {
3934
private dropdownElement: HTMLElement;
4035
private listRef: React.RefObject<HTMLDivElement>;
4136

@@ -49,7 +44,6 @@ export class BoardListDropDown extends React.Component<
4944
document.body.appendChild(list);
5045
this.dropdownElement = list;
5146
}
52-
this.state = { expandedItems: [] };
5347
}
5448

5549
override componentDidUpdate(prevProps: BoardsDropDown.Props): void {
@@ -105,14 +99,15 @@ export class BoardListDropDown extends React.Component<
10599
}
106100

107101
private readonly onDefaultAction = (item: BoardListItemUI): unknown => {
108-
const { boardList } = this.props;
102+
const { boardList, hide } = this.props;
109103
const { type, params } = item.defaultAction;
104+
hide();
110105
switch (type) {
111106
case 'select-boards-config': {
112-
return boardList.onSelect(params);
107+
return boardList.select(params);
113108
}
114109
case 'edit-boards-config': {
115-
return boardList.onEdit(params);
110+
return boardList.edit(params);
116111
}
117112
default:
118113
return assertUnreachable(type);
@@ -166,11 +161,20 @@ export class BoardListDropDown extends React.Component<
166161
}
167162

168163
private renderActions(item: BoardListItemUI): React.ReactNode {
169-
const { boardList } = this.props;
164+
const { boardList, hide } = this.props;
170165
const { revert, edit } = item.otherActions;
171166
if (!edit && !revert) {
172167
return undefined;
173168
}
169+
const handleOnClick = (
170+
event: React.MouseEvent<HTMLElement, MouseEvent>,
171+
callback: () => void
172+
) => {
173+
event.preventDefault();
174+
event.stopPropagation();
175+
hide();
176+
callback();
177+
};
174178
return (
175179
<div className={TabBarToolbar.Styles.TAB_BAR_TOOLBAR}>
176180
{edit && (
@@ -185,11 +189,9 @@ export class BoardListDropDown extends React.Component<
185189
'arduino/board/editBoardsConfig',
186190
'Edit Board and Port...'
187191
)}
188-
onClick={(event) => {
189-
event.preventDefault();
190-
event.stopPropagation();
191-
boardList.onEdit(edit.params);
192-
}}
192+
onClick={(event) =>
193+
handleOnClick(event, () => boardList.edit(edit.params))
194+
}
193195
/>
194196
}
195197
</div>
@@ -208,11 +210,9 @@ export class BoardListDropDown extends React.Component<
208210
boardIdentifierLabel(revert.params.selectedBoard),
209211
item.labels.portLabel
210212
)}
211-
onClick={(event) => {
212-
event.preventDefault();
213-
event.stopPropagation();
214-
boardList.onSelect(revert.params);
215-
}}
213+
onClick={(event) =>
214+
handleOnClick(event, () => boardList.select(revert.params))
215+
}
216216
/>
217217
}
218218
</div>
@@ -292,16 +292,6 @@ export class BoardsToolBarItem extends React.Component<
292292
'fa',
293293
protocolIcon
294294
);
295-
const originalOnSelect = boardList.onSelect;
296-
boardList['onSelect'] = (params) => {
297-
this.hide();
298-
return originalOnSelect.bind(boardList)(params);
299-
};
300-
const originalOnEdit = boardList.onEdit;
301-
boardList['onEdit'] = (params) => {
302-
this.hide();
303-
return originalOnEdit.bind(boardList)(params);
304-
};
305295
return (
306296
<React.Fragment>
307297
<div
@@ -325,7 +315,8 @@ export class BoardsToolBarItem extends React.Component<
325315
<BoardListDropDown
326316
coords={coords}
327317
boardList={boardList}
328-
openBoardsConfig={() => boardList.onEdit({ query: '' })}
318+
openBoardsConfig={() => boardList.edit({ query: '' })}
319+
hide={this.hide}
329320
/>
330321
</React.Fragment>
331322
);

arduino-ide-extension/src/common/protocol/board-list.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -667,6 +667,10 @@ function createEditAction(item: BoardListItem): EditBoardsConfigAction {
667667
const uniqueBoardName = findUniqueBoardName(item);
668668
params.query = uniqueBoardName ?? '';
669669
params.searchSet = item.boards;
670+
} else if (isInferredBoardListItem(item)) {
671+
params.query = item.inferredBoard.name;
672+
} else if (item.board) {
673+
params.query = item.board.name;
670674
} else {
671675
params.query = '';
672676
}

0 commit comments

Comments
 (0)