Skip to content

Commit 54374b0

Browse files
committed
refactor: extract sort config into pluginSortOptions
1 parent a65e265 commit 54374b0

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>
@@ -112,6 +117,11 @@ const hasFeaturedPlugins = featured.length > 0;
112117

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

116126
const searchInput = document.querySelector("#search") as HTMLInputElement;
117127
const tagsSelect = document.querySelector("#tags") as HTMLSelectElement;
@@ -120,14 +130,6 @@ const notFound = document.querySelector("#not-found") as HTMLElement;
120130
const store = document.querySelector("#store") as HTMLElement;
121131

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

137139
const searchParam = url.searchParams.get("search") ?? "";
138140
let tagsParam: any = url.searchParams.get("tags") ?? "";
139-
let sortParam = url.searchParams.get("sort") ?? defaultSortValue;
141+
let sortParam = url.searchParams.get("sort") ?? DEFAULT_PLUGIN_SORT;
140142

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

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

149151
searchInput.value = searchParam;
@@ -183,11 +185,7 @@ function updateUrlParams(): void {
183185
url.searchParams.delete("tags");
184186
}
185187

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

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

236-
if (!allowedSortValues.includes(sortValue)) {
234+
if (!isPluginSortValue(sortValue)) {
237235
return;
238236
}
239237

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

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

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

260-
case "name-asc":
258+
case PLUGIN_SORTS.NAME_ASC.value:
261259
default: {
262260
const titleA = a.querySelector(".name")!.textContent!.trim().toLowerCase();
263261
const titleB = b.querySelector(".name")!.textContent!.trim().toLowerCase();
@@ -266,7 +264,7 @@ function applySort(): void {
266264
}
267265
});
268266

269-
// Re-appending existing plugin nodes moves them into sorted order (no duplicates)
267+
// Re-appending existing plugin nodes moves them into sorted order (no duplicates)
270268
store.append(...sortedPlugins);
271269
}
272270
</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)