Skip to content
This repository was archived by the owner on Jun 26, 2025. It is now read-only.

Commit 7a41e18

Browse files
committed
feat: enhance snippet management UI with dynamic install/delete buttons
1 parent 26ba74b commit 7a41e18

File tree

1 file changed

+50
-33
lines changed

1 file changed

+50
-33
lines changed

main.ts

Lines changed: 50 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -153,6 +153,51 @@ class CssSnippetStoreModal extends Modal {
153153
return await vault.adapter.exists(fullPath);
154154
}
155155

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+
156201
private renderSnippetsUI(filter: string = "") {
157202
const { contentEl } = this;
158203
const grid = contentEl.querySelector('.community-items-container') as HTMLDivElement;
@@ -183,44 +228,16 @@ class CssSnippetStoreModal extends Modal {
183228

184229
filteredSnippets.forEach(snippet => {
185230
const card = grid.createDiv({ cls: 'community-item' });
231+
card.setAttr("data-snippet-id", snippet.id);
186232

187233
card.createEl('div', { text: snippet.name, cls: 'community-item-name' });
188234
card.createEl('div', { text: `By ${snippet.author}`, cls: 'community-item-author' });
189235
card.createEl('div', { text: snippet.description, cls: 'community-desc' });
190236

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);
224241
});
225242
}
226243

0 commit comments

Comments
 (0)