@@ -8,6 +8,10 @@ import SectionSubheader from "@/components/SectionSubheader.astro";
88import LabelValue from " @/components/plugins/LabelValue.astro" ;
99import {TAGS } from " @/constants" ;
1010import featuredPluginIds from " @/data/featured-plugins.yml" ;
11+ import {
12+ DEFAULT_PLUGIN_SORT ,
13+ PLUGIN_SORT_LIST ,
14+ } from " @/pages/plugins/pluginSortOptions" ;
1115import Swiper from " @/components/Swiper.astro" ;
1216
1317const 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 >
114119import {TAGS} from "@/constants";
120+ import {
121+ DEFAULT_PLUGIN_SORT,
122+ isPluginSortValue,
123+ PLUGIN_SORTS,
124+ } from "@/pages/plugins/pluginSortOptions";
115125
116126const searchInput = document.querySelector("#search") as HTMLInputElement;
117127const tagsSelect = document.querySelector("#tags") as HTMLSelectElement;
@@ -120,14 +130,6 @@ const notFound = document.querySelector("#not-found") as HTMLElement;
120130const store = document.querySelector("#store") as HTMLElement;
121131
122132const 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-
131133searchInput.addEventListener("input", onFilterChange);
132134tagsSelect.addEventListener("change", onFilterChange);
133135sortSelect.addEventListener("change", onSortChange);
@@ -136,14 +138,14 @@ const url = new URL(window.location.href);
136138
137139const searchParam = url.searchParams.get("search") ?? "";
138140let 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
141143if (!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
149151searchInput.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 {
233231function 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 >
0 commit comments