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

Commit e61b454

Browse files
committed
feat: refactor snippet rendering
1 parent f2ca571 commit e61b454

File tree

1 file changed

+78
-77
lines changed

1 file changed

+78
-77
lines changed

main.ts

Lines changed: 78 additions & 77 deletions
Original file line numberDiff line numberDiff line change
@@ -176,6 +176,78 @@ class CssSnippetStoreModal extends Modal {
176176
return await vault.adapter.exists(fullPath);
177177
}
178178

179+
private renderSnippetsUI(filter: string = "") {
180+
const { contentEl } = this;
181+
const grid = contentEl.querySelector('.community-items-container') as HTMLDivElement;
182+
const messageEl = contentEl.querySelector('.snippet-status-message') as HTMLDivElement;
183+
184+
if (!grid || !messageEl) return;
185+
186+
grid.empty();
187+
messageEl.empty();
188+
189+
const lowerFilter = filter.toLowerCase();
190+
191+
const filteredSnippets = this.snippets.filter(snippet =>
192+
!filter ||
193+
snippet.name.toLowerCase().includes(lowerFilter) ||
194+
snippet.author.toLowerCase().includes(lowerFilter) ||
195+
snippet.description.toLowerCase().includes(lowerFilter)
196+
);
197+
198+
if (filteredSnippets.length === 0) {
199+
messageEl.setText(
200+
this.snippets.length === 0
201+
? "No Internet connection"
202+
: "No snippets match your search."
203+
);
204+
return;
205+
}
206+
207+
filteredSnippets.forEach(snippet => {
208+
const card = grid.createDiv({ cls: 'community-item' });
209+
210+
card.createEl('div', { text: snippet.name, cls: 'community-item-name' });
211+
card.createEl('div', { text: `By ${snippet.author}`, cls: 'community-item-author' });
212+
card.createEl('div', { text: snippet.description, cls: 'community-desc' });
213+
214+
const buttonWrapper = card.createEl('div', { cls: 'snippet-store-button-wrapper' });
215+
const button = buttonWrapper.createEl('button', { cls: 'mod-cta' });
216+
217+
this.checkSnippetExists(snippet.id).then((exists) => {
218+
if (exists) {
219+
button.textContent = 'Delete';
220+
button.className = 'mod-danger';
221+
button.addEventListener('click', async () => {
222+
await this.uninstall(snippet.id);
223+
this.renderSnippetsUI(filter);
224+
});
225+
} else {
226+
button.textContent = 'Install';
227+
button.className = 'mod-cta';
228+
button.addEventListener('click', async () => {
229+
const url = `https://raw.githubusercontent.com/${snippet.repo}/refs/heads/main/${snippet.folder}/snippet.css`;
230+
try {
231+
if (await isOnline()) {
232+
const response = await fetchWithTimeout(url);
233+
if (!response.ok) throw new Error(`Network response was not ok: ${response.statusText}`);
234+
const code = await response.text();
235+
await this.install(snippet.id, code);
236+
this.renderSnippetsUI(filter);
237+
} else {
238+
new Notice(`No Internet connection...`);
239+
}
240+
} catch (error) {
241+
console.error(error);
242+
new Notice(`Error: ${error.message}`);
243+
}
244+
});
245+
}
246+
});
247+
});
248+
}
249+
250+
179251
onOpen() {
180252
const { contentEl } = this;
181253
contentEl.addClass('snippet-store-modal');
@@ -186,11 +258,9 @@ class CssSnippetStoreModal extends Modal {
186258

187259
contentEl.createEl('h1', { text: 'CSS Snippet Store' });
188260

189-
// Wrapper for search + status message
190261
const topContainer = contentEl.createDiv();
191262
topContainer.style.marginBottom = '1rem';
192263

193-
// Search bar
194264
const searchInput = topContainer.createEl('input', {
195265
type: 'text',
196266
placeholder: 'Search snippets...',
@@ -202,95 +272,26 @@ class CssSnippetStoreModal extends Modal {
202272

203273
// Message container
204274
const messageEl = topContainer.createEl('div');
275+
messageEl.classList.add('snippet-status-message');
205276
messageEl.style.marginTop = '0.5rem';
206277
messageEl.style.textAlign = 'center';
207278
messageEl.style.color = 'var(--text-muted)';
208279
messageEl.style.fontStyle = 'italic';
209280

281+
// Snippet container
210282
const grid = contentEl.createEl('div', { cls: 'community-items-container' });
211283

212-
// Render Function
213-
const renderSnippets = (filter: string = "") => {
214-
grid.empty();
215-
messageEl.empty();
216-
217-
const lowerFilter = filter.toLowerCase();
218-
219-
const filteredSnippets = this.snippets.filter(snippet =>
220-
!filter ||
221-
snippet.name.toLowerCase().includes(lowerFilter) ||
222-
snippet.author.toLowerCase().includes(lowerFilter) ||
223-
snippet.description.toLowerCase().includes(lowerFilter)
224-
);
225-
226-
if (filteredSnippets.length === 0) {
227-
messageEl.setText(
228-
this.snippets.length === 0
229-
? "No Internet connection"
230-
: "No snippets match your search."
231-
);
232-
return;
233-
}
234-
235-
filteredSnippets.forEach(snippet => {
236-
const card = grid.createDiv({ cls: 'community-item' });
237-
238-
card.createEl('div', { text: snippet.name, cls: 'community-item-name' });
239-
card.createEl('div', { text: `By ${snippet.author}`, cls: 'community-item-author' });
240-
card.createEl('div', { text: snippet.description, cls: 'community-desc' });
241-
242-
const buttonWrapper = card.createEl('div', { cls: 'snippet-store-button-wrapper' });
243-
const button = buttonWrapper.createEl('button', { cls: 'mod-cta' });
244-
245-
this.checkSnippetExists(snippet.id).then((exists) => {
246-
if (exists) {
247-
button.textContent = 'Delete';
248-
button.className = 'mod-danger';
249-
250-
button.addEventListener('click', async () => {
251-
await this.uninstall(snippet.id);
252-
this.close();
253-
this.open();
254-
});
255-
} else {
256-
button.textContent = 'Install';
257-
button.className = 'mod-cta';
258-
259-
button.addEventListener('click', async () => {
260-
const url = `https://raw.githubusercontent.com/${snippet.repo}/refs/heads/main/${snippet.folder}/snippet.css`;
261-
try {
262-
if (await isOnline()) {
263-
const response = await fetchWithTimeout(url);
264-
if (!response.ok) {
265-
throw new Error(`Network response was not ok: ${response.statusText}`);
266-
}
267-
const code = await response.text();
268-
await this.install(snippet.id, code);
269-
this.close();
270-
this.open();
271-
} else {
272-
new Notice(`No Internet connection...`);
273-
}
274-
} catch (error) {
275-
console.error(error);
276-
new Notice(`Error: ${error.message}`);
277-
}
278-
});
279-
}
280-
});
281-
});
282-
};
283-
284284
// Initial rendering
285-
renderSnippets();
285+
this.renderSnippetsUI();
286286

287-
// Attach event listener to search input
287+
// Live search
288288
searchInput.addEventListener('input', () => {
289289
const value = searchInput.value.trim();
290-
renderSnippets(value);
290+
this.renderSnippetsUI(value);
291291
});
292292
}
293293

294+
294295
onClose() {
295296
const { contentEl } = this;
296297
contentEl.empty();

0 commit comments

Comments
 (0)