From 7863427690a10162a0fb5ddcd7127f067bf6a0fd Mon Sep 17 00:00:00 2001 From: Beebles <102569435+beebls@users.noreply.github.com> Date: Mon, 26 Jun 2023 20:42:11 -0600 Subject: [PATCH] fix: Add "Theme Already Exists" warnings for presets and templates --- backend/tauriMethods/checkIfThemeExists.ts | 7 +++++++ backend/tauriMethods/index.ts | 1 + components/CreatePresetModal.tsx | 11 +++++++++-- components/Settings/CreateTemplateTheme.tsx | 20 +++++++++++++------- src-tauri/tauri.conf.json | 4 ++-- 5 files changed, 32 insertions(+), 11 deletions(-) create mode 100644 backend/tauriMethods/checkIfThemeExists.ts diff --git a/backend/tauriMethods/checkIfThemeExists.ts b/backend/tauriMethods/checkIfThemeExists.ts new file mode 100644 index 0000000..5c4386d --- /dev/null +++ b/backend/tauriMethods/checkIfThemeExists.ts @@ -0,0 +1,7 @@ +import { exists, BaseDirectory } from "@tauri-apps/api/fs"; + +export async function checkIfThemeExists(themeName: string): Promise { + return await exists(`homebrew/themes/${themeName}`, { + dir: BaseDirectory.Home, + }); +} diff --git a/backend/tauriMethods/index.ts b/backend/tauriMethods/index.ts index 246aec6..65c6d57 100644 --- a/backend/tauriMethods/index.ts +++ b/backend/tauriMethods/index.ts @@ -6,3 +6,4 @@ export * from "./checkIfStandaloneBackendExists"; export * from "./copyBackend"; export * from "./getOS"; export * from "./downloadTemplate"; +export * from "./checkIfThemeExists"; diff --git a/components/CreatePresetModal.tsx b/components/CreatePresetModal.tsx index c0cc395..24eaf39 100644 --- a/components/CreatePresetModal.tsx +++ b/components/CreatePresetModal.tsx @@ -1,6 +1,6 @@ import { useContext, useMemo, useState } from "react"; import { themeContext } from "@contexts/themeContext"; -import { generatePreset } from "../backend"; +import { checkIfThemeExists, generatePreset, toast } from "../backend"; import { BiPlus } from "react-icons/bi"; import { LabelledInput, Modal } from "./Primitives"; import { twMerge } from "tailwind-merge"; @@ -12,9 +12,16 @@ export function CreatePresetModal() { [localThemeList] ); const [presetName, setPresetName] = useState(""); - function createPreset() { + async function createPreset() { if (presetName) { + const alreadyExists = await checkIfThemeExists(presetName); + if (alreadyExists) { + toast("Theme Already Exists!"); + setPresetName(""); + return; + } generatePreset(presetName).then(() => { + toast("Preset Created Successfully"); refreshThemes(true); setPresetName(""); }); diff --git a/components/Settings/CreateTemplateTheme.tsx b/components/Settings/CreateTemplateTheme.tsx index d87b57c..beaf9e3 100644 --- a/components/Settings/CreateTemplateTheme.tsx +++ b/components/Settings/CreateTemplateTheme.tsx @@ -1,8 +1,9 @@ import { useMemo, useState, useContext } from "react"; import { LabelledInput, Modal } from ".."; -import { downloadTemplate, toast } from "backend"; +import { checkIfThemeExists, downloadTemplate, toast } from "backend"; import { ImSpinner5 } from "react-icons/im"; import { themeContext } from "@contexts/themeContext"; +import { exists, BaseDirectory } from "@tauri-apps/api/fs"; export function CreateTemplateTheme({ ongoingAction }: { ongoingAction: boolean }) { const { refreshThemes } = useContext(themeContext); @@ -29,14 +30,19 @@ export function CreateTemplateTheme({ ongoingAction }: { ongoingAction: boolean } actionDisabled={name.length === 0 || nameContainsInvalidCharacters || invalidName} actionText="Create" - onAction={() => + onAction={async () => { + const alreadyExists = await checkIfThemeExists(name); + if (alreadyExists) { + toast("Theme Already Exists!"); + setName(""); + return; + } downloadTemplate(name).then((success) => { - if (!success) { - toast("Error Creating Template"); - } + toast(success ? "Template Created Successfully" : "Error Creating Template"); refreshThemes(true); - }) - } + setName(""); + }); + }} title="Create Template Theme" description={`This will create a blank theme in your themes folder that you can use as the starting point for your own theme.`} triggerDisabled={ongoingAction} diff --git a/src-tauri/tauri.conf.json b/src-tauri/tauri.conf.json index 2fb516e..e313b49 100644 --- a/src-tauri/tauri.conf.json +++ b/src-tauri/tauri.conf.json @@ -32,7 +32,7 @@ "$CONFIG\\Microsoft\\Windows\\Start Menu\\Programs\\Startup\\*", "$APPDATA/standaloneVersion.txt", "$APPDATA", - "$HOME/homebrew/themes" + "$HOME/homebrew/themes/*" ], "writeFile": true }, @@ -183,7 +183,7 @@ "resizable": true, "title": "CSSLoader Desktop", "decorations": false, - "transparent": true, + "transparent": true, "width": 800, "minWidth": 460, "minHeight": 300