[learn-example][m] - code section for the tutorial part 3
This commit is contained in:
40
examples/learn-example/components/Catalog.tsx
Normal file
40
examples/learn-example/components/Catalog.tsx
Normal 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>
|
||||
</>
|
||||
);
|
||||
}
|
||||
Reference in New Issue
Block a user