@@ -153,6 +153,51 @@ class CssSnippetStoreModal extends Modal {
153
153
return await vault . adapter . exists ( fullPath ) ;
154
154
}
155
155
156
+ private updateSnippetCard ( snippet : Snippet ) {
157
+ const card = this . contentEl . querySelector ( `.community-item[data-snippet-id="${ snippet . id } "]` ) as HTMLDivElement ;
158
+ if ( ! card ) return ;
159
+
160
+ const buttonWrapper = card . querySelector ( '.snippet-store-button-wrapper' ) as HTMLDivElement ;
161
+ if ( ! buttonWrapper ) return ;
162
+
163
+ // Clear any existing button
164
+ buttonWrapper . empty ( ) ;
165
+
166
+ const button = buttonWrapper . createEl ( 'button' ) ;
167
+ button . classList . add ( 'mod-cta' ) ; // default, will be overridden
168
+
169
+ this . checkSnippetExists ( snippet . id ) . then ( ( exists ) => {
170
+ if ( exists ) {
171
+ button . textContent = 'Delete' ;
172
+ button . className = 'mod-danger' ;
173
+ button . addEventListener ( 'click' , async ( ) => {
174
+ await this . uninstall ( snippet . id ) ;
175
+ this . updateSnippetCard ( snippet ) ;
176
+ } ) ;
177
+ } else {
178
+ button . textContent = 'Install' ;
179
+ button . className = 'mod-cta' ;
180
+ button . addEventListener ( 'click' , async ( ) => {
181
+ const url = `https://raw.githubusercontent.com/${ snippet . repo } /refs/heads/main/${ snippet . folder } /snippet.css` ;
182
+ try {
183
+ if ( await isOnline ( ) ) {
184
+ const response = await fetchWithTimeout ( url ) ;
185
+ if ( ! response . ok ) throw new Error ( `Network response was not ok: ${ response . statusText } ` ) ;
186
+ const code = await response . text ( ) ;
187
+ await this . install ( snippet . id , code ) ;
188
+ this . updateSnippetCard ( snippet ) ;
189
+ } else {
190
+ new Notice ( `No Internet connection...` ) ;
191
+ }
192
+ } catch ( error ) {
193
+ console . error ( error ) ;
194
+ new Notice ( `Error: ${ error . message } ` ) ;
195
+ }
196
+ } ) ;
197
+ }
198
+ } ) ;
199
+ }
200
+
156
201
private renderSnippetsUI ( filter : string = "" ) {
157
202
const { contentEl } = this ;
158
203
const grid = contentEl . querySelector ( '.community-items-container' ) as HTMLDivElement ;
@@ -183,44 +228,16 @@ class CssSnippetStoreModal extends Modal {
183
228
184
229
filteredSnippets . forEach ( snippet => {
185
230
const card = grid . createDiv ( { cls : 'community-item' } ) ;
231
+ card . setAttr ( "data-snippet-id" , snippet . id ) ;
186
232
187
233
card . createEl ( 'div' , { text : snippet . name , cls : 'community-item-name' } ) ;
188
234
card . createEl ( 'div' , { text : `By ${ snippet . author } ` , cls : 'community-item-author' } ) ;
189
235
card . createEl ( 'div' , { text : snippet . description , cls : 'community-desc' } ) ;
190
236
191
- const buttonWrapper = card . createEl ( 'div' , { cls : 'snippet-store-button-wrapper' } ) ;
192
- const button = buttonWrapper . createEl ( 'button' , { cls : 'mod-cta' } ) ;
193
-
194
- this . checkSnippetExists ( snippet . id ) . then ( ( exists ) => {
195
- if ( exists ) {
196
- button . textContent = 'Delete' ;
197
- button . className = 'mod-danger' ;
198
- button . addEventListener ( 'click' , async ( ) => {
199
- await this . uninstall ( snippet . id ) ;
200
- this . renderSnippetsUI ( filter ) ;
201
- } ) ;
202
- } else {
203
- button . textContent = 'Install' ;
204
- button . className = 'mod-cta' ;
205
- button . addEventListener ( 'click' , async ( ) => {
206
- const url = `https://raw.githubusercontent.com/${ snippet . repo } /refs/heads/main/${ snippet . folder } /snippet.css` ;
207
- try {
208
- if ( await isOnline ( ) ) {
209
- const response = await fetchWithTimeout ( url ) ;
210
- if ( ! response . ok ) throw new Error ( `Network response was not ok: ${ response . statusText } ` ) ;
211
- const code = await response . text ( ) ;
212
- await this . install ( snippet . id , code ) ;
213
- this . renderSnippetsUI ( filter ) ;
214
- } else {
215
- new Notice ( `No Internet connection...` ) ;
216
- }
217
- } catch ( error ) {
218
- console . error ( error ) ;
219
- new Notice ( `Error: ${ error . message } ` ) ;
220
- }
221
- } ) ;
222
- }
223
- } ) ;
237
+ card . createDiv ( { cls : 'snippet-store-button-wrapper' } ) ;
238
+
239
+ // Now update just the button based on snippet state
240
+ this . updateSnippetCard ( snippet ) ;
224
241
} ) ;
225
242
}
226
243
0 commit comments