mirror of
https://github.com/hydralauncher/hydra.git
synced 2025-02-03 08:43:48 +03:00
feat: searching from api
This commit is contained in:
parent
262e95e4db
commit
2e82c29f4c
@ -3,7 +3,7 @@ import { shuffle } from "lodash-es";
|
|||||||
import { getSteam250List } from "@main/services";
|
import { getSteam250List } from "@main/services";
|
||||||
|
|
||||||
import { registerEvent } from "../register-event";
|
import { registerEvent } from "../register-event";
|
||||||
import { searchSteamGames } from "../helpers/search-games";
|
import { getSteamGameById } from "../helpers/search-games";
|
||||||
import type { Steam250Game } from "@types";
|
import type { Steam250Game } from "@types";
|
||||||
|
|
||||||
const state = { games: Array<Steam250Game>(), index: 0 };
|
const state = { games: Array<Steam250Game>(), index: 0 };
|
||||||
@ -12,11 +12,9 @@ const filterGames = async (games: Steam250Game[]) => {
|
|||||||
const results: Steam250Game[] = [];
|
const results: Steam250Game[] = [];
|
||||||
|
|
||||||
for (const game of games) {
|
for (const game of games) {
|
||||||
const catalogue = await searchSteamGames({ query: game.title });
|
const steamGame = await getSteamGameById(game.objectID);
|
||||||
|
|
||||||
if (catalogue.length) {
|
|
||||||
const [steamGame] = catalogue;
|
|
||||||
|
|
||||||
|
if (steamGame) {
|
||||||
if (steamGame.repacks.length) {
|
if (steamGame.repacks.length) {
|
||||||
results.push(game);
|
results.push(game);
|
||||||
}
|
}
|
||||||
|
@ -1,10 +1,24 @@
|
|||||||
import { registerEvent } from "../register-event";
|
import { registerEvent } from "../register-event";
|
||||||
import { searchSteamGames } from "../helpers/search-games";
|
import { getSteamGameById } from "../helpers/search-games";
|
||||||
import { CatalogueEntry } from "@types";
|
import { CatalogueEntry } from "@types";
|
||||||
|
import { HydraApi } from "@main/services";
|
||||||
|
|
||||||
const searchGamesEvent = async (
|
const searchGamesEvent = async (
|
||||||
_event: Electron.IpcMainInvokeEvent,
|
_event: Electron.IpcMainInvokeEvent,
|
||||||
query: string
|
query: string
|
||||||
): Promise<CatalogueEntry[]> => searchSteamGames({ query, limit: 12 });
|
): Promise<CatalogueEntry[]> => {
|
||||||
|
const games = await HydraApi.get<
|
||||||
|
{ objectId: string; title: string; shop: string }[]
|
||||||
|
>("/games/search", { title: query, take: 12, skip: 0 }, { needsAuth: false });
|
||||||
|
|
||||||
|
const steamGames = await Promise.all(
|
||||||
|
games.map((game) => getSteamGameById(game.objectId))
|
||||||
|
);
|
||||||
|
const filteredGames = steamGames.filter(
|
||||||
|
(game) => game !== null
|
||||||
|
) as CatalogueEntry[];
|
||||||
|
|
||||||
|
return filteredGames;
|
||||||
|
};
|
||||||
|
|
||||||
registerEvent("searchGames", searchGamesEvent);
|
registerEvent("searchGames", searchGamesEvent);
|
||||||
|
@ -1,6 +1,3 @@
|
|||||||
import { orderBy } from "lodash-es";
|
|
||||||
import flexSearch from "flexsearch";
|
|
||||||
|
|
||||||
import type { GameShop, CatalogueEntry, SteamGame } from "@types";
|
import type { GameShop, CatalogueEntry, SteamGame } from "@types";
|
||||||
|
|
||||||
import { getSteamAppAsset } from "@main/helpers";
|
import { getSteamAppAsset } from "@main/helpers";
|
||||||
@ -23,20 +20,18 @@ export const convertSteamGameToCatalogueEntry = (
|
|||||||
repacks: [],
|
repacks: [],
|
||||||
});
|
});
|
||||||
|
|
||||||
export const searchSteamGames = async (
|
export const getSteamGameById = async (
|
||||||
options: flexSearch.SearchOptions
|
objectId: string
|
||||||
): Promise<CatalogueEntry[]> => {
|
): Promise<CatalogueEntry | null> => {
|
||||||
const steamGames = (await steamGamesWorker.run(options, {
|
const steamGame = await steamGamesWorker.run(Number(objectId), {
|
||||||
name: "search",
|
name: "getById",
|
||||||
})) as SteamGame[];
|
});
|
||||||
|
|
||||||
const result = RepacksManager.findRepacksForCatalogueEntries(
|
if (!steamGame) return null;
|
||||||
steamGames.map((game) => convertSteamGameToCatalogueEntry(game))
|
|
||||||
);
|
|
||||||
|
|
||||||
return orderBy(
|
const catalogueEntry = convertSteamGameToCatalogueEntry(steamGame);
|
||||||
result,
|
|
||||||
[({ repacks }) => repacks.length, "repacks"],
|
const result = RepacksManager.findRepacksForCatalogueEntry(catalogueEntry);
|
||||||
["desc"]
|
|
||||||
);
|
return result;
|
||||||
};
|
};
|
||||||
|
@ -7,6 +7,10 @@ import { clearGamesRemoteIds } from "./library-sync/clear-games-remote-id";
|
|||||||
import { logger } from "./logger";
|
import { logger } from "./logger";
|
||||||
import { UserNotLoggedInError } from "@shared";
|
import { UserNotLoggedInError } from "@shared";
|
||||||
|
|
||||||
|
interface HydraApiOptions {
|
||||||
|
needsAuth: boolean;
|
||||||
|
}
|
||||||
|
|
||||||
export class HydraApi {
|
export class HydraApi {
|
||||||
private static instance: AxiosInstance;
|
private static instance: AxiosInstance;
|
||||||
|
|
||||||
@ -204,50 +208,76 @@ export class HydraApi {
|
|||||||
throw err;
|
throw err;
|
||||||
};
|
};
|
||||||
|
|
||||||
static async get<T = any>(url: string, params?: any) {
|
static async get<T = any>(
|
||||||
if (!this.isLoggedIn()) throw new UserNotLoggedInError();
|
url: string,
|
||||||
|
params?: any,
|
||||||
|
options?: HydraApiOptions
|
||||||
|
) {
|
||||||
|
if (!options || options.needsAuth) {
|
||||||
|
if (!this.isLoggedIn()) throw new UserNotLoggedInError();
|
||||||
|
await this.revalidateAccessTokenIfExpired();
|
||||||
|
}
|
||||||
|
|
||||||
await this.revalidateAccessTokenIfExpired();
|
|
||||||
return this.instance
|
return this.instance
|
||||||
.get<T>(url, { params, ...this.getAxiosConfig() })
|
.get<T>(url, { params, ...this.getAxiosConfig() })
|
||||||
.then((response) => response.data)
|
.then((response) => response.data)
|
||||||
.catch(this.handleUnauthorizedError);
|
.catch(this.handleUnauthorizedError);
|
||||||
}
|
}
|
||||||
|
|
||||||
static async post<T = any>(url: string, data?: any) {
|
static async post<T = any>(
|
||||||
if (!this.isLoggedIn()) throw new UserNotLoggedInError();
|
url: string,
|
||||||
|
data?: any,
|
||||||
|
options?: HydraApiOptions
|
||||||
|
) {
|
||||||
|
if (!options || options.needsAuth) {
|
||||||
|
if (!this.isLoggedIn()) throw new UserNotLoggedInError();
|
||||||
|
await this.revalidateAccessTokenIfExpired();
|
||||||
|
}
|
||||||
|
|
||||||
await this.revalidateAccessTokenIfExpired();
|
|
||||||
return this.instance
|
return this.instance
|
||||||
.post<T>(url, data, this.getAxiosConfig())
|
.post<T>(url, data, this.getAxiosConfig())
|
||||||
.then((response) => response.data)
|
.then((response) => response.data)
|
||||||
.catch(this.handleUnauthorizedError);
|
.catch(this.handleUnauthorizedError);
|
||||||
}
|
}
|
||||||
|
|
||||||
static async put<T = any>(url: string, data?: any) {
|
static async put<T = any>(
|
||||||
if (!this.isLoggedIn()) throw new UserNotLoggedInError();
|
url: string,
|
||||||
|
data?: any,
|
||||||
|
options?: HydraApiOptions
|
||||||
|
) {
|
||||||
|
if (!options || options.needsAuth) {
|
||||||
|
if (!this.isLoggedIn()) throw new UserNotLoggedInError();
|
||||||
|
await this.revalidateAccessTokenIfExpired();
|
||||||
|
}
|
||||||
|
|
||||||
await this.revalidateAccessTokenIfExpired();
|
|
||||||
return this.instance
|
return this.instance
|
||||||
.put<T>(url, data, this.getAxiosConfig())
|
.put<T>(url, data, this.getAxiosConfig())
|
||||||
.then((response) => response.data)
|
.then((response) => response.data)
|
||||||
.catch(this.handleUnauthorizedError);
|
.catch(this.handleUnauthorizedError);
|
||||||
}
|
}
|
||||||
|
|
||||||
static async patch<T = any>(url: string, data?: any) {
|
static async patch<T = any>(
|
||||||
if (!this.isLoggedIn()) throw new UserNotLoggedInError();
|
url: string,
|
||||||
|
data?: any,
|
||||||
|
options?: HydraApiOptions
|
||||||
|
) {
|
||||||
|
if (!options || options.needsAuth) {
|
||||||
|
if (!this.isLoggedIn()) throw new UserNotLoggedInError();
|
||||||
|
await this.revalidateAccessTokenIfExpired();
|
||||||
|
}
|
||||||
|
|
||||||
await this.revalidateAccessTokenIfExpired();
|
|
||||||
return this.instance
|
return this.instance
|
||||||
.patch<T>(url, data, this.getAxiosConfig())
|
.patch<T>(url, data, this.getAxiosConfig())
|
||||||
.then((response) => response.data)
|
.then((response) => response.data)
|
||||||
.catch(this.handleUnauthorizedError);
|
.catch(this.handleUnauthorizedError);
|
||||||
}
|
}
|
||||||
|
|
||||||
static async delete<T = any>(url: string) {
|
static async delete<T = any>(url: string, options?: HydraApiOptions) {
|
||||||
if (!this.isLoggedIn()) throw new UserNotLoggedInError();
|
if (!options || options.needsAuth) {
|
||||||
|
if (!this.isLoggedIn()) throw new UserNotLoggedInError();
|
||||||
|
await this.revalidateAccessTokenIfExpired();
|
||||||
|
}
|
||||||
|
|
||||||
await this.revalidateAccessTokenIfExpired();
|
|
||||||
return this.instance
|
return this.instance
|
||||||
.delete<T>(url, this.getAxiosConfig())
|
.delete<T>(url, this.getAxiosConfig())
|
||||||
.then((response) => response.data)
|
.then((response) => response.data)
|
||||||
|
@ -49,6 +49,11 @@ export class RepacksManager {
|
|||||||
.map((index) => this.repacks[index]);
|
.map((index) => this.repacks[index]);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public static findRepacksForCatalogueEntry(entry: CatalogueEntry) {
|
||||||
|
const repacks = this.search({ query: formatName(entry.title) });
|
||||||
|
return { ...entry, repacks };
|
||||||
|
}
|
||||||
|
|
||||||
public static findRepacksForCatalogueEntries(entries: CatalogueEntry[]) {
|
public static findRepacksForCatalogueEntries(entries: CatalogueEntry[]) {
|
||||||
return entries.map((entry) => {
|
return entries.map((entry) => {
|
||||||
const repacks = this.search({ query: formatName(entry.title) });
|
const repacks = this.search({ query: formatName(entry.title) });
|
||||||
|
@ -1,36 +1,15 @@
|
|||||||
import { SteamGame } from "@types";
|
import { SteamGame } from "@types";
|
||||||
import { orderBy, slice } from "lodash-es";
|
import { slice } from "lodash-es";
|
||||||
import flexSearch from "flexsearch";
|
|
||||||
import fs from "node:fs";
|
import fs from "node:fs";
|
||||||
|
|
||||||
import { formatName } from "@shared";
|
|
||||||
import { workerData } from "node:worker_threads";
|
import { workerData } from "node:worker_threads";
|
||||||
|
|
||||||
const steamGamesIndex = new flexSearch.Index({
|
|
||||||
tokenize: "reverse",
|
|
||||||
});
|
|
||||||
|
|
||||||
const { steamGamesPath } = workerData;
|
const { steamGamesPath } = workerData;
|
||||||
|
|
||||||
const data = fs.readFileSync(steamGamesPath, "utf-8");
|
const data = fs.readFileSync(steamGamesPath, "utf-8");
|
||||||
|
|
||||||
const steamGames = JSON.parse(data) as SteamGame[];
|
const steamGames = JSON.parse(data) as SteamGame[];
|
||||||
|
|
||||||
for (let i = 0; i < steamGames.length; i++) {
|
|
||||||
const steamGame = steamGames[i];
|
|
||||||
|
|
||||||
const formattedName = formatName(steamGame.name);
|
|
||||||
|
|
||||||
steamGamesIndex.add(i, formattedName);
|
|
||||||
}
|
|
||||||
|
|
||||||
export const search = (options: flexSearch.SearchOptions) => {
|
|
||||||
const results = steamGamesIndex.search(options);
|
|
||||||
const games = results.map((index) => steamGames[index]);
|
|
||||||
|
|
||||||
return orderBy(games, ["name"], ["asc"]);
|
|
||||||
};
|
|
||||||
|
|
||||||
export const getById = (id: number) =>
|
export const getById = (id: number) =>
|
||||||
steamGames.find((game) => game.id === id);
|
steamGames.find((game) => game.id === id);
|
||||||
|
|
||||||
|
@ -48,7 +48,7 @@ export function SearchResults() {
|
|||||||
.finally(() => {
|
.finally(() => {
|
||||||
setIsLoading(false);
|
setIsLoading(false);
|
||||||
});
|
});
|
||||||
}, 300);
|
}, 500);
|
||||||
|
|
||||||
debouncedFunc.current();
|
debouncedFunc.current();
|
||||||
}, [searchParams, dispatch]);
|
}, [searchParams, dispatch]);
|
||||||
|
Loading…
Reference in New Issue
Block a user