@@ -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 >
@@ -111,6 +116,11 @@ const hasFeaturedPlugins = featured.length > 0;
111116
112117<script >
113118import {TAGS} from "@/constants";
119+ import {
120+ DEFAULT_PLUGIN_SORT,
121+ isPluginSortValue,
122+ PLUGIN_SORTS,
123+ } from "@/pages/plugins/pluginSortOptions";
114124
115125const searchInput = document.querySelector("#search") as HTMLInputElement;
116126const tagsSelect = document.querySelector("#tags") as HTMLSelectElement;
@@ -119,14 +129,6 @@ const notFound = document.querySelector("#not-found") as HTMLElement;
119129const store = document.querySelector("#store") as HTMLElement;
120130
121131const 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-
130132searchInput.addEventListener("input", onFilterChange);
131133tagsSelect.addEventListener("change", onFilterChange);
132134sortSelect.addEventListener("change", onSortChange);
@@ -135,14 +137,14 @@ const url = new URL(window.location.href);
135137
136138const searchParam = url.searchParams.get("search") ?? "";
137139let 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
140142if (!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
148150searchInput.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 {
232230function 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 >
0 commit comments