From 8bae84e5f47597a1ebcb1c783b00aa91aa6afb19 Mon Sep 17 00:00:00 2001 From: Vincenzo Scamporlino Date: Thu, 25 May 2023 12:00:07 +0200 Subject: [PATCH] catalog-react: introduce UserOwnersFilter Signed-off-by: Vincenzo Scamporlino --- plugins/catalog-react/src/filters.ts | 46 ++++++++++++++++++++++++++++ 1 file changed, 46 insertions(+) diff --git a/plugins/catalog-react/src/filters.ts b/plugins/catalog-react/src/filters.ts index 1213ab9b7e..a5a39f9b02 100644 --- a/plugins/catalog-react/src/filters.ts +++ b/plugins/catalog-react/src/filters.ts @@ -200,8 +200,54 @@ export class EntityNamespaceFilter implements EntityFilter { } } +/** + * @public + */ +export class UserOwnersFilter implements EntityFilter { + private constructor( + readonly value: UserListFilterKind, + readonly refs?: string[], + ) {} + + static owned(ownershipEntityRefs: string[]) { + return new UserOwnersFilter('owned', ownershipEntityRefs); + } + + static all() { + return new UserOwnersFilter('all'); + } + + static starred(starredEntityRefs: string[]) { + return new UserOwnersFilter('starred', starredEntityRefs); + } + + getCatalogFilters(): Record { + if (this.value === 'owned') { + return { 'relations.ownedBy': this.refs ?? [] }; + } + if (this.value === 'starred') { + return { + 'metadata.name': this.refs?.map(e => parseEntityRef(e).name) ?? [], + }; + } + return {}; + } + + filterEntity(entity: Entity) { + if (this.value === 'starred') { + return this.refs?.includes(stringifyEntityRef(entity)) ?? true; + } + return true; + } + + toQueryValue(): string { + return this.value; + } +} + /** * Filters entities based on whatever the user has starred or owns them. + * @deprecated use UserOwnersFilter * @public */ export class UserListFilter implements EntityFilter {