diff --git a/components/editor/code-panel.tsx b/components/editor/code-panel.tsx index 9125f5e7..6e04eff2 100644 --- a/components/editor/code-panel.tsx +++ b/components/editor/code-panel.tsx @@ -1,4 +1,4 @@ -import { useState } from "react"; +import { useMemo, useState } from "react"; import { Button } from "@/components/ui/button"; import { Copy, Check, Heart } from "lucide-react"; import { ThemeEditorState } from "@/types/editor"; @@ -38,9 +38,7 @@ const CodePanel: React.FC = ({ themeEditorState }) => { const setPackageManager = usePreferencesStore( (state) => state.setPackageManager ); - const hasUnsavedChanges = useEditorStore( - (state) => state.hasThemeChangedFromCheckpoint - ); + const hasUnsavedChanges = useEditorStore((state) => state.hasUnsavedChanges); const isSavedPreset = useThemePresetStore( (state) => preset && state.getPreset(preset)?.source === "SAVED" @@ -101,27 +99,17 @@ const CodePanel: React.FC = ({ themeEditorState }) => { } }; + const showRegistryCommand = useMemo(() => { + return preset && preset !== "default" && !hasUnsavedChanges(); + }, [preset, hasUnsavedChanges]); + return (

Theme Code

- {preset && preset !== "default" && hasUnsavedChanges() && ( - - You have unsaved changes. - -
-
- - Save -
- your theme to get the registry command. -
-
-
- )} - {preset && preset !== "default" && !hasUnsavedChanges() && ( + {showRegistryCommand ? (
{(["pnpm", "npm", "yarn", "bun"] as const).map((pm) => ( @@ -158,13 +146,26 @@ const CodePanel: React.FC = ({ themeEditorState }) => {
- {getRegistryCommand(preset)} + {getRegistryCommand(preset as string)}
+ ) : ( + + You have unsaved changes. + +
+
+ + Save +
+ your theme to get the registry command. +
+
+
)}
diff --git a/components/editor/theme-preset-select.tsx b/components/editor/theme-preset-select.tsx index 5c0daa6a..dc1ae15c 100644 --- a/components/editor/theme-preset-select.tsx +++ b/components/editor/theme-preset-select.tsx @@ -36,7 +36,7 @@ import { } from "../ui/tooltip"; import { Input } from "../ui/input"; import { Badge } from "../ui/badge"; -import { cn, isDeepEqual } from "@/lib/utils"; +import { cn } from "@/lib/utils"; interface ThemePresetSelectProps { presets: Record; @@ -168,7 +168,7 @@ const ThemePresetSelect: React.FC = ({ currentPreset, onPresetChange, }) => { - const { themeState, hasThemeChangedFromCheckpoint } = useEditorStore(); + const { themeState, hasUnsavedChanges } = useEditorStore(); const { theme, toggleTheme } = useTheme(); const mode = themeState.currentMode; const [search, setSearch] = useState(""); @@ -256,19 +256,6 @@ const ThemePresetSelect: React.FC = ({ ); }, [filteredPresets, isSavedTheme]); - const hasUnsavedChanges = useMemo(() => { - return ( - hasThemeChangedFromCheckpoint() || - (!currentPreset && - !isDeepEqual(themeState.styles, presets["default"]?.styles)) - ); - }, [ - hasThemeChangedFromCheckpoint, - currentPreset, - themeState.styles, - presets, - ]); - return (
@@ -290,7 +277,7 @@ const ThemePresetSelect: React.FC = ({ {value !== "default" && value && isSavedTheme(value) && - !hasUnsavedChanges && ( + !hasUnsavedChanges() && (
= ({
)} - {hasUnsavedChanges ? ( + {hasUnsavedChanges() ? ( <>Custom (Unsaved) ) : ( presets[value || "default"]?.label || "default" diff --git a/store/editor-store.ts b/store/editor-store.ts index 21064604..3873b3b4 100644 --- a/store/editor-store.ts +++ b/store/editor-store.ts @@ -13,6 +13,7 @@ interface EditorStore { saveThemeCheckpoint: () => void; restoreThemeCheckpoint: () => void; hasThemeChangedFromCheckpoint: () => boolean; + hasUnsavedChanges: () => boolean; } export const useEditorStore = create()( @@ -53,6 +54,13 @@ export const useEditorStore = create()( const checkpoint = get().themeCheckpoint; return !isDeepEqual(get().themeState, checkpoint); }, + hasUnsavedChanges: () => { + const themeState = get().themeState; + const presetThemeStyles = getPresetThemeStyles( + themeState.preset ?? "default" + ); + return !isDeepEqual(themeState.styles, presetThemeStyles); + }, }), { name: "editor-storage",