/** * Copyright (c) 2020-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 { observer } from 'mobx-react-lite'; import { cn, DataCubeIcon, DropdownMenu, DropdownMenuItem, useDropdownMenu, } from '@finos/legend-art'; import { debounce, formatDistanceToNow, quantifyList, } from '@finos/legend-shared'; import { useRef, useMemo, useEffect } from 'react'; import { FormButton, FormCheckbox, FormDropdownMenu, FormDropdownMenuItem, FormDropdownMenuTrigger, FormTextInput, } from '@finos/legend-data-cube'; import { useLegendDataCubeBuilderStore } from './LegendDataCubeBuilderStoreProvider.js'; import { DATA_CUBE_LOADER_TYPEAHEAD_SEARCH_LIMIT, DataCubeSortByType, } from '../../stores/builder/LegendDataCubeLoaderState.js'; import { useApplicationStore } from '@finos/legend-application'; const LegendDataCubeSearcher = observer(() => { const store = useLegendDataCubeBuilderStore(); const state = store.loader; const searchInputRef = useRef(null); const searchResults = state.searchResults; useEffect(() => { searchInputRef.current?.focus(); }, [state]); // search text const debouncedLoader = useMemo( () => debounce((input: string) => { state .searchDataCubes(input) .catch((error) => store.alertService.alertUnhandledError(error)); }, 500), [store, state], ); const onSearchTextChange: React.ChangeEventHandler = ( event, ) => { if (event.target.value !== state.searchText) { state.setSearchText(event.target.value); debouncedLoader.cancel(); debouncedLoader(event.target.value); } }; const clearSearches = () => { state.setSearchText(''); debouncedLoader.cancel(); debouncedLoader(''); }; // filter and sort const toggleShowCurrentUserResultsOnly = () => { state.setShowCurrentUserResultsOnly(!state.showCurrentUserResultsOnly); debouncedLoader.cancel(); debouncedLoader(state.searchText); }; const [ openSortDropdown, closeSortDropdown, sortDropdownProps, sortDropdownPropsOpen, ] = useDropdownMenu(); const applySort = (value: DataCubeSortByType) => { state.setSortBy(value); debouncedLoader.cancel(); debouncedLoader(state.searchText); }; useEffect(() => { state .searchDataCubes('') .catch((error) => store.alertService.alertUnhandledError(error)); }, [store, state]); return (
{Boolean(state.searchText) && ( <> )}
Filters:
Sort by: {state.sortBy} {Object.values(DataCubeSortByType).map((option) => ( { applySort(option); closeSortDropdown(); }} autoFocus={option === state.sortBy} > {option} ))}
{state.searchState.hasCompleted && ( <>
{state.showingDefaultResults ? ( `Refine your search to get better matches` ) : searchResults.length >= DATA_CUBE_LOADER_TYPEAHEAD_SEARCH_LIMIT ? ( <> {`Found ${DATA_CUBE_LOADER_TYPEAHEAD_SEARCH_LIMIT}+ matches`}{' '} ) : ( `Found ${quantifyList(searchResults, 'match', 'matches')}` )}
{searchResults .slice(0, DATA_CUBE_LOADER_TYPEAHEAD_SEARCH_LIMIT) .map((result, idx) => (
state.setSelectedResult(result)} >
{result.name}
{result.lastUpdatedAt ? formatDistanceToNow( new Date(result.lastUpdatedAt), { includeSeconds: true, addSuffix: true, }, ) : '(unknown)'}
{result.owner}
))} )} {!state.searchState.hasCompleted && (
Searching...
)}
); }); export const LegendDataCubeLoader = observer(() => { const store = useLegendDataCubeBuilderStore(); const application = useApplicationStore(); const state = store.loader; const selectedResult = state.selectedResult; const [openManageDropdown, closeManageDropdown, manageDropdownProps] = useDropdownMenu(); return ( <>
{!selectedResult ? ( ) : (
{selectedResult.name}
{selectedResult.lastUpdatedAt ? formatDistanceToNow( new Date(selectedResult.lastUpdatedAt), { includeSeconds: true, addSuffix: true, }, ) : '(unknown)'}
{selectedResult.owner}
state.setSelectedResult(undefined)} > Go Back {store.canCurrentUserManageDataCube(selectedResult) && ( <>
Manage DataCube
{ store.setDataCubeToDelete(selectedResult); store.deleteConfirmationDisplay.open(); closeManageDropdown(); }} > Delete... )}
)}
state.display.close()}>Cancel { state .finalize() .catch((error) => store.alertService.alertUnhandledError(error)); }} > OK
); });