fix: fixing stale state

This commit is contained in:
Chubby Granny Chaser 2025-02-16 05:18:08 +00:00
parent 9449d7cdcd
commit 484fa863dc
No known key found for this signature in database
17 changed files with 50 additions and 90 deletions

View file

@ -88,6 +88,7 @@ import "./themes/get-active-custom-theme";
import "./themes/css-injector";
import "./themes/close-editor-window";
import "./themes/import-theme";
import "./themes/toggle-custom-theme";
import { isPortableVersion } from "@main/helpers";
ipcMain.handle("ping", () => "pong");

View file

@ -1,12 +1,12 @@
import { Theme } from "@types";
import { themes } from "@main/level/sublevels/themes";
import { registerEvent } from "../register-event";
import { themesSublevel } from "@main/level";
const addCustomTheme = async (
_event: Electron.IpcMainInvokeEvent,
theme: Theme
) => {
await themes.put(theme.id, theme);
await themesSublevel.put(theme.id, theme);
};
registerEvent("addCustomTheme", addCustomTheme);

View file

@ -1,38 +0,0 @@
// import { themes } from "@main/level/sublevels/themes";
// import { WindowManager } from "@main/services";
// import { Theme } from "@types";
// export const handleDeepLinkTheme = async (
// themeName: string,
// authorCode: string
// ) => {
// const theme: Theme = {
// id: crypto.randomUUID(),
// name: themeName,
// isActive: false,
// author: authorCode,
// authorName: "spectre",
// code: `https://hydrathemes.shop/themes/${themeName}.css`,
// createdAt: new Date(),
// updatedAt: new Date(),
// };
// await themes.put(theme.id, theme);
// const allThemes = await themes.values().all();
// const activeTheme = allThemes.find((theme: Theme) => theme.isActive);
// if (activeTheme) {
// await themes.put(activeTheme.id, {
// ...activeTheme,
// isActive: false,
// });
// }
// WindowManager.mainWindow?.webContents.send("css-injected", theme.code);
// await themes.put(theme.id, {
// ...theme,
// isActive: true,
// });
// };

View file

@ -1,9 +1,8 @@
import { themes } from "@main/level/sublevels/themes";
import { themesSublevel } from "@main/level";
import { registerEvent } from "../register-event";
const deleteAllCustomThemes = async (_event: Electron.IpcMainInvokeEvent) => {
console.log("sexo2");
await themes.clear();
await themesSublevel.clear();
};
registerEvent("deleteAllCustomThemes", deleteAllCustomThemes);

View file

@ -1,11 +1,11 @@
import { themes } from "@main/level/sublevels/themes";
import { themesSublevel } from "@main/level";
import { registerEvent } from "../register-event";
const deleteCustomTheme = async (
_event: Electron.IpcMainInvokeEvent,
themeId: string
) => {
await themes.del(themeId);
await themesSublevel.del(themeId);
};
registerEvent("deleteCustomTheme", deleteCustomTheme);

View file

@ -1,10 +1,9 @@
import { themesSublevel } from "@main/level";
import { registerEvent } from "../register-event";
import { themes } from "@main/level/sublevels/themes";
import { Theme } from "@types";
const getActiveCustomTheme = async () => {
const allThemes = await themes.values().all();
return allThemes.find((theme: Theme) => theme.isActive);
const allThemes = await themesSublevel.values().all();
return allThemes.find((theme) => theme.isActive);
};
registerEvent("getActiveCustomTheme", getActiveCustomTheme);

View file

@ -1,8 +1,8 @@
import { themes } from "@main/level/sublevels/themes";
import { themesSublevel } from "@main/level";
import { registerEvent } from "../register-event";
const getAllCustomThemes = async (_event: Electron.IpcMainInvokeEvent) => {
return await themes.values().all();
return themesSublevel.values().all();
};
registerEvent("getAllCustomThemes", getAllCustomThemes);

View file

@ -1,11 +1,11 @@
import { themes } from "@main/level/sublevels/themes";
import { themesSublevel } from "@main/level";
import { registerEvent } from "../register-event";
const getCustomThemeById = async (
_event: Electron.IpcMainInvokeEvent,
themeId: string
) => {
return await themes.get(themeId);
return themesSublevel.get(themeId);
};
registerEvent("getCustomThemeById", getCustomThemeById);

View file

@ -1,13 +1,22 @@
import { themes } from "@main/level/sublevels/themes";
import { themesSublevel } from "@main/level";
import { registerEvent } from "../register-event";
import { Theme } from "@types";
const updateCustomTheme = async (
_event: Electron.IpcMainInvokeEvent,
themeId: string,
theme: Theme
code: string
) => {
await themes.put(themeId, theme);
const theme = await themesSublevel.get(themeId);
if (!theme) {
throw new Error("Theme not found");
}
await themesSublevel.put(themeId, {
...theme,
code,
updatedAt: new Date(),
});
};
registerEvent("updateCustomTheme", updateCustomTheme);

View file

@ -3,3 +3,4 @@ export * from "./games";
export * from "./game-shop-cache";
export * from "./game-achievements";
export * from "./keys";
export * from "./themes";

View file

@ -2,6 +2,6 @@ import type { Theme } from "@types";
import { db } from "../level";
import { levelKeys } from "./keys";
export const themes = db.sublevel<string, Theme>(levelKeys.themes, {
export const themesSublevel = db.sublevel<string, Theme>(levelKeys.themes, {
valueEncoding: "json",
});

View file

@ -355,11 +355,13 @@ contextBridge.exposeInMainWorld("electron", {
deleteAllCustomThemes: () => ipcRenderer.invoke("deleteAllCustomThemes"),
deleteCustomTheme: (themeId: string) =>
ipcRenderer.invoke("deleteCustomTheme", themeId),
updateCustomTheme: (themeId: string, theme: Theme) =>
ipcRenderer.invoke("updateCustomTheme", themeId, theme),
updateCustomTheme: (themeId: string, code: string) =>
ipcRenderer.invoke("updateCustomTheme", themeId, code),
getCustomThemeById: (themeId: string) =>
ipcRenderer.invoke("getCustomThemeById", themeId),
getActiveCustomTheme: () => ipcRenderer.invoke("getActiveCustomTheme"),
toggleCustomTheme: (themeId: string, isActive: boolean) =>
ipcRenderer.invoke("toggleCustomTheme", themeId, isActive),
onImportTheme: (cb: (theme: string, author: string) => void) => {
const listener = (
_event: Electron.IpcRendererEvent,

View file

@ -286,9 +286,10 @@ declare global {
getAllCustomThemes: () => Promise<Theme[]>;
deleteAllCustomThemes: () => Promise<void>;
deleteCustomTheme: (themeId: string) => Promise<void>;
updateCustomTheme: (themeId: string, theme: Theme) => Promise<void>;
updateCustomTheme: (themeId: string, code: string) => Promise<void>;
getCustomThemeById: (themeId: string) => Promise<Theme | null>;
getActiveCustomTheme: () => Promise<Theme | null>;
toggleCustomTheme: (themeId: string, isActive: boolean) => Promise<void>;
onImportTheme: (
cb: (theme: string, author: string) => void
) => () => Electron.IpcRenderer;

View file

@ -1,4 +1,4 @@
import { useEffect } from "react";
import { useEffect, useState } from "react";
enum Feature {
CheckDownloadWritePermission = "CHECK_DOWNLOAD_WRITE_PERMISSION",
@ -6,14 +6,21 @@ enum Feature {
}
export function useFeature() {
const [features, setFeatures] = useState<string[] | null>(null);
useEffect(() => {
window.electron.getFeatures().then((features) => {
localStorage.setItem("features", JSON.stringify(features || []));
setFeatures(features || []);
});
}, []);
const isFeatureEnabled = (feature: Feature) => {
const features = JSON.parse(localStorage.getItem("features") ?? "[]");
if (!features) {
const features = JSON.parse(localStorage.getItem("features") ?? "[]");
return features.includes(feature);
}
return features.includes(feature);
};

View file

@ -29,20 +29,14 @@ export const ThemeCard = ({ theme, onListUpdated }: ThemeCardProps) => {
if (activeTheme) {
removeCustomCss();
await window.electron.updateCustomTheme(activeTheme.id, {
...activeTheme,
isActive: false,
});
await window.electron.toggleCustomTheme(activeTheme.id, false);
}
if (currentTheme.code) {
injectCustomCss(currentTheme.code);
}
await window.electron.updateCustomTheme(currentTheme.id, {
...currentTheme,
isActive: true,
});
await window.electron.toggleCustomTheme(currentTheme.id, true);
onListUpdated();
} catch (error) {
@ -53,10 +47,7 @@ export const ThemeCard = ({ theme, onListUpdated }: ThemeCardProps) => {
const handleUnsetTheme = async () => {
try {
removeCustomCss();
await window.electron.updateCustomTheme(theme.id, {
...theme,
isActive: false,
});
await window.electron.toggleCustomTheme(theme.id, false);
onListUpdated();
} catch (error) {

View file

@ -47,20 +47,14 @@ export const ImportThemeModal = ({
if (activeTheme) {
removeCustomCss();
await window.electron.updateCustomTheme(activeTheme.id, {
...activeTheme,
isActive: false,
});
await window.electron.toggleCustomTheme(activeTheme.id, false);
}
if (currentTheme.code) {
injectCustomCss(currentTheme.code);
}
await window.electron.updateCustomTheme(currentTheme.id, {
...currentTheme,
isActive: true,
});
await window.electron.toggleCustomTheme(currentTheme.id, true);
onThemeImported();
showSuccessToast(t("theme_imported"));
onClose();

View file

@ -31,13 +31,7 @@ export default function ThemeEditor() {
const handleSave = useCallback(async () => {
if (theme) {
const updatedTheme = {
...theme,
code: code,
updatedAt: new Date(),
};
await window.electron.updateCustomTheme(theme.id, updatedTheme);
await window.electron.updateCustomTheme(theme.id, code);
setHasUnsavedChanges(false);
if (theme.isActive) {