-
Notifications
You must be signed in to change notification settings - Fork 20
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix(react-components): asset search hook to support more than 1000 re…
…sults (#4655) * re-written asset search hook to support more than 1000 results * fixed nextCursor issue which was making duplicate data for each fetchNextPage call * fixed rule based color handling asset mapping * addressed review comments * lint fix * removed unused query key * update assetmapping variable name to match its content * updated storybook example to load more option
- Loading branch information
Showing
5 changed files
with
211 additions
and
94 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
/*! | ||
* Copyright 2024 Cognite AS | ||
*/ | ||
import { type CogniteClient, type CursorResponse, type Asset } from '@cognite/sdk'; | ||
import { buildFilter } from '../../utilities/buildFilter'; | ||
|
||
const sortOption = [{ property: ['_score_'] }]; | ||
|
||
export const getAssetsList = async ( | ||
sdk: CogniteClient, | ||
{ | ||
query, | ||
cursor, | ||
limit = 1000, | ||
sort = sortOption, | ||
aggregatedProperties = ['path'] | ||
}: { | ||
query: string; | ||
cursor?: string; | ||
limit?: number; | ||
sort?: Array<{ property: string[] }>; | ||
aggregatedProperties?: string[]; | ||
} | ||
): Promise<{ items: Asset[]; nextCursor: string | undefined }> => { | ||
const advancedFilter = buildFilter(query); | ||
|
||
return await sdk | ||
.post<CursorResponse<Asset[]>>(`/api/v1/projects/${sdk.project}/assets/list`, { | ||
headers: { | ||
'cdf-version': 'alpha' | ||
}, | ||
data: { | ||
limit, | ||
sort, | ||
advancedFilter, | ||
aggregatedProperties, | ||
cursor | ||
} | ||
}) | ||
.then(({ data }) => { | ||
return { | ||
items: data.items, | ||
nextCursor: data.nextCursor | ||
}; | ||
}); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
/*! | ||
* Copyright 2024 Cognite AS | ||
*/ | ||
|
||
export const buildFilter = (query: string): any => { | ||
if (query === '') { | ||
return undefined; | ||
} | ||
const conditions = ['search'] | ||
.map((condition) => [ | ||
{ | ||
[condition]: { | ||
property: ['name'], | ||
value: query | ||
} | ||
}, | ||
{ | ||
[condition]: { | ||
property: ['description'], | ||
value: query | ||
} | ||
} | ||
]) | ||
.flat(); | ||
|
||
return { | ||
and: [ | ||
{ | ||
or: conditions | ||
} | ||
] | ||
}; | ||
}; |
Oops, something went wrong.