From b46a1d1fcfef87f97f2cbd96a2ed93c916f1b9e6 Mon Sep 17 00:00:00 2001 From: Thoronium <107363768+NotThorny@users.noreply.github.com> Date: Wed, 26 Apr 2023 19:20:49 -0600 Subject: [PATCH] Better mod searching --- src-tauri/src/gamebanana.rs | 27 ++++++++++----- src/ui/components/mods/ModList.tsx | 45 ++++++++++--------------- src/utils/gamebanana.ts | 53 ++++++++++++++++-------------- 3 files changed, 65 insertions(+), 60 deletions(-) diff --git a/src-tauri/src/gamebanana.rs b/src-tauri/src/gamebanana.rs index f0aeef9..e9764ff 100644 --- a/src-tauri/src/gamebanana.rs +++ b/src-tauri/src/gamebanana.rs @@ -13,15 +13,26 @@ pub async fn get_download_links(mod_id: String) -> String { } #[tauri::command] -pub async fn list_submissions(mode: String, page: String) -> String { - web::query( - format!( - "{}/apiv9/Util/Game/Submissions?_idGameRow=8552&_nPage={}&_nPerpage=50&_sMode={}", - SITE_URL, page, mode +pub async fn list_submissions(mode: String, page: String, search: String) -> String { + if search.is_empty() { + web::query( + format!( + "{}/apiv9/Util/Game/Submissions?_idGameRow=8552&_nPage={}&_nPerpage=50&_sMode={}", + SITE_URL, page, mode + ) + .as_str(), ) - .as_str(), - ) - .await + .await + } else { + web::query( + format!( + "{}/apiv11/Util/Search/Results?_nPage={}&_sOrder=best_match&_idGameRow=8552&_sSearchString={}&_csvFields=name,description,article,attribs,studio,owner,credits", + SITE_URL, page, search + ) + .as_str() + ) + .await + } } #[tauri::command] diff --git a/src/ui/components/mods/ModList.tsx b/src/ui/components/mods/ModList.tsx index 91552f7..3095970 100644 --- a/src/ui/components/mods/ModList.tsx +++ b/src/ui/components/mods/ModList.tsx @@ -1,6 +1,6 @@ import React from 'react' import { getConfigOption } from '../../../utils/configuration' -import { getAllMods, getInstalledMods, getMods, ModData, PartialModData } from '../../../utils/gamebanana' +import { getInstalledMods, getMods, ModData, PartialModData } from '../../../utils/gamebanana' import { LoadingCircle } from './LoadingCircle' import './ModList.css' @@ -17,11 +17,11 @@ interface IState { horny: boolean modList: ModData[] | null installedList: - | { - path: string - info: ModData | PartialModData - }[] - | null + | { + path: string + info: ModData | PartialModData + }[] + | null } export class ModList extends React.Component { @@ -64,16 +64,7 @@ export class ModList extends React.Component { return } - let mods: ModData[] - - if (!(this.props.search == '')) { - // idk the api so just filter all mods to search - mods = (await getAllMods(this.props.mode)).filter((mod) => - mod.name.toLowerCase().includes(this.props.search.toLowerCase()) - ) - } else { - mods = await getMods(this.props.mode, this.props.page) - } + const mods = await getMods(this.props.mode, this.props.page, this.props.search) const horny = await getConfigOption('horny_mode') @@ -91,21 +82,21 @@ export class ModList extends React.Component { return (
{(this.state.modList && this.props.mode !== 'installed') || - (this.state.installedList && this.props.mode === 'installed') ? ( + (this.state.installedList && this.props.mode === 'installed') ? (
{this.props.mode === 'installed' ? this.state.installedList?.map((mod) => ( - - )) + + )) : this.state.modList?.map((mod: ModData) => ( - - ))} + + ))}
) : ( diff --git a/src/utils/gamebanana.ts b/src/utils/gamebanana.ts index 642dc07..c9e2892 100644 --- a/src/utils/gamebanana.ts +++ b/src/utils/gamebanana.ts @@ -117,13 +117,38 @@ interface ModDownload { containsExe: boolean } -export async function getMods(mode: string, page: number) { +export async function getMods(mode: string, page: number, search: string) { let modList: GamebananaResponse[] = [] + if (search.length > 0) { + let hadMods = true + let page = 1 + + while (hadMods) { + const resp = JSON.parse( + await invoke('list_submissions', { + mode, + page: '' + page, + search: search + }) + ) + + const total = resp._aMetadata._nRecordCount + + if (page > (total / 15)) hadMods = false + + modList = [...modList, ...resp._aRecords] + page++ + } + + return formatGamebananaData(modList) + } + const resp = JSON.parse( await invoke('list_submissions', { mode, page: '' + page, + search: '', }) ) @@ -132,28 +157,6 @@ export async function getMods(mode: string, page: number) { return formatGamebananaData(modList) } -export async function getAllMods(mode: string) { - let modList: GamebananaResponse[] = [] - let hadMods = true - let page = 1 - - while (hadMods) { - const resp = JSON.parse( - await invoke('list_submissions', { - mode, - page: '' + page, - }) - ) - - if (resp.length === 0) hadMods = false - - modList = [...modList, ...resp] - page++ - } - - return formatGamebananaData(modList) -} - export async function formatGamebananaData(obj: GamebananaResponse[]) { if (!obj) return [] @@ -166,8 +169,8 @@ export async function formatGamebananaData(obj: GamebananaResponse[]) { name: itm._sName, images: img ? img.map((i) => { - return i._sBaseUrl + '/' + i._sFile220 - }) + return i._sBaseUrl + '/' + i._sFile220 + }) : [], dateadded: itm._tsDateAdded, submitter: {