[learn-example][m] - code section for the tutorial part 3

This commit is contained in:
Luccas Mateus de Medeiros Gomes
2023-05-03 09:49:50 -03:00
parent 72405162a1
commit b52aa38249
7 changed files with 1050 additions and 16 deletions

View File

@@ -0,0 +1,40 @@
import { Index } from 'flexsearch';
import { useState } from 'react';
import DebouncedInput from './DebouncedInput';
export default function Catalog({ datasets }: { datasets: any[] }) {
const [indexFilter, setIndexFilter] = useState('');
const index = new Index({ tokenize: "full"});
datasets.forEach((dataset) =>
index.add(
dataset._id,
Object.entries(dataset.metadata).reduce(
(acc, curr) => acc + ' ' + curr.toString(),
''
) + ' ' + dataset.url_path
)
);
return (
<>
<DebouncedInput
value={indexFilter ?? ''}
onChange={(value) => setIndexFilter(String(value))}
className="p-2 text-sm shadow border border-block"
placeholder="Search all datasets..."
/>
<ul>
{datasets
.filter((dataset) =>
indexFilter !== ''
? index.search(indexFilter).includes(dataset._id)
: true
)
.map((dataset) => (
<li key={dataset.id}>
<a href={dataset.url_path}>{dataset.metadata.title ? dataset.metadata.title : dataset.url_path}</a>
</li>
))}
</ul>
</>
);
}