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

@ -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) {