(feat:background) Implement game-specific backgrounds, depending on what's enabled

this also uses the Tauri built-in HTTP library to bypass the CORS restriction set by miHoYo for some reason on the HSR API
This commit is contained in:
KingRainbow44
2023-11-30 11:59:30 -05:00
parent 142befac86
commit 4d37cfdb1c
3 changed files with 40 additions and 4 deletions

View File

@@ -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<GameDataStore>()(
*/
export async function fetchLatestBackground(set: StoreWrite, serviceUrl: string): Promise<void> {
// Fetch the launcher data.
const launcherData = await fetch(serviceUrl, { cache: "force-cache" });
const responseData = await launcherData.json() as LauncherResponse;
const launcherData = await fetch<LauncherResponse>(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<string> {
* 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<string | null>(null);
useEffect(() => {

View File

@@ -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<SettingsStore>()(
persist(
(set) => ({
selectedGame: SupportedGames.GenshinImpact,
setGame: (selectedGame) => set({ selectedGame })
}),
{
name: "settings",
storage: createJSONStorage(() => localStorage)
}
)
);
export default useSettings;

View File

@@ -67,3 +67,8 @@ export enum PostType {
Activity = "POST_TYPE_ACTIVITY",
Announcement = "POST_TYPE_ANNOUNCE"
}
export enum SupportedGames {
GenshinImpact = "genshin_impact",
StarRail = "starrail"
}