UNPKG

@finos/legend-application-marketplace

Version:
228 lines 8.63 kB
/** * Copyright (c) 2026-present, Goldman Sachs * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ import { action, computed, flow, makeObservable, observable } from 'mobx'; import { ActionState, assertErrorThrown, isNonEmptyString, LogEvent, } from '@finos/legend-shared'; import { FieldSearchType, GroupedFieldSearchResponse, } from '@finos/legend-server-marketplace'; import { LEGEND_MARKETPLACE_APP_EVENT } from '../../__lib__/LegendMarketplaceAppEvent.js'; import { LegendMarketplaceTelemetryHelper } from '../../__lib__/LegendMarketplaceTelemetryHelper.js'; import { FieldSearchResultState } from './fieldSearch/FieldSearchResultState.js'; export class LegendMarketplaceFieldSearchResultsStore { marketplaceBaseStore; searchQuery = undefined; fieldSearchResultStates = []; responseMetadata = undefined; page = 1; itemsPerPage = 12; errorMessage = undefined; selectedProductTypes = new Set(); expandedRows = new Set(); loadState = ActionState.create(); _currentFetchToken = 0; _abortController = undefined; constructor(marketplaceBaseStore) { this.marketplaceBaseStore = marketplaceBaseStore; makeObservable(this, { searchQuery: observable, fieldSearchResultStates: observable, responseMetadata: observable, page: observable, itemsPerPage: observable, errorMessage: observable, selectedProductTypes: observable, expandedRows: observable, _currentFetchToken: false, _abortController: false, setSearchQuery: action, setFieldSearchResultStates: action, setResponseMetadata: action, setPage: action, setItemsPerPage: action, setErrorMessage: action, toggleProductType: action, clearAllFilters: action, toggleExpandRow: action, clearExpandedRows: action, fieldSearchRequest: computed, tableRows: computed, totalItems: computed, totalFieldMatches: computed, lakehouseCount: computed, legacyCount: computed, hasActiveFilters: computed, isLoading: computed, hasFailed: computed, executeSearch: flow, changePageAndFetch: flow, changeItemsPerPageAndFetch: flow, toggleProductTypeAndFetch: flow, clearAllFiltersAndFetch: flow, }); } setSearchQuery(query) { this.searchQuery = query; this.page = 1; } setFieldSearchResultStates(results) { this.fieldSearchResultStates = results; } setResponseMetadata(value) { this.responseMetadata = value; } setPage(value) { this.page = value; } setItemsPerPage(value) { this.itemsPerPage = value; this.page = 1; } setErrorMessage(value) { this.errorMessage = value; } toggleProductType(productType) { const wasSelected = this.selectedProductTypes.has(productType); if (wasSelected) { this.selectedProductTypes.delete(productType); } else { this.selectedProductTypes.add(productType); } this.page = 1; LegendMarketplaceTelemetryHelper.logEvent_ApplySearchFilter(this.marketplaceBaseStore.applicationStore.telemetryService, 'dataProductType', productType, wasSelected ? 'deselect' : 'select', this.searchQuery); } clearAllFilters() { this.selectedProductTypes.clear(); this.page = 1; LegendMarketplaceTelemetryHelper.logEvent_ClearSearchFilters(this.marketplaceBaseStore.applicationStore.telemetryService, this.searchQuery); } toggleExpandRow(rowId) { if (this.expandedRows.has(rowId)) { this.expandedRows.delete(rowId); } else { this.expandedRows.add(rowId); } } clearExpandedRows() { this.expandedRows.clear(); } isRowExpanded(rowId) { return this.expandedRows.has(rowId); } get fieldSearchRequest() { if (!isNonEmptyString(this.searchQuery)) { return undefined; } return { query: this.searchQuery, searchType: FieldSearchType.HYBRID, pageSize: this.itemsPerPage, pageNumber: this.page, ...(this.selectedProductTypes.size > 0 ? { dataProductTypes: Array.from(this.selectedProductTypes) } : {}), }; } get tableRows() { return this.fieldSearchResultStates; } get totalItems() { return this.responseMetadata?.total_count ?? 0; } get totalFieldMatches() { return this.responseMetadata?.total_field_matches ?? 0; } get lakehouseCount() { return this.responseMetadata?.lakehouse_count ?? 0; } get legacyCount() { return this.responseMetadata?.legacy_count ?? 0; } get hasActiveFilters() { return this.selectedProductTypes.size > 0; } get isLoading() { return (isNonEmptyString(this.searchQuery) && (this.loadState.isInInitialState || this.loadState.isInProgress)); } get hasFailed() { return this.loadState.hasFailed; } *executeSearch() { yield* this.fetchFieldSearchResultsInternal(); } *changePageAndFetch(page) { this.setPage(page); yield* this.fetchFieldSearchResultsInternal(); } *changeItemsPerPageAndFetch(itemsPerPage) { this.setItemsPerPage(itemsPerPage); yield* this.fetchFieldSearchResultsInternal(); } *toggleProductTypeAndFetch(productType) { this.toggleProductType(productType); yield* this.fetchFieldSearchResultsInternal(); } *clearAllFiltersAndFetch() { this.clearAllFilters(); yield* this.fetchFieldSearchResultsInternal(); } *fetchFieldSearchResultsInternal() { const request = this.fieldSearchRequest; const fetchToken = ++this._currentFetchToken; if (!request) { this.setFieldSearchResultStates([]); this.setResponseMetadata(undefined); this.setErrorMessage(undefined); this.clearExpandedRows(); this.loadState.complete(); return; } this._abortController?.abort(); this._abortController = new AbortController(); const { signal } = this._abortController; this.loadState.inProgress(); this.setErrorMessage(undefined); this.clearExpandedRows(); try { const rawResponse = (yield this.marketplaceBaseStore.marketplaceServerClient.fieldSearch(this.marketplaceBaseStore.envState.lakehouseEnvironment, request, signal)); if (fetchToken !== this._currentFetchToken) { return; } const response = GroupedFieldSearchResponse.serialization.fromJson(rawResponse); const results = response.results.map((entry) => new FieldSearchResultState(entry)); this.setFieldSearchResultStates(results); this.setResponseMetadata(response.metadata); this.loadState.complete(); } catch (error) { if (fetchToken !== this._currentFetchToken) { return; } assertErrorThrown(error); this.marketplaceBaseStore.applicationStore.logService.error(LogEvent.create(LEGEND_MARKETPLACE_APP_EVENT.FIELD_SEARCH_FAILURE), error); this.marketplaceBaseStore.applicationStore.notificationService.notifyError(`Error fetching field search results: ${error.message}`); this.setFieldSearchResultStates([]); this.setResponseMetadata(undefined); this.setErrorMessage(error.message); this.loadState.fail(); } } dispose() { this._abortController?.abort(); this._abortController = undefined; } } //# sourceMappingURL=LegendMarketplaceFieldSearchResultsStore.js.map