Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -21,13 +21,19 @@ import { debounce } from 'lodash';
import { useCallback, useEffect, useMemo, useRef, useState } from 'react';
import { useTranslation } from 'react-i18next';
import { useNavigate } from 'react-router-dom';
import { ReactComponent as IconSuggestionsActive } from '../../../assets/svg/ic-suggestions-active.svg';
import { ReactComponent as IconSuggestionsBlue } from '../../../assets/svg/ic-suggestions-blue.svg';
import { INITIAL_PAGING_VALUE } from '../../../constants/constants';
import { SearchIndex } from '../../../enums/search.enum';
import { DataProduct } from '../../../generated/entity/domains/dataProduct';
import { Domain } from '../../../generated/entity/domains/domain';
import { useMarketplaceRecentSearches } from '../../../hooks/useMarketplaceRecentSearches';
import { useMarketplaceStore } from '../../../hooks/useMarketplaceStore';
import { searchQuery } from '../../../rest/searchAPI';
import {
getNLPEnabledStatus,
nlqSearch,
searchQuery,
} from '../../../rest/searchAPI';
import { getDataProductIconByUrl } from '../../../utils/DataProductUtils';
import { getDomainIcon } from '../../../utils/DomainUtils';
import { getDomainDetailsPath } from '../../../utils/RouterUtils';
Expand All @@ -40,6 +46,8 @@ const MarketplaceSearchBar = ({ isEditView }: { isEditView?: boolean }) => {
const { t } = useTranslation();
const navigate = useNavigate();
const { dataProductBasePath } = useMarketplaceStore();
const [isNLPEnabled, setIsNLPEnabled] = useState(false);
const [isNLQActive, setIsNLQActive] = useState(false);
const [searchValue, setSearchValue] = useState('');
const [isOpen, setIsOpen] = useState(false);
const [dataProducts, setDataProducts] = useState<DataProduct[]>([]);
Expand All @@ -48,41 +56,73 @@ const MarketplaceSearchBar = ({ isEditView }: { isEditView?: boolean }) => {
const containerRef = useRef<HTMLDivElement>(null);
const { addSearch } = useMarketplaceRecentSearches();

const fetchResults = useCallback(async (query: string) => {
if (!query.trim()) {
setDataProducts([]);
setDomains([]);
useEffect(() => {
getNLPEnabledStatus()
.then(setIsNLPEnabled)
.catch(() => setIsNLPEnabled(false));
}, []);

return;
}
setIsSearching(true);
try {
const [dpRes, domainRes] = await Promise.all([
searchQuery({
query,
pageNumber: INITIAL_PAGING_VALUE,
pageSize: PAGE_SIZE,
searchIndex: SearchIndex.DATA_PRODUCT,
}),
searchQuery({
query,
pageNumber: INITIAL_PAGING_VALUE,
pageSize: PAGE_SIZE,
searchIndex: SearchIndex.DOMAIN,
}),
]);
const fetchResults = useCallback(
async (query: string) => {
if (!query.trim()) {
setDataProducts([]);
setDomains([]);

setDataProducts(
dpRes.hits.hits.map((hit) => hit._source) as DataProduct[]
);
setDomains(domainRes.hits.hits.map((hit) => hit._source) as Domain[]);
} catch {
setDataProducts([]);
setDomains([]);
} finally {
setIsSearching(false);
}
}, []);
return;
}
setIsSearching(true);
try {
if (isNLPEnabled && isNLQActive) {
const res = await nlqSearch({
query,
pageNumber: INITIAL_PAGING_VALUE,
pageSize: PAGE_SIZE * 2,
searchIndex: SearchIndex.MARKETPLACE,
Comment on lines +78 to +80
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Are we not using pagination on this endpoint yet?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Since this is a suggestions dropdown, not a paginated list page, we just want just the top results.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We always fetch the first page to get enough items to split between data products and domains.
Image

});

const hits = res.hits.hits;
setDataProducts(
hits
.filter((h) => h._source.entityType === SearchIndex.DATA_PRODUCT)
.slice(0, PAGE_SIZE)
.map((h) => h._source as unknown as DataProduct)
);
setDomains(
hits
.filter((h) => h._source.entityType === SearchIndex.DOMAIN)
.slice(0, PAGE_SIZE)
.map((h) => h._source as unknown as Domain)
);
} else {
const [dpRes, domainRes] = await Promise.all([
searchQuery({
query,
pageNumber: INITIAL_PAGING_VALUE,
pageSize: PAGE_SIZE,
searchIndex: SearchIndex.DATA_PRODUCT,
}),
searchQuery({
query,
pageNumber: INITIAL_PAGING_VALUE,
pageSize: PAGE_SIZE,
searchIndex: SearchIndex.DOMAIN,
}),
]);

setDataProducts(
dpRes.hits.hits.map((hit) => hit._source) as DataProduct[]
);
setDomains(domainRes.hits.hits.map((hit) => hit._source) as Domain[]);
}
} catch {
setDataProducts([]);
setDomains([]);
} finally {
setIsSearching(false);
}
},
[isNLPEnabled, isNLQActive]
);

const debouncedFetch = useMemo(
() => debounce(fetchResults, 400),
Expand Down Expand Up @@ -248,27 +288,50 @@ const MarketplaceSearchBar = ({ isEditView }: { isEditView?: boolean }) => {
className="marketplace-search-bar"
data-testid="marketplace-search-bar"
ref={containerRef}>
<Input
autoComplete="off"
data-testid="marketplace-search-input"
fontSize="sm"
icon={SearchLg}
iconClassName="tw:!size-4"
inputClassName="tw:!pl-9"
isDisabled={isEditView}
placeholder={t('label.search-for-type', {
type:
t('label.data-product-plural') + ', ' + t('label.domain-plural'),
})}
value={searchValue}
wrapperClassName="marketplace-search-input tw:!rounded-xl tw:!items-center tw:!py-1"
onChange={(value) => handleChange(value)}
onKeyDown={(e) => {
if (e.key === 'Enter') {
handleSearch(searchValue);
}
}}
/>
<div className="tw:relative">
<div className="tw:absolute tw:left-3 tw:top-1/2 tw:-translate-y-1/2 tw:z-10 tw:flex tw:items-center">
{isNLPEnabled ? (
<button
className={`marketplace-nlq-button${
isNLQActive ? ' active' : ''
}`}
data-testid="marketplace-nlq-toggle"
title={
isNLQActive
? t('message.natural-language-search-active')
: t('label.use-natural-language-search')
}
onClick={() => setIsNLQActive((prev) => !prev)}>
{isNLQActive ? (
<IconSuggestionsActive />
) : (
<IconSuggestionsBlue />
)}
</button>
) : (
<SearchLg className="tw:size-4 tw:text-text-tertiary" />
)}
</div>
<Input
autoComplete="off"
data-testid="marketplace-search-input"
fontSize="sm"
inputClassName="tw:!pl-11"
isDisabled={isEditView}
placeholder={t('label.search-for-type', {
type:
t('label.data-product-plural') + ', ' + t('label.domain-plural'),
})}
value={searchValue}
wrapperClassName="marketplace-search-input tw:!rounded-xl tw:!items-center tw:!py-1"
onChange={(value) => handleChange(value)}
onKeyDown={(e) => {
if (e.key === 'Enter') {
handleSearch(searchValue);
}
}}
/>
</div>
<SelectPopover
isNonModal
className="!tw:max-h-[400px]"
Expand Down
Loading
Loading