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/css-injector";
import "./themes/close-editor-window"; import "./themes/close-editor-window";
import "./themes/import-theme"; import "./themes/import-theme";
import "./themes/toggle-custom-theme";
import { isPortableVersion } from "@main/helpers"; import { isPortableVersion } from "@main/helpers";
ipcMain.handle("ping", () => "pong"); ipcMain.handle("ping", () => "pong");

View file

@ -1,12 +1,12 @@
import { Theme } from "@types"; import { Theme } from "@types";
import { themes } from "@main/level/sublevels/themes";
import { registerEvent } from "../register-event"; import { registerEvent } from "../register-event";
import { themesSublevel } from "@main/level";
const addCustomTheme = async ( const addCustomTheme = async (
_event: Electron.IpcMainInvokeEvent, _event: Electron.IpcMainInvokeEvent,
theme: Theme theme: Theme
) => { ) => {
await themes.put(theme.id, theme); await themesSublevel.put(theme.id, theme);
}; };
registerEvent("addCustomTheme", addCustomTheme); 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"; import { registerEvent } from "../register-event";
const deleteAllCustomThemes = async (_event: Electron.IpcMainInvokeEvent) => { const deleteAllCustomThemes = async (_event: Electron.IpcMainInvokeEvent) => {
console.log("sexo2"); await themesSublevel.clear();
await themes.clear();
}; };
registerEvent("deleteAllCustomThemes", deleteAllCustomThemes); 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"; import { registerEvent } from "../register-event";
const deleteCustomTheme = async ( const deleteCustomTheme = async (
_event: Electron.IpcMainInvokeEvent, _event: Electron.IpcMainInvokeEvent,
themeId: string themeId: string
) => { ) => {
await themes.del(themeId); await themesSublevel.del(themeId);
}; };
registerEvent("deleteCustomTheme", deleteCustomTheme); registerEvent("deleteCustomTheme", deleteCustomTheme);

View file

@ -1,10 +1,9 @@
import { themesSublevel } from "@main/level";
import { registerEvent } from "../register-event"; import { registerEvent } from "../register-event";
import { themes } from "@main/level/sublevels/themes";
import { Theme } from "@types";
const getActiveCustomTheme = async () => { const getActiveCustomTheme = async () => {
const allThemes = await themes.values().all(); const allThemes = await themesSublevel.values().all();
return allThemes.find((theme: Theme) => theme.isActive); return allThemes.find((theme) => theme.isActive);
}; };
registerEvent("getActiveCustomTheme", getActiveCustomTheme); 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"; import { registerEvent } from "../register-event";
const getAllCustomThemes = async (_event: Electron.IpcMainInvokeEvent) => { const getAllCustomThemes = async (_event: Electron.IpcMainInvokeEvent) => {
return await themes.values().all(); return themesSublevel.values().all();
}; };
registerEvent("getAllCustomThemes", getAllCustomThemes); 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"; import { registerEvent } from "../register-event";
const getCustomThemeById = async ( const getCustomThemeById = async (
_event: Electron.IpcMainInvokeEvent, _event: Electron.IpcMainInvokeEvent,
themeId: string themeId: string
) => { ) => {
return await themes.get(themeId); return themesSublevel.get(themeId);
}; };
registerEvent("getCustomThemeById", getCustomThemeById); 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 { registerEvent } from "../register-event";
import { Theme } from "@types";
const updateCustomTheme = async ( const updateCustomTheme = async (
_event: Electron.IpcMainInvokeEvent, _event: Electron.IpcMainInvokeEvent,
themeId: string, 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); registerEvent("updateCustomTheme", updateCustomTheme);

View file

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

View file

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

View file

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

View file

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

View file

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

View file

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

View file

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

View file

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