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

Text search sorting #81

Merged
merged 4 commits 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
2 changes: 1 addition & 1 deletion frontend/src/App.svelte
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<script lang="ts">

import {crates, indexes} from './full-crate-db.json'
import {open_filter} from './store/FilterStore.svelte';
import {open_filter} from './store/FilterStore.ts';
import CrateList from "./lib/CrateList.svelte";
import type {FullCrate, Indexes} from "./crate-db";
import Filter from "./lib/Filter.svelte";
Expand Down
4 changes: 4 additions & 0 deletions frontend/src/app.css
Original file line number Diff line number Diff line change
Expand Up @@ -153,6 +153,10 @@ label {
display: flex;
gap: 1rem;
margin-bottom: 1rem;

input[type="text"] {
min-height: 54px;
}
}

.filter {
Expand Down
24 changes: 23 additions & 1 deletion frontend/src/lib/CrateList.svelte
Original file line number Diff line number Diff line change
@@ -1,7 +1,12 @@
<script lang="ts">
import type {FullCrate} from "../crate-db";
import Crate from "./Crate.svelte";
import {open_filter, sort_by} from '../store/FilterStore.svelte';
import {open_filter, sort_by, scores} from '../store/FilterStore.ts';

interface Score {
name: String;
score: number;
}

export let crates: FullCrate[];

Expand All @@ -10,6 +15,7 @@
let filtered_crates: FullCrate[];

$ : filtered_crates = crates.filter((_, i) => filter.includes(i)).sort((a, b) => {

if ($sort_by == 'all_downloads') {
return b.downloads - a.downloads;
}
Expand All @@ -23,8 +29,24 @@
return b.updated_at > a.updated_at ? 1 : 0;
}

// Sort by text search score
else if ($sort_by == 'score') {
const score_a = $scores.find((s : Score) => s.name == a.name);
const score_b = $scores.find((s : Score) => s.name == b.name);
if (score_a && score_b) {
return score_a.score - score_b.score;
} else if (score_a) {
return 0 - score_a.score;
} else if (score_b) {
return score_b.score;
} else {
return 0;
}
}

return b.name < a.name ? 1 : 0;
});

</script>

<ol>
Expand Down
2 changes: 1 addition & 1 deletion frontend/src/lib/Filter.svelte
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<script lang="ts">

import {open_filter} from '../store/FilterStore.svelte';
import {open_filter} from '../store/FilterStore.ts';

export let name: string;
export let values: { [key: string]: number[] };
Expand Down
6 changes: 5 additions & 1 deletion frontend/src/lib/SortFilter.svelte
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<script lang="ts">

import {open_filter, sort_by} from '../store/FilterStore.svelte';
import {open_filter, sort_by} from '../store/FilterStore.ts';

export let name: string;

Expand Down Expand Up @@ -41,6 +41,7 @@
$: recent_downloads_open = $sort_by == 'recent_downloads';
$: newly_added_open = $sort_by == 'newly_added';
$: recently_updated_open = $sort_by == 'recently_updated';
$: text_score = $sort_by == 'score';

</script>

Expand Down Expand Up @@ -68,6 +69,9 @@
<label>
<input type="checkbox" on:click={() => update_sort('recently_updated')} bind:checked={recently_updated_open}> Recently updated
</label>
<label>
<input type="checkbox" on:click={() => update_sort('score')} bind:checked={text_score}> Text search
</label>
</div>
</div>
</div>
36 changes: 28 additions & 8 deletions frontend/src/lib/TextFilter.svelte
Original file line number Diff line number Diff line change
@@ -1,32 +1,52 @@
<script lang="ts">
import type {FullCrate} from "../crate-db";
import {sort_by, scores} from '../store/FilterStore.ts';
import Fuse from "fuse.js";

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

interface Score {
name: String;
score: number;
}

let search: string = "";

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

// Store the scores in the FilterStore on changes
// and force sorting by score
$ : {
if (search.length > 0) {
$sort_by = 'score';
$scores = (fuse.search(search).map((r) => ({ score: r.score, name: r.item.name } as Score))) as Score[];
} else {
$sort_by = 'alphanumeric';
}
}

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

<div>
<input type="text" bind:value={search}>
</div>
<input type="text" placeholder="Search" bind:value={search}>
</div>
11 changes: 0 additions & 11 deletions frontend/src/store/FilterStore.svelte

This file was deleted.

15 changes: 15 additions & 0 deletions frontend/src/store/FilterStore.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import { writable } from "svelte/store";

export interface Score {
name: String;
score: number;
}

// Which filter is currenly opened
export let open_filter = writable("");

// The results are sorted by
export let sort_by = writable("alphanumeric");

// Results of text search
export let scores = writable<Score[]>([]);
3 changes: 2 additions & 1 deletion frontend/tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,8 @@
*/
"allowJs": true,
"checkJs": true,
"isolatedModules": true
"isolatedModules": true,
"allowImportingTsExtensions": true
},
"include": ["src/**/*.ts", "src/**/*.js", "src/**/*.svelte"],
"references": [
Expand Down