mirror of
https://github.com/Grasscutters/Cultivation.git
synced 2025-12-13 15:44:35 +01:00
(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:
@@ -1,13 +1,16 @@
|
|||||||
import { useEffect, useState } from "preact/hooks";
|
import { useEffect, useState } from "preact/hooks";
|
||||||
|
|
||||||
|
import useSettings from "@backend/stores/settings.ts";
|
||||||
|
|
||||||
import { create } from "zustand";
|
import { create } from "zustand";
|
||||||
import { persist, createJSONStorage } from "zustand/middleware";
|
import { persist, createJSONStorage } from "zustand/middleware";
|
||||||
|
|
||||||
import { invoke } from "@tauri-apps/api";
|
import { invoke } from "@tauri-apps/api";
|
||||||
|
import { fetch } from "@tauri-apps/api/http";
|
||||||
import { exists } from "@tauri-apps/api/fs";
|
import { exists } from "@tauri-apps/api/fs";
|
||||||
import { convertFileSrc } from "@tauri-apps/api/tauri";
|
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";
|
import { AppDataPath, LauncherUrls } from "@app/constants.ts";
|
||||||
|
|
||||||
export type GameDataStore = {
|
export type GameDataStore = {
|
||||||
@@ -52,8 +55,8 @@ export const useStarRailStore = create<GameDataStore>()(
|
|||||||
*/
|
*/
|
||||||
export async function fetchLatestBackground(set: StoreWrite, serviceUrl: string): Promise<void> {
|
export async function fetchLatestBackground(set: StoreWrite, serviceUrl: string): Promise<void> {
|
||||||
// Fetch the launcher data.
|
// Fetch the launcher data.
|
||||||
const launcherData = await fetch(serviceUrl, { cache: "force-cache" });
|
const launcherData = await fetch<LauncherResponse>(serviceUrl);
|
||||||
const responseData = await launcherData.json() as LauncherResponse;
|
const responseData = launcherData.data;
|
||||||
|
|
||||||
// Check if the background exists on the system.
|
// Check if the background exists on the system.
|
||||||
const backgroundUrl = responseData.data.adv.background;
|
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.
|
* React hook which returns the URL of the locally cached background image.
|
||||||
*/
|
*/
|
||||||
export function useBackground() {
|
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);
|
const [background, setBackground] = useState<string | null>(null);
|
||||||
|
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
|
|||||||
26
src/backend/stores/settings.ts
Normal file
26
src/backend/stores/settings.ts
Normal 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;
|
||||||
@@ -67,3 +67,8 @@ export enum PostType {
|
|||||||
Activity = "POST_TYPE_ACTIVITY",
|
Activity = "POST_TYPE_ACTIVITY",
|
||||||
Announcement = "POST_TYPE_ANNOUNCE"
|
Announcement = "POST_TYPE_ANNOUNCE"
|
||||||
}
|
}
|
||||||
|
|
||||||
|
export enum SupportedGames {
|
||||||
|
GenshinImpact = "genshin_impact",
|
||||||
|
StarRail = "starrail"
|
||||||
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user