|
1 | | -import { useState } from "react"; |
| 1 | +import { useMemo, useState } from "react"; |
2 | 2 | import { Button } from "@/components/ui/button"; |
3 | 3 | import { Copy, Check, Heart } from "lucide-react"; |
4 | 4 | import { ThemeEditorState } from "@/types/editor"; |
@@ -38,9 +38,7 @@ const CodePanel: React.FC<CodePanelProps> = ({ themeEditorState }) => { |
38 | 38 | const setPackageManager = usePreferencesStore( |
39 | 39 | (state) => state.setPackageManager |
40 | 40 | ); |
41 | | - const hasUnsavedChanges = useEditorStore( |
42 | | - (state) => state.hasThemeChangedFromCheckpoint |
43 | | - ); |
| 41 | + const hasUnsavedChanges = useEditorStore((state) => state.hasUnsavedChanges); |
44 | 42 |
|
45 | 43 | const isSavedPreset = useThemePresetStore( |
46 | 44 | (state) => preset && state.getPreset(preset)?.source === "SAVED" |
@@ -101,27 +99,17 @@ const CodePanel: React.FC<CodePanelProps> = ({ themeEditorState }) => { |
101 | 99 | } |
102 | 100 | }; |
103 | 101 |
|
| 102 | + const showRegistryCommand = useMemo(() => { |
| 103 | + return preset && preset !== "default" && !hasUnsavedChanges(); |
| 104 | + }, [preset, hasUnsavedChanges]); |
| 105 | + |
104 | 106 | return ( |
105 | 107 | <div className="h-full flex flex-col"> |
106 | 108 | <div className="flex-none mb-4"> |
107 | 109 | <div className="flex items-center justify-between gap-2"> |
108 | 110 | <h2 className="text-lg font-semibold">Theme Code</h2> |
109 | 111 | </div> |
110 | | - {preset && preset !== "default" && hasUnsavedChanges() && ( |
111 | | - <Alert className="mt-4"> |
112 | | - <AlertTitle>You have unsaved changes.</AlertTitle> |
113 | | - <AlertDescription className="flex flex-col gap-2 mt-2"> |
114 | | - <div className="flex items-center gap-1"> |
115 | | - <div className="flex items-center gap-1 px-2 py-0.5 border rounded-md"> |
116 | | - <Heart className="size-3.5" /> |
117 | | - <span>Save</span> |
118 | | - </div> |
119 | | - your theme to get the registry command. |
120 | | - </div> |
121 | | - </AlertDescription> |
122 | | - </Alert> |
123 | | - )} |
124 | | - {preset && preset !== "default" && !hasUnsavedChanges() && ( |
| 112 | + {showRegistryCommand ? ( |
125 | 113 | <div className="mt-4 rounded-md overflow-hidden border"> |
126 | 114 | <div className="flex border-b"> |
127 | 115 | {(["pnpm", "npm", "yarn", "bun"] as const).map((pm) => ( |
@@ -158,13 +146,26 @@ const CodePanel: React.FC<CodePanelProps> = ({ themeEditorState }) => { |
158 | 146 | <ScrollArea className="w-full"> |
159 | 147 | <div className="whitespace-nowrap overflow-y-hidden pb-2"> |
160 | 148 | <code className="text-sm font-mono"> |
161 | | - {getRegistryCommand(preset)} |
| 149 | + {getRegistryCommand(preset as string)} |
162 | 150 | </code> |
163 | 151 | </div> |
164 | 152 | <ScrollBar orientation="horizontal" /> |
165 | 153 | </ScrollArea> |
166 | 154 | </div> |
167 | 155 | </div> |
| 156 | + ) : ( |
| 157 | + <Alert className="mt-4"> |
| 158 | + <AlertTitle>You have unsaved changes.</AlertTitle> |
| 159 | + <AlertDescription className="flex flex-col gap-2 mt-2"> |
| 160 | + <div className="flex items-center gap-1"> |
| 161 | + <div className="flex items-center gap-1 px-2 py-0.5 border rounded-md"> |
| 162 | + <Heart className="size-3.5" /> |
| 163 | + <span>Save</span> |
| 164 | + </div> |
| 165 | + your theme to get the registry command. |
| 166 | + </div> |
| 167 | + </AlertDescription> |
| 168 | + </Alert> |
168 | 169 | )} |
169 | 170 | </div> |
170 | 171 | <div className="flex items-center gap-2 mb-4 "> |
|
0 commit comments