diff --git a/src/backend/stores/mihoyo.ts b/src/backend/stores/mihoyo.ts index 8c9b1ca..27fbd32 100644 --- a/src/backend/stores/mihoyo.ts +++ b/src/backend/stores/mihoyo.ts @@ -1,13 +1,16 @@ import { useEffect, useState } from "preact/hooks"; +import useSettings from "@backend/stores/settings.ts"; + import { create } from "zustand"; import { persist, createJSONStorage } from "zustand/middleware"; import { invoke } from "@tauri-apps/api"; +import { fetch } from "@tauri-apps/api/http"; import { exists } from "@tauri-apps/api/fs"; import { convertFileSrc } from "@tauri-apps/api/tauri"; -import { LauncherResponse, StoreWrite } from "@backend/types.ts"; +import { LauncherResponse, StoreWrite, SupportedGames } from "@backend/types.ts"; import { AppDataPath, LauncherUrls } from "@app/constants.ts"; export type GameDataStore = { @@ -52,8 +55,8 @@ export const useStarRailStore = create()( */ export async function fetchLatestBackground(set: StoreWrite, serviceUrl: string): Promise { // Fetch the launcher data. - const launcherData = await fetch(serviceUrl, { cache: "force-cache" }); - const responseData = await launcherData.json() as LauncherResponse; + const launcherData = await fetch(serviceUrl); + const responseData = launcherData.data; // Check if the background exists on the system. const backgroundUrl = responseData.data.adv.background; @@ -86,7 +89,9 @@ export async function getBackgroundFile(hash: string): Promise { * React hook which returns the URL of the locally cached background image. */ export function useBackground() { - const { backgroundHash, fetchLatestBackground } = useGenshinStore(); + const { selectedGame } = useSettings(); + const { backgroundHash, fetchLatestBackground } = + selectedGame == SupportedGames.GenshinImpact ? useGenshinStore() : useStarRailStore(); const [background, setBackground] = useState(null); useEffect(() => { diff --git a/src/backend/stores/settings.ts b/src/backend/stores/settings.ts new file mode 100644 index 0000000..0f0eb80 --- /dev/null +++ b/src/backend/stores/settings.ts @@ -0,0 +1,26 @@ +import { create } from "zustand"; +import { createJSONStorage, persist } from "zustand/middleware"; + +import { SupportedGames } from "@backend/types.ts"; + +export type SettingsStore = { + selectedGame: SupportedGames | any; + + setGame: (game: SupportedGames) => void; +}; + +const useSettings = create()( + persist( + (set) => ({ + selectedGame: SupportedGames.GenshinImpact, + + setGame: (selectedGame) => set({ selectedGame }) + }), + { + name: "settings", + storage: createJSONStorage(() => localStorage) + } + ) +); + +export default useSettings; diff --git a/src/backend/types.ts b/src/backend/types.ts index 00ee6d5..66977f8 100644 --- a/src/backend/types.ts +++ b/src/backend/types.ts @@ -67,3 +67,8 @@ export enum PostType { Activity = "POST_TYPE_ACTIVITY", Announcement = "POST_TYPE_ANNOUNCE" } + +export enum SupportedGames { + GenshinImpact = "genshin_impact", + StarRail = "starrail" +}