Skip to content

Commit a0463a0

Browse files
author
Akos Kitta
committed
Basic filter.
Signed-off-by: Akos Kitta <[email protected]>
1 parent d769858 commit a0463a0

File tree

3 files changed

+65
-4
lines changed

3 files changed

+65
-4
lines changed

arduino-ide-extension/src/browser/widgets/component-list/component-list.tsx

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -100,8 +100,11 @@ export class ComponentList<T extends ArduinoComponent> extends React.Component<
100100

101101
private clear(index: number): void {
102102
this.cache.clear(index, 0);
103-
if (this.list) {
104-
this.list.recomputeRowHeights(index);
103+
this.list?.recomputeRowHeights(index);
104+
// Update the last item if the if the one before was updated
105+
if (index === this.props.items.length - 2) {
106+
this.cache.clear(index + 1, 0);
107+
this.list?.recomputeRowHeights(index + 1);
105108
}
106109
}
107110

arduino-ide-extension/src/common/protocol/library-service.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -118,6 +118,8 @@ export interface LibraryPackage extends ArduinoComponent {
118118
readonly exampleUris: string[];
119119
readonly location: LibraryLocation;
120120
readonly installDirUri?: string;
121+
readonly category?: string;
122+
readonly maintainer?: string;
121123
}
122124
export namespace LibraryPackage {
123125
export function is(arg: any): arg is LibraryPackage {

arduino-ide-extension/src/node/library-service-impl.ts

Lines changed: 58 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import {
33
LibraryDependency,
44
LibraryLocation,
55
LibraryPackage,
6+
LibrarySearch,
67
LibraryService,
78
} from '../common/protocol/library-service';
89
import { CoreClientAware } from './core-client-provider';
@@ -46,7 +47,7 @@ export class LibraryServiceImpl
4647
protected readonly notificationServer: NotificationServiceServer;
4748

4849
@duration()
49-
async search(options: { query?: string }): Promise<LibraryPackage[]> {
50+
async search(options: LibrarySearch): Promise<LibraryPackage[]> {
5051
const coreClient = await this.coreClient;
5152
const { client, instance } = coreClient;
5253

@@ -104,7 +105,60 @@ export class LibraryServiceImpl
104105
);
105106
});
106107

107-
return items;
108+
const typePredicate = this.typePredicate(options);
109+
const topicPredicate = this.topicPredicate(options);
110+
return items.filter((item) => typePredicate(item) && topicPredicate(item));
111+
}
112+
113+
private typePredicate(
114+
options: LibrarySearch
115+
): (item: LibraryPackage) => boolean {
116+
if (!options.type) {
117+
return () => true;
118+
}
119+
switch (options.type) {
120+
case 'All':
121+
return () => true;
122+
case 'Arduino':
123+
return ({ maintainer }: LibraryPackage) =>
124+
maintainer === 'Arduino <[email protected]>';
125+
case 'Installed':
126+
return ({ installedVersion }: LibraryPackage) => !!installedVersion;
127+
case 'Retired':
128+
return ({ deprecated }: LibraryPackage) => deprecated;
129+
case 'Updatable':
130+
return (item: LibraryPackage) => {
131+
const { installedVersion } = item;
132+
if (!installedVersion) {
133+
return false;
134+
}
135+
const latestVersion = item.availableVersions[0];
136+
if (!latestVersion) {
137+
console.warn(
138+
`Installed version ${installedVersion} is available for ${item.name}, but available versions are mission. Skipping.`
139+
);
140+
return false;
141+
}
142+
const result = Installable.Version.COMPARATOR(
143+
latestVersion,
144+
installedVersion
145+
);
146+
return result > 0;
147+
};
148+
default: {
149+
console.error('unhandled type: ' + options.type);
150+
return () => true;
151+
}
152+
}
153+
}
154+
155+
private topicPredicate(
156+
options: LibrarySearch
157+
): (item: LibraryPackage) => boolean {
158+
if (!options.topic || options.topic === 'All') {
159+
return () => true;
160+
}
161+
return (item: LibraryPackage) => item.category === options.topic;
108162
}
109163

110164
async list({
@@ -409,5 +463,7 @@ function toLibrary(
409463
description: lib.getSentence(),
410464
moreInfoLink: lib.getWebsite(),
411465
summary: lib.getParagraph(),
466+
category: lib.getCategory(),
467+
maintainer: lib.getMaintainer(),
412468
};
413469
}

0 commit comments

Comments
 (0)