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

Commit 5564e5a

Browse files
committed
feat: improve snippet filtering with no results message and online check
1 parent bd8692b commit 5564e5a

File tree

1 file changed

+67
-55
lines changed

1 file changed

+67
-55
lines changed

main.ts

Lines changed: 67 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -202,66 +202,78 @@ class CssSnippetStoreModal extends Modal {
202202
grid.empty();
203203
const lowerFilter = filter.toLowerCase();
204204

205-
this.snippets
206-
.filter(snippet =>
207-
!filter ||
208-
snippet.name.toLowerCase().includes(lowerFilter) ||
209-
snippet.author.toLowerCase().includes(lowerFilter) ||
210-
snippet.description.toLowerCase().includes(lowerFilter)
211-
)
212-
.forEach(snippet => {
213-
const card = grid.createDiv({ cls: 'community-item' });
214-
215-
card.createEl('div', { text: snippet.name, cls: 'community-item-name' });
216-
card.createEl('div', { text: `By ${snippet.author}`, cls: 'community-item-author' });
217-
card.createEl('div', { text: snippet.description, cls: 'community-desc' });
218-
219-
const buttonWrapper = card.createEl('div', { cls: 'snippet-store-button-wrapper' });
220-
const button = buttonWrapper.createEl('button', { cls: 'mod-cta' });
221-
222-
this.checkSnippetExists(snippet.id).then((exists) => {
223-
if (exists) {
224-
button.textContent = 'Delete';
225-
button.className = 'mod-danger';
226-
227-
button.addEventListener('click', async () => {
228-
await this.uninstall(snippet.id);
229-
this.close();
230-
this.open();
231-
});
232-
} else {
233-
button.textContent = 'Install';
234-
button.className = 'mod-cta';
235-
236-
button.addEventListener('click', async () => {
237-
const url = `https://raw.githubusercontent.com/${snippet.repo}/refs/heads/main/${snippet.folder}/snippet.css`;
238-
try {
239-
if (await isOnline()) {
240-
const response = await fetchWithTimeout(url);
241-
if (!response.ok) {
242-
throw new Error(`Network response was not ok: ${response.statusText}`);
243-
}
244-
/*
245-
if (!response.headers.get('content-type')?.includes('text/css')) {
246-
throw new Error("Expected CSS content");
247-
}*/
248-
const code = await response.text();
249-
await this.install(snippet.id, code);
250-
this.close();
251-
this.open();
252-
} else {
253-
new Notice(`No Internet connection...`);
205+
const filteredSnippets = this.snippets.filter(snippet =>
206+
!filter ||
207+
snippet.name.toLowerCase().includes(lowerFilter) ||
208+
snippet.author.toLowerCase().includes(lowerFilter) ||
209+
snippet.description.toLowerCase().includes(lowerFilter)
210+
);
211+
212+
if (filteredSnippets.length === 0) {
213+
const noResultsMessage = grid.createEl('div', {
214+
text: this.snippets.length === 0
215+
? "No Internet connection"
216+
: "No snippets match your search.",
217+
cls: 'no-snippets-message'
218+
});
219+
noResultsMessage.style.marginTop = '1rem';
220+
noResultsMessage.style.textAlign = 'center';
221+
noResultsMessage.style.color = 'var(--text-muted)';
222+
noResultsMessage.style.fontStyle = 'italic';
223+
noResultsMessage.style.width = '100%';
224+
return;
225+
}
226+
227+
filteredSnippets.forEach(snippet => {
228+
const card = grid.createDiv({ cls: 'community-item' });
229+
230+
card.createEl('div', { text: snippet.name, cls: 'community-item-name' });
231+
card.createEl('div', { text: `By ${snippet.author}`, cls: 'community-item-author' });
232+
card.createEl('div', { text: snippet.description, cls: 'community-desc' });
233+
234+
const buttonWrapper = card.createEl('div', { cls: 'snippet-store-button-wrapper' });
235+
const button = buttonWrapper.createEl('button', { cls: 'mod-cta' });
236+
237+
this.checkSnippetExists(snippet.id).then((exists) => {
238+
if (exists) {
239+
button.textContent = 'Delete';
240+
button.className = 'mod-danger';
241+
242+
button.addEventListener('click', async () => {
243+
await this.uninstall(snippet.id);
244+
this.close();
245+
this.open();
246+
});
247+
} else {
248+
button.textContent = 'Install';
249+
button.className = 'mod-cta';
250+
251+
button.addEventListener('click', async () => {
252+
const url = `https://raw.githubusercontent.com/${snippet.repo}/refs/heads/main/${snippet.folder}/snippet.css`;
253+
try {
254+
if (await isOnline()) {
255+
const response = await fetchWithTimeout(url);
256+
if (!response.ok) {
257+
throw new Error(`Network response was not ok: ${response.statusText}`);
254258
}
255-
} catch (error) {
256-
console.error(error);
257-
new Notice(`Error: ${error.message}`);
259+
const code = await response.text();
260+
await this.install(snippet.id, code);
261+
this.close();
262+
this.open();
263+
} else {
264+
new Notice(`No Internet connection...`);
258265
}
259-
});
260-
}
261-
});
266+
} catch (error) {
267+
console.error(error);
268+
new Notice(`Error: ${error.message}`);
269+
}
270+
});
271+
}
262272
});
273+
});
263274
};
264275

276+
265277
// Initial rendering
266278
renderSnippets();
267279

0 commit comments

Comments
 (0)