Skip to content

Commit

Permalink
Never try to filter when input query is empty
Browse files Browse the repository at this point in the history
  • Loading branch information
bobvandevijver committed Sep 18, 2024
1 parent 5c4cb06 commit 3d68bc2
Showing 1 changed file with 26 additions and 16 deletions.
42 changes: 26 additions & 16 deletions app.ts
Original file line number Diff line number Diff line change
Expand Up @@ -59,25 +59,29 @@ module.exports = class TuyaOAuth2App extends OAuth2App {
};

const autocompleteListener = async (
query: string,
query: string | undefined,
args: DeviceArgs,
filter: ({ value }: { value: unknown }) => boolean,
): Promise<ArgumentAutocompleteResults> => {
function convert(
values: TuyaStatusResponse | Array<TuyaDeviceDataPoint>,
dataPoints: boolean,
): ArgumentAutocompleteResults {
return values
.filter(filter)
.filter(({ code }: { code: string }) => {
return code.toLowerCase().includes(query.toLowerCase());
})
.map(value => ({
name: value.code,
id: value.code,
title: value.code,
dataPoint: dataPoints,
}));
values = values.filter(filter);

const trimmedQuery = (query ?? '').trim();
if (trimmedQuery) {
values = values.filter(({ code }: { code: string }) =>
code.toLowerCase().includes(trimmedQuery.toLowerCase()),
);
}

return values.map(value => ({
name: value.code,
id: value.code,
title: value.code,
dataPoint: dataPoints,
}));
}

const deviceId = args.device.getData().deviceId;
Expand Down Expand Up @@ -221,7 +225,7 @@ module.exports = class TuyaOAuth2App extends OAuth2App {
const client = this.getFirstSavedOAuth2Client();
await client.triggerScene(scene.id);
})
.registerArgumentAutocompleteListener('scene', async (query: string) => {
.registerArgumentAutocompleteListener('scene', async (query?: string) => {
if (!this.apiCache.has(SCENE_CACHE_KEY)) {
this.log('Retrieving available scenes');
const client = this.getFirstSavedOAuth2Client();
Expand Down Expand Up @@ -259,9 +263,15 @@ module.exports = class TuyaOAuth2App extends OAuth2App {

this.apiCache.set(SCENE_CACHE_KEY, scenes);
}
return (this.apiCache.get<HomeyTuyaScene[]>(SCENE_CACHE_KEY) ?? []).filter(scene =>
scene.name.toLowerCase().includes(query.toLowerCase()),
);

const scenes = this.apiCache.get<HomeyTuyaScene[]>(SCENE_CACHE_KEY) ?? [];

const trimmedQuery = (query ?? '').trim();
if (!trimmedQuery) {
return scenes;
}

return scenes.filter(scene => scene.name.toLowerCase().includes(trimmedQuery.toLowerCase()));
});

this.log('Tuya started');
Expand Down

0 comments on commit 3d68bc2

Please sign in to comment.