Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement basic text search #47

Merged
merged 1 commit into from
Nov 1, 2024
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
11 changes: 11 additions & 0 deletions frontend/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 3 additions & 1 deletion frontend/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -21,5 +21,7 @@
"vite": "^5.4.10",
"prettier": "^3.3.3"
},
"dependencies": {}
"dependencies": {
"fuse.js": "^7.0.0"
}
}
6 changes: 4 additions & 2 deletions frontend/src/App.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import type {FullCrate, Indexes} from "./crate-db";
import Filter from "./lib/Filter.svelte";
import ForkMe from "./lib/ForkMe.svelte";
import TextFilter from "./lib/TextFilter.svelte";

export let BUILD_DATE: string;

Expand All @@ -16,6 +17,7 @@
let selected_l: number[][] = [];
let selected_r: number[][] = [];
let selected_i: number[][] = [];
let selected_f: number[][] = [];

function combine_filters(crate_length: number, selected: any): number[] {
let selected_crates = Array.from({length: crate_length}, (_, i) => i + 1);
Expand All @@ -39,7 +41,7 @@
}
}

$: selected_crates = combine_filters(t_crates.length, [selected_d, selected_l, selected_r, selected_i]);
$: selected_crates = combine_filters(t_crates.length, [selected_d, selected_l, selected_r, selected_i, selected_f]);

</script>

Expand All @@ -49,8 +51,8 @@

<h1>{selected_crates.length} awesome drivers waiting for you!</h1>
<main>

<div class="filters">
<TextFilter crates={t_crates} bind:selected={selected_f}/>
<Filter name="Dependencies" values={t_indexes.dependencies} bind:selected={selected_d}/>
<Filter name="Interfaces" values={t_indexes.interfaces} bind:selected={selected_i}/>
<Filter name="👮 License" values={t_indexes.license} bind:selected={selected_l}/>
Expand Down
32 changes: 32 additions & 0 deletions frontend/src/lib/TextFilter.svelte
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
<script lang="ts">
import type {FullCrate} from "../crate-db";
import Fuse from "fuse.js";

export let crates: FullCrate[];
export let selected: number[][];

let search: string = "";

const options = {
keys: [
"description",
"manufacturer",
"name",
"names",
"part_numbers",
"resources.title",
],
includeMatches: true,
includeScore: true,
useExtendedSearch: true,
};
const index = Fuse.createIndex(options.keys, crates)
const fuse = new Fuse(crates, options, index)

$: results = fuse.search(search)
$: selected = search === "" ? [] : [results.map((r) => r.refIndex)]
</script>

<div>
<input type="text" bind:value={search}>
</div>