Skip to content

Commit 2d2f286

Browse files
committed
refactor: extract sort config into pluginSortOptions
1 parent f1b2498 commit 2d2f286

File tree

2 files changed

+56
-26
lines changed

2 files changed

+56
-26
lines changed

src/pages/plugins/index.astro

Lines changed: 24 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,10 @@ import SectionSubheader from "@/components/SectionSubheader.astro";
88
import LabelValue from "@/components/plugins/LabelValue.astro";
99
import {TAGS} from "@/constants";
1010
import featuredPluginIds from "@/data/featured-plugins.yml";
11+
import {
12+
DEFAULT_PLUGIN_SORT,
13+
PLUGIN_SORT_LIST,
14+
} from "@/pages/plugins/pluginSortOptions";
1115
import Swiper from "@/components/Swiper.astro";
1216
1317
const plugins = await getPluginsJson();
@@ -48,10 +52,11 @@ const hasFeaturedPlugins = featured.length > 0;
4852

4953
<LabelValue label="Sort by">
5054
<select aria-label="Sort by" id="sort">
51-
<option value="name-asc" selected>Name (A-Z)</option>
52-
<option value="name-desc">Name (Z-A)</option>
53-
<option value="first-release-desc">Release Date</option>
54-
<option value="latest-release-desc">Last Updated</option>
55+
{PLUGIN_SORT_LIST.map(option => (
56+
<option value={option.value} selected={option.value === DEFAULT_PLUGIN_SORT}>
57+
{option.label}
58+
</option>
59+
))}
5560
</select>
5661
</LabelValue>
5762
</div>
@@ -111,6 +116,11 @@ const hasFeaturedPlugins = featured.length > 0;
111116

112117
<script>
113118
import {TAGS} from "@/constants";
119+
import {
120+
DEFAULT_PLUGIN_SORT,
121+
isPluginSortValue,
122+
PLUGIN_SORTS,
123+
} from "@/pages/plugins/pluginSortOptions";
114124

115125
const searchInput = document.querySelector("#search") as HTMLInputElement;
116126
const tagsSelect = document.querySelector("#tags") as HTMLSelectElement;
@@ -119,14 +129,6 @@ const notFound = document.querySelector("#not-found") as HTMLElement;
119129
const store = document.querySelector("#store") as HTMLElement;
120130

121131
const plugins = document.querySelectorAll("#store > .plugin") as NodeListOf<HTMLElement>;
122-
const defaultSortValue = "name-asc";
123-
const allowedSortValues = [
124-
"name-asc",
125-
"name-desc",
126-
"latest-release-desc",
127-
"first-release-desc",
128-
];
129-
130132
searchInput.addEventListener("input", onFilterChange);
131133
tagsSelect.addEventListener("change", onFilterChange);
132134
sortSelect.addEventListener("change", onSortChange);
@@ -135,14 +137,14 @@ const url = new URL(window.location.href);
135137

136138
const searchParam = url.searchParams.get("search") ?? "";
137139
let tagsParam: any = url.searchParams.get("tags") ?? "";
138-
let sortParam = url.searchParams.get("sort") ?? defaultSortValue;
140+
let sortParam = url.searchParams.get("sort") ?? DEFAULT_PLUGIN_SORT;
139141

140142
if (!TAGS.includes(tagsParam)) {
141143
tagsParam = "";
142144
}
143145

144-
if (!allowedSortValues.includes(sortParam)) {
145-
sortParam = defaultSortValue;
146+
if (!isPluginSortValue(sortParam)) {
147+
sortParam = DEFAULT_PLUGIN_SORT;
146148
}
147149

148150
searchInput.value = searchParam;
@@ -182,11 +184,7 @@ function updateUrlParams(): void {
182184
url.searchParams.delete("tags");
183185
}
184186

185-
if (allowedSortValues.includes(sortValue)) {
186-
url.searchParams.set("sort", sortValue);
187-
} else {
188-
url.searchParams.delete("sort");
189-
}
187+
url.searchParams.set("sort", sortValue);
190188

191189
history.replaceState(null, "", url);
192190
}
@@ -232,31 +230,31 @@ function getDateTimestamp(plugin: HTMLElement, attributeName: string): number {
232230
function applySort(): void {
233231
const sortValue = sortSelect.value;
234232

235-
if (!allowedSortValues.includes(sortValue)) {
233+
if (!isPluginSortValue(sortValue)) {
236234
return;
237235
}
238236

239237
const sortedPlugins = Array.from(plugins).sort((a, b) => {
240238
switch (sortValue) {
241-
case "latest-release-desc": {
239+
case PLUGIN_SORTS.LATEST_RELEASE_DESC.value: {
242240
const latestA = getDateTimestamp(a, "data-latest-release-date");
243241
const latestB = getDateTimestamp(b, "data-latest-release-date");
244242
return latestB - latestA;
245243
}
246244

247-
case "first-release-desc": {
245+
case PLUGIN_SORTS.FIRST_RELEASE_DESC.value: {
248246
const firstA = getDateTimestamp(a, "data-first-release-date");
249247
const firstB = getDateTimestamp(b, "data-first-release-date");
250248
return firstB - firstA;
251249
}
252250

253-
case "name-desc": {
251+
case PLUGIN_SORTS.NAME_DESC.value: {
254252
const titleA = a.querySelector(".name")!.textContent!.trim().toLowerCase();
255253
const titleB = b.querySelector(".name")!.textContent!.trim().toLowerCase();
256254
return titleB.localeCompare(titleA);
257255
}
258256

259-
case "name-asc":
257+
case PLUGIN_SORTS.NAME_ASC.value:
260258
default: {
261259
const titleA = a.querySelector(".name")!.textContent!.trim().toLowerCase();
262260
const titleB = b.querySelector(".name")!.textContent!.trim().toLowerCase();
@@ -265,7 +263,7 @@ function applySort(): void {
265263
}
266264
});
267265

268-
// Re-appending existing plugin nodes moves them into sorted order (no duplicates)
266+
// Re-appending existing plugin nodes moves them into sorted order (no duplicates)
269267
store.append(...sortedPlugins);
270268
}
271269
</script>
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
export const PLUGIN_SORTS = {
2+
NAME_ASC: {
3+
value: "name-asc",
4+
label: "Name (A-Z)",
5+
},
6+
NAME_DESC: {
7+
value: "name-desc",
8+
label: "Name (Z-A)",
9+
},
10+
FIRST_RELEASE_DESC: {
11+
value: "first-release-desc",
12+
label: "Release Date",
13+
},
14+
LATEST_RELEASE_DESC: {
15+
value: "latest-release-desc",
16+
label: "Last Updated",
17+
},
18+
} as const;
19+
20+
export const PLUGIN_SORT_LIST = Object.values(PLUGIN_SORTS);
21+
22+
export type PluginSortValue = typeof PLUGIN_SORTS[keyof typeof PLUGIN_SORTS]["value"];
23+
24+
export const DEFAULT_PLUGIN_SORT: PluginSortValue = PLUGIN_SORTS.NAME_ASC.value;
25+
26+
const PLUGIN_SORT_VALUE_SET = new Set<string>(
27+
PLUGIN_SORT_LIST.map(option => option.value),
28+
);
29+
30+
export function isPluginSortValue(value: string): value is PluginSortValue {
31+
return PLUGIN_SORT_VALUE_SET.has(value);
32+
}

0 commit comments

Comments
 (0)