Skip to content

Commit 92e6590

Browse files
authored
Filter entries by language type in onefetch.dev (#1227)
* init * format * improve styling * add help text * add break line * fix help text * review * rename variable * select all types by default * styling * show filter by default * fix css
1 parent 43e0c69 commit 92e6590

File tree

2 files changed

+71
-25
lines changed

2 files changed

+71
-25
lines changed

docs/vercel/src/Index.svelte

+69-25
Original file line numberDiff line numberDiff line change
@@ -1,41 +1,51 @@
11
<script lang="ts">
22
import AsciiPreview from './lib/AsciiPreview.svelte';
33
import data from '../../../languages.yaml';
4-
import type { Languages } from '../../../languages.yaml';
4+
import type { Languages, Language } from '../../../languages.yaml';
55
import { onMount } from 'svelte';
6+
import { writable, derived } from 'svelte/store';
67
7-
let tag_name: string;
8-
let html_url: string;
8+
let tagName: string;
9+
let htmlUrl: string;
910
10-
const languages = Object.entries(data as Languages).map(
11-
([name, { ascii, colors }]) => ({
11+
const languages: Language[] = Object.entries(data as Languages).map(
12+
([name, { type, ascii, colors }]) => ({
1213
name,
14+
type,
1315
ascii,
14-
...colors,
16+
colors,
1517
})
1618
);
1719
20+
const languageTypes: string[] = Array.from(
21+
new Set<string>(Object.values(data as Languages).map(({ type }) => type))
22+
);
23+
24+
const filter = writable({
25+
checkboxes: languageTypes,
26+
});
27+
28+
const filteredLanguages = derived(filter, ($filter) => {
29+
return languages.filter(({ type }) => $filter.checkboxes.includes(type));
30+
});
31+
1832
onMount(async () => {
19-
try {
20-
const response = await fetch(
21-
'https://api.github.com/repos/o2sh/onefetch/releases/latest'
22-
);
23-
const data = await response.json();
24-
25-
tag_name = data.tag_name;
26-
html_url = data.html_url;
27-
} catch (error) {
28-
console.error('Error:', error);
29-
}
33+
const response = await fetch(
34+
'https://api.github.com/repos/o2sh/onefetch/releases/latest'
35+
);
36+
const data = await response.json();
37+
38+
tagName = data.tag_name;
39+
htmlUrl = data.html_url;
3040
});
3141
</script>
3242

3343
<header>
34-
{#if tag_name && html_url}
44+
{#if tagName && htmlUrl}
3545
<div class="banner">
3646
<small
37-
>Version {tag_name} is available 🎉 Check the
38-
<a href={html_url}>release notes</a>!!</small>
47+
>Version {tagName} is available 🎉 Check the
48+
<a href={htmlUrl}>release notes</a>!!</small>
3949
</div>
4050
{/if}
4151
<h1>Onefetch</h1>
@@ -61,14 +71,36 @@
6171
Suggestions and PRs are welcome at <a
6272
href="https://github.com/o2sh/onefetch">github.com/o2sh/onefetch</a>
6373
</p>
64-
<h3>Languages <small>({languages.length})</small></h3>
65-
{#each languages as language}
74+
75+
<h3>Languages <small>({$filteredLanguages.length})</small></h3>
76+
77+
<strong>Filter by type</strong>
78+
79+
<div class="checkbox-group">
80+
{#each languageTypes as type}
81+
<label for={type}>
82+
<input
83+
id={type}
84+
type="checkbox"
85+
value={type}
86+
bind:group={$filter.checkboxes} />
87+
{type}
88+
</label>
89+
{/each}
90+
</div>
91+
92+
<small
93+
>Note: By default, onefetch will only recognize <strong>programming</strong>
94+
and <strong>markup</strong> types. Use the
95+
<code>--type</code> flag to configure.</small>
96+
97+
{#each $filteredLanguages as language}
6698
<AsciiPreview
6799
name={language.name}
68-
ansi={language.ansi}
69-
hex={language.hex}
100+
ansi={language.colors.ansi}
101+
hex={language.colors.hex}
70102
ascii={language.ascii}
71-
chip={language.chip} />
103+
chip={language.colors.chip} />
72104
{/each}
73105
</main>
74106

@@ -82,4 +114,16 @@
82114
text-align: center;
83115
padding: 0.5rem 0;
84116
}
117+
118+
.checkbox-group {
119+
margin-top: 1.5rem;
120+
}
121+
122+
.checkbox-group label {
123+
width: fit-content;
124+
}
125+
126+
.checkbox-group label {
127+
text-transform: capitalize;
128+
}
85129
</style>

docs/vercel/typings/yaml.d.ts

+2
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,12 @@ declare module '*/languages.yaml' {
66
}
77

88
export interface Language {
9+
name: string;
910
type: string;
1011
ascii: string;
1112
colors: LanguageColors;
1213
}
1314

1415
export type Languages = Record<string, Language>;
16+
export type Language = Language;
1517
}

0 commit comments

Comments
 (0)