This commit is contained in:
Hachi-R 2025-02-16 01:28:01 -03:00
parent d7d88ecb8c
commit bf3905f19e
5 changed files with 87 additions and 80 deletions

View file

@ -4,7 +4,7 @@ import { WindowManager } from "@main/services";
const importTheme = async ( const importTheme = async (
_event: Electron.IpcMainInvokeEvent, _event: Electron.IpcMainInvokeEvent,
theme: string, theme: string,
author: string, author: string
) => { ) => {
WindowManager.mainWindow?.webContents.send("import-theme", theme, author); WindowManager.mainWindow?.webContents.send("import-theme", theme, author);
}; };

View file

@ -92,7 +92,11 @@ const handleDeepLinkPath = (uri?: string) => {
const authorCode = url.searchParams.get("author"); const authorCode = url.searchParams.get("author");
if (themeName && authorCode) { if (themeName && authorCode) {
WindowManager.mainWindow?.webContents.send("import-theme", themeName, authorCode); WindowManager.mainWindow?.webContents.send(
"import-theme",
themeName,
authorCode
);
} }
} }
} catch (error) { } catch (error) {

View file

@ -361,8 +361,11 @@ contextBridge.exposeInMainWorld("electron", {
ipcRenderer.invoke("getCustomThemeById", themeId), ipcRenderer.invoke("getCustomThemeById", themeId),
getActiveCustomTheme: () => ipcRenderer.invoke("getActiveCustomTheme"), getActiveCustomTheme: () => ipcRenderer.invoke("getActiveCustomTheme"),
onImportTheme: (cb: (theme: string, author: string) => void) => { onImportTheme: (cb: (theme: string, author: string) => void) => {
const listener = (_event: Electron.IpcRendererEvent, theme: string, author: string) => const listener = (
cb(theme, author); _event: Electron.IpcRendererEvent,
theme: string,
author: string
) => cb(theme, author);
ipcRenderer.on("import-theme", listener); ipcRenderer.on("import-theme", listener);
return () => ipcRenderer.removeListener("import-theme", listener); return () => ipcRenderer.removeListener("import-theme", listener);
}, },

View file

@ -39,13 +39,13 @@ export interface AppProps {
export function App() { export function App() {
const contentRef = useRef<HTMLDivElement>(null); const contentRef = useRef<HTMLDivElement>(null);
const { updateLibrary, library } = useLibrary(); const { updateLibrary, library } = useLibrary();
const [isImportThemeModalVisible, setIsImportThemeModalVisible] = useState(false); const [isImportThemeModalVisible, setIsImportThemeModalVisible] =
useState(false);
const [importTheme, setImportTheme] = useState<{ const [importTheme, setImportTheme] = useState<{
theme: string; theme: string;
author: string; author: string;
} | null>(null); } | null>(null);
const { t } = useTranslation("app"); const { t } = useTranslation("app");
const { updateRepacks } = useRepacks(); const { updateRepacks } = useRepacks();

View file

@ -7,86 +7,86 @@ import { injectCustomCss, removeCustomCss } from "@renderer/helpers";
import { useToast } from "@renderer/hooks"; import { useToast } from "@renderer/hooks";
interface ImportThemeModalProps { interface ImportThemeModalProps {
visible: boolean; visible: boolean;
onClose: () => void; onClose: () => void;
onThemeImported: () => void; onThemeImported: () => void;
themeName: string; themeName: string;
authorCode: string; authorCode: string;
} }
export const ImportThemeModal = ({ export const ImportThemeModal = ({
visible, visible,
onClose, onClose,
onThemeImported, onThemeImported,
themeName, themeName,
authorCode, authorCode,
}: ImportThemeModalProps) => { }: ImportThemeModalProps) => {
const { t } = useTranslation("settings"); const { t } = useTranslation("settings");
const { showSuccessToast, showErrorToast } = useToast(); const { showSuccessToast, showErrorToast } = useToast();
const handleImportTheme = async () => { const handleImportTheme = async () => {
const theme: Theme = { const theme: Theme = {
id: crypto.randomUUID(), id: crypto.randomUUID(),
name: themeName, name: themeName,
isActive: false, isActive: false,
author: authorCode, author: authorCode,
authorName: "spectre", authorName: "spectre",
code: `https://hydrathemes.shop/themes/${themeName}.css`, code: `https://hydrathemes.shop/themes/${themeName}.css`,
createdAt: new Date(), createdAt: new Date(),
updatedAt: new Date(), updatedAt: new Date(),
};
try {
await window.electron.addCustomTheme(theme);
const currentTheme = await window.electron.getCustomThemeById(theme.id);
if (!currentTheme) return;
const activeTheme = await window.electron.getActiveCustomTheme();
if (activeTheme) {
removeCustomCss();
await window.electron.updateCustomTheme(activeTheme.id, {
...activeTheme,
isActive: false,
});
}
if (currentTheme.code) {
injectCustomCss(currentTheme.code);
}
await window.electron.updateCustomTheme(currentTheme.id, {
...currentTheme,
isActive: true,
});
onThemeImported();
showSuccessToast(t("theme_imported"));
onClose();
} catch (error) {
console.error(error);
showErrorToast(t("error_importing_theme"));
onClose();
}
}; };
return ( try {
<Modal await window.electron.addCustomTheme(theme);
visible={visible}
title={t("import_theme")}
description={t("import_theme_description")}
onClose={onClose}
>
<div className="delete-all-themes-modal__container">
<Button theme="outline" onClick={handleImportTheme}>
{t("import_theme")}
</Button>
<Button theme="primary" onClick={onClose}> const currentTheme = await window.electron.getCustomThemeById(theme.id);
{t("cancel")}
</Button> if (!currentTheme) return;
</div>
</Modal> const activeTheme = await window.electron.getActiveCustomTheme();
);
if (activeTheme) {
removeCustomCss();
await window.electron.updateCustomTheme(activeTheme.id, {
...activeTheme,
isActive: false,
});
}
if (currentTheme.code) {
injectCustomCss(currentTheme.code);
}
await window.electron.updateCustomTheme(currentTheme.id, {
...currentTheme,
isActive: true,
});
onThemeImported();
showSuccessToast(t("theme_imported"));
onClose();
} catch (error) {
console.error(error);
showErrorToast(t("error_importing_theme"));
onClose();
}
};
return (
<Modal
visible={visible}
title={t("import_theme")}
description={t("import_theme_description")}
onClose={onClose}
>
<div className="delete-all-themes-modal__container">
<Button theme="outline" onClick={handleImportTheme}>
{t("import_theme")}
</Button>
<Button theme="primary" onClick={onClose}>
{t("cancel")}
</Button>
</div>
</Modal>
);
}; };