Skip to content

Commit

Permalink
(test) use fuzzysort
Browse files Browse the repository at this point in the history
  • Loading branch information
pylixonly committed Feb 28, 2024
1 parent a37847f commit 314fa28
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 1 deletion.
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
},
"dependencies": {
"@swc/helpers": "0.5.0",
"fuzzysort": "^2.0.4",
"spitroast": "^1.4.3"
},
"pnpm": {
Expand Down
7 changes: 7 additions & 0 deletions pnpm-lock.yaml

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

15 changes: 14 additions & 1 deletion src/core/ui/components/AddonPage.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import { HTTP_REGEX_MULTI } from "@/lib/utils/constants";
import { showInputAlert } from "@ui/alerts";
import { getAssetIDByName } from "@/lib/api/assets";
import { Strings, formatString } from "@/core/i18n";
import fuzzysort from "fuzzysort";

interface AddonPageProps<T> {
title: string;
Expand All @@ -18,6 +19,18 @@ interface AddonPageProps<T> {
card: React.ComponentType<CardWrapper<T>>;
}

function getItemsByQuery<T extends { id?: string }>(items: T[], query: string): T[] {
if (!query) return items;

return fuzzysort.go(query, items, { keys: [
"id",
"manifest.name",
"manifest.description",
"manifest.authors.0.name",
"manifest.authors.1.name"
]}).map(r => r.obj);
}

const reanimated = findByProps("useSharedValue");
const { FloatingActionButton } = findByProps("FloatingActionButton");

Expand Down Expand Up @@ -52,7 +65,7 @@ export default function AddonPage<T>({ title, fetchFunction, items, safeModeMess
onMomentumScrollEnd={() => collapseText.value = 0}
style={{ paddingHorizontal: 10, paddingTop: 10 }}
contentContainerStyle={{ paddingBottom: 70 }}
data={Object.values(items).filter(i => i.id?.toLowerCase().includes(search))}
data={getItemsByQuery(Object.values(items), search)}
renderItem={({ item, index }) => <CardComponent item={item} index={index} />}
/>
<FloatingActionButton
Expand Down

0 comments on commit 314fa28

Please sign in to comment.