Skip to content

Commit 9967d51

Browse files
committed
feat: show database card when a database is selected; closes #81
1 parent 10abb36 commit 9967d51

File tree

2 files changed

+34
-12
lines changed

2 files changed

+34
-12
lines changed

src/components/SearchPage/DatabaseCard.tsx

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ import RoutesEnum from "types/routes.enum";
1414
import { modalityValueToEnumLabel } from "utils/SearchPageFunctions/modalityLabels";
1515

1616
type Props = {
17-
dbName?: string;
17+
dbId?: string;
1818
fullName?: string;
1919
datasets?: number;
2020
modalities?: string[];
@@ -24,15 +24,15 @@ type Props = {
2424
};
2525

2626
const DatabaseCard: React.FC<Props> = ({
27-
dbName,
27+
dbId,
2828
fullName,
2929
datasets,
3030
modalities,
3131
logo,
3232
keyword,
3333
onChipClick,
3434
}) => {
35-
const databaseLink = `${RoutesEnum.DATABASES}/${dbName}`;
35+
const databaseLink = `${RoutesEnum.DATABASES}/${dbId}`;
3636
// keyword hightlight functional component
3737
const highlightKeyword = (text: string, keyword?: string) => {
3838
if (!keyword || !text?.toLowerCase().includes(keyword.toLowerCase())) {

src/pages/SearchPage.tsx

Lines changed: 31 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -76,14 +76,34 @@ const SearchPage: React.FC = () => {
7676

7777
// for database card
7878
const keywordInput = String(formData?.keyword ?? "").trim();
79+
const selectedDbId = String(formData?.database ?? "").trim();
7980
console.log("keyword", keywordInput);
8081

82+
// const registryMatches: RegistryItem[] = React.useMemo(() => {
83+
// if (!Array.isArray(registry) || !keywordInput) return [];
84+
// return (registry as RegistryItem[]).filter((r) =>
85+
// matchesKeyword(r, keywordInput)
86+
// );
87+
// }, [registry, keywordInput]);
88+
8189
const registryMatches: RegistryItem[] = React.useMemo(() => {
82-
if (!Array.isArray(registry) || !keywordInput) return [];
83-
return (registry as RegistryItem[]).filter((r) =>
84-
matchesKeyword(r, keywordInput)
85-
);
86-
}, [registry, keywordInput]);
90+
if (!Array.isArray(registry)) return [];
91+
const list = registry as RegistryItem[];
92+
93+
const fromId =
94+
selectedDbId && selectedDbId !== "any"
95+
? list.filter((r) => r.id === selectedDbId)
96+
: [];
97+
98+
const fromKeyword = keywordInput
99+
? list.filter((r) => matchesKeyword(r, keywordInput))
100+
: [];
101+
102+
// merge the db results of selectedDB and keywordInput --> de duplicates
103+
const map = new Map<string, RegistryItem>();
104+
[...fromId, ...fromKeyword].forEach((r) => map.set(r.id, r));
105+
return Array.from(map.values()); // return matched registry
106+
}, [registry, selectedDbId, keywordInput]);
87107

88108
// to show the applied chips on the top of results
89109
const activeFilters = Object.entries(appliedFilters).filter(
@@ -328,7 +348,8 @@ const SearchPage: React.FC = () => {
328348
);
329349

330350
// check if has database/dataset matches
331-
const hasDbMatches = !!keywordInput && registryMatches.length > 0;
351+
// const hasDbMatches = !!keywordInput && registryMatches.length > 0;
352+
const hasDbMatches = registryMatches.length > 0;
332353
const hasDatasetMatches = Array.isArray(results) && results.length > 0;
333354
// when backend find nothing
334355
const backendEmpty =
@@ -522,7 +543,8 @@ const SearchPage: React.FC = () => {
522543
}}
523544
>
524545
{/* matching databases */}
525-
{keywordInput && registryMatches.length > 0 && (
546+
{/* {keywordInput && registryMatches.length > 0 && ( */}
547+
{registryMatches.length > 0 && (
526548
<Box
527549
sx={{
528550
mb: 3,
@@ -543,8 +565,8 @@ const SearchPage: React.FC = () => {
543565
{registryMatches.map((db) => (
544566
<DatabaseCard
545567
key={db.id}
546-
dbName={db.name}
547-
fullName={db.fullname}
568+
dbId={db.id}
569+
fullName={db.fullname ?? db.name}
548570
datasets={db.datasets}
549571
modalities={db.datatype}
550572
logo={db.logo}

0 commit comments

Comments
 (0)