From 78cea2ce5b43f9af95aca688aa0f954ac28e594c Mon Sep 17 00:00:00 2001 From: LynithDev <61880709+LynithDev@users.noreply.github.com> Date: Fri, 24 May 2024 21:22:25 +0200 Subject: [PATCH] refactor(mods): Search + fixed a bug when trying to resort --- .../ui/pages/cluster/cluster/ClusterMods.tsx | 97 +++++++++++-------- 1 file changed, 56 insertions(+), 41 deletions(-) diff --git a/desktop/src/ui/pages/cluster/cluster/ClusterMods.tsx b/desktop/src/ui/pages/cluster/cluster/ClusterMods.tsx index 504e6b94..1f2ae3f3 100644 --- a/desktop/src/ui/pages/cluster/cluster/ClusterMods.tsx +++ b/desktop/src/ui/pages/cluster/cluster/ClusterMods.tsx @@ -1,5 +1,5 @@ import { ArrowRightIcon, ChevronDownIcon, ChevronUpIcon, Edit02Icon, SearchMdIcon, Trash03Icon } from '@untitled-theme/icons-solid'; -import { For, Index, Match, Switch, createEffect, createSignal } from 'solid-js'; +import { For, Match, Switch, createSignal, onMount } from 'solid-js'; import * as uuid from 'uuid'; import uFuzzy from '@leeoniya/ufuzzy'; import Button from '~ui/components/base/Button'; @@ -19,46 +19,68 @@ const randomModNames = [ 'PolyEffects', ]; -const mods: ModEntryProps[] = []; - -for (let i = 0; i < randomModNames.length; i++) { - mods.push({ - id: uuid.v4(), - name: randomModNames[i] || 'Unknown', - author: 'Author Name', - version: '1.0.0', - description: 'This is a mod description', - provider: 'curseforge', - thumbnail: 'https://cdn.modrinth.com/data/AANobbMI/icon.png', - }); -} +const _mods: readonly ModEntryProps[] = Array(randomModNames.length).fill(undefined).map((_, index) => ({ + id: uuid.v4(), + name: randomModNames[index] || 'Unknown', + author: 'Author Name', + version: '1.0.0', + description: 'This is a mod description', + provider: 'curseforge', + thumbnail: 'https://cdn.modrinth.com/data/AANobbMI/icon.png', +})); +// TODO: Possibly optimise this as it has 2 cloned lists, and another containing only the names function ClusterMods() { - // Search state: - // `null` - No search query, show all mods - // `[]` - Search query, but no results, show "no results" message - // `[...]` - Search query, with results, show results - const [indexes, setIndexes] = createSignal(null); + // Initial mods for this cluster + const [mods] = createSignal([..._mods]); // TODO: Replace with hook - // State: - // true - A to Z - // false - Z to A - const [sortingName, setSortingName] = createSignal(true); + // Mods to display, should be a clone of the mods array but sorted + const [displayedMods, setDisplayedMods] = createSignal([]); + + // true - `A to Z` & false - `Z to A` + const [sortingAtoZ, setSortingAtoZ] = createSignal(true); const uf = new uFuzzy(); - const modsSearchable = mods - .map(mod => mod.name); + const [modsSearchable] = createSignal(() => mods().map(mod => mod.name)); function search(value: string) { if (value === '' || value === undefined) { - setIndexes(null); + resetMods(); return; } - const result = uf.search(modsSearchable, value); - setIndexes(result[0] ?? []); + const result = uf.search(modsSearchable()(), value); + + const filtered: ModEntryProps[] = []; + result[0]?.forEach((index) => { + const mod = mods()[index]; + if (mod) + filtered.push(mod); + }); + + setDisplayedMods(sortListByName(filtered)); } + function sortListByName(list: ModEntryProps[]): ModEntryProps[] { + if (sortingAtoZ()) + return list.sort((a, b) => a.name.localeCompare(b.name)); + else + return list.sort((a, b) => b.name.localeCompare(a.name)); + } + + function toggleNameSort() { + setSortingAtoZ(!sortingAtoZ()); + setDisplayedMods(sortListByName([...displayedMods()])); + } + + function resetMods() { + setDisplayedMods(sortListByName([...mods()])); + } + + onMount(() => { + resetMods(); + }); + return (
@@ -66,8 +88,8 @@ function ClusterMods() {