From 484fa863dc5eff4474f8b6a4543fcad30ecbac85 Mon Sep 17 00:00:00 2001 From: Chubby Granny Chaser Date: Sun, 16 Feb 2025 05:18:08 +0000 Subject: [PATCH] fix: fixing stale state --- src/main/events/index.ts | 1 + src/main/events/themes/add-custom-theme.ts | 4 +- src/main/events/themes/deeplink.ts | 38 ------------------- .../events/themes/delete-all-custom-themes.ts | 5 +-- src/main/events/themes/delete-custom-theme.ts | 4 +- .../events/themes/get-active-custom-theme.ts | 7 ++-- .../events/themes/get-all-custom-themes.ts | 4 +- .../events/themes/get-custom-theme-by-id.ts | 4 +- src/main/events/themes/update-custom-theme.ts | 17 +++++++-- src/main/level/sublevels/index.ts | 1 + src/main/level/sublevels/themes.ts | 2 +- src/preload/index.ts | 6 ++- src/renderer/src/declaration.d.ts | 3 +- src/renderer/src/hooks/use-feature.ts | 11 +++++- .../aparence/components/theme-card.tsx | 15 ++------ .../aparence/modals/import-theme-modal.tsx | 10 +---- .../src/pages/theme-editor/theme-editor.tsx | 8 +--- 17 files changed, 50 insertions(+), 90 deletions(-) delete mode 100644 src/main/events/themes/deeplink.ts diff --git a/src/main/events/index.ts b/src/main/events/index.ts index f377cdba..d3f70c2d 100644 --- a/src/main/events/index.ts +++ b/src/main/events/index.ts @@ -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"); diff --git a/src/main/events/themes/add-custom-theme.ts b/src/main/events/themes/add-custom-theme.ts index 8e9c4c98..95f526d9 100644 --- a/src/main/events/themes/add-custom-theme.ts +++ b/src/main/events/themes/add-custom-theme.ts @@ -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); diff --git a/src/main/events/themes/deeplink.ts b/src/main/events/themes/deeplink.ts deleted file mode 100644 index 47197a4f..00000000 --- a/src/main/events/themes/deeplink.ts +++ /dev/null @@ -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, -// }); -// }; diff --git a/src/main/events/themes/delete-all-custom-themes.ts b/src/main/events/themes/delete-all-custom-themes.ts index a3c6018e..d7a42d39 100644 --- a/src/main/events/themes/delete-all-custom-themes.ts +++ b/src/main/events/themes/delete-all-custom-themes.ts @@ -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); diff --git a/src/main/events/themes/delete-custom-theme.ts b/src/main/events/themes/delete-custom-theme.ts index 604053e0..d47c43fb 100644 --- a/src/main/events/themes/delete-custom-theme.ts +++ b/src/main/events/themes/delete-custom-theme.ts @@ -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); diff --git a/src/main/events/themes/get-active-custom-theme.ts b/src/main/events/themes/get-active-custom-theme.ts index a5018a35..b117f758 100644 --- a/src/main/events/themes/get-active-custom-theme.ts +++ b/src/main/events/themes/get-active-custom-theme.ts @@ -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); diff --git a/src/main/events/themes/get-all-custom-themes.ts b/src/main/events/themes/get-all-custom-themes.ts index 87a9c02a..f59a87cd 100644 --- a/src/main/events/themes/get-all-custom-themes.ts +++ b/src/main/events/themes/get-all-custom-themes.ts @@ -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); diff --git a/src/main/events/themes/get-custom-theme-by-id.ts b/src/main/events/themes/get-custom-theme-by-id.ts index 78d2ccb3..4ec5dc03 100644 --- a/src/main/events/themes/get-custom-theme-by-id.ts +++ b/src/main/events/themes/get-custom-theme-by-id.ts @@ -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); diff --git a/src/main/events/themes/update-custom-theme.ts b/src/main/events/themes/update-custom-theme.ts index 4773cfbf..38c97abe 100644 --- a/src/main/events/themes/update-custom-theme.ts +++ b/src/main/events/themes/update-custom-theme.ts @@ -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); diff --git a/src/main/level/sublevels/index.ts b/src/main/level/sublevels/index.ts index 3f0e840e..e63f0a3b 100644 --- a/src/main/level/sublevels/index.ts +++ b/src/main/level/sublevels/index.ts @@ -3,3 +3,4 @@ export * from "./games"; export * from "./game-shop-cache"; export * from "./game-achievements"; export * from "./keys"; +export * from "./themes"; diff --git a/src/main/level/sublevels/themes.ts b/src/main/level/sublevels/themes.ts index 3967c510..5e23468f 100644 --- a/src/main/level/sublevels/themes.ts +++ b/src/main/level/sublevels/themes.ts @@ -2,6 +2,6 @@ import type { Theme } from "@types"; import { db } from "../level"; import { levelKeys } from "./keys"; -export const themes = db.sublevel(levelKeys.themes, { +export const themesSublevel = db.sublevel(levelKeys.themes, { valueEncoding: "json", }); diff --git a/src/preload/index.ts b/src/preload/index.ts index 16415b89..34e3b8fd 100644 --- a/src/preload/index.ts +++ b/src/preload/index.ts @@ -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, diff --git a/src/renderer/src/declaration.d.ts b/src/renderer/src/declaration.d.ts index e8a6bf3c..ab36b5c8 100644 --- a/src/renderer/src/declaration.d.ts +++ b/src/renderer/src/declaration.d.ts @@ -286,9 +286,10 @@ declare global { getAllCustomThemes: () => Promise; deleteAllCustomThemes: () => Promise; deleteCustomTheme: (themeId: string) => Promise; - updateCustomTheme: (themeId: string, theme: Theme) => Promise; + updateCustomTheme: (themeId: string, code: string) => Promise; getCustomThemeById: (themeId: string) => Promise; getActiveCustomTheme: () => Promise; + toggleCustomTheme: (themeId: string, isActive: boolean) => Promise; onImportTheme: ( cb: (theme: string, author: string) => void ) => () => Electron.IpcRenderer; diff --git a/src/renderer/src/hooks/use-feature.ts b/src/renderer/src/hooks/use-feature.ts index 0ba71fe2..65d0f694 100644 --- a/src/renderer/src/hooks/use-feature.ts +++ b/src/renderer/src/hooks/use-feature.ts @@ -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(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); }; diff --git a/src/renderer/src/pages/settings/aparence/components/theme-card.tsx b/src/renderer/src/pages/settings/aparence/components/theme-card.tsx index b9460ffc..d6226b7e 100644 --- a/src/renderer/src/pages/settings/aparence/components/theme-card.tsx +++ b/src/renderer/src/pages/settings/aparence/components/theme-card.tsx @@ -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) { diff --git a/src/renderer/src/pages/settings/aparence/modals/import-theme-modal.tsx b/src/renderer/src/pages/settings/aparence/modals/import-theme-modal.tsx index 65eea7c3..eba51d6f 100644 --- a/src/renderer/src/pages/settings/aparence/modals/import-theme-modal.tsx +++ b/src/renderer/src/pages/settings/aparence/modals/import-theme-modal.tsx @@ -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(); diff --git a/src/renderer/src/pages/theme-editor/theme-editor.tsx b/src/renderer/src/pages/theme-editor/theme-editor.tsx index 9219f1d0..2bdfe6f2 100644 --- a/src/renderer/src/pages/theme-editor/theme-editor.tsx +++ b/src/renderer/src/pages/theme-editor/theme-editor.tsx @@ -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) {