From 3b7f30908cc171d7123010b0b122f9098adb291e Mon Sep 17 00:00:00 2001 From: Christian Beutel <> Date: Sun, 7 Sep 2025 17:59:35 +0200 Subject: [PATCH] fixes sorting by difficulty --- db/util/meilisearch.go | 15 ++++++++++++++- .../trail/trail_filter_panel.svelte | 6 +++--- web/src/lib/models/trail.ts | 4 ++-- web/src/lib/stores/trail_store.ts | 2 +- web/src/routes/map/+page.ts | 2 +- .../routes/profile/[handle]/trails/+page.ts | 2 +- web/src/routes/trails/+page.svelte | 19 ++++++++++++++++++- web/src/routes/trails/+page.ts | 2 +- 8 files changed, 41 insertions(+), 11 deletions(-) diff --git a/db/util/meilisearch.go b/db/util/meilisearch.go index a986909d..98a122a0 100644 --- a/db/util/meilisearch.go +++ b/db/util/meilisearch.go @@ -69,7 +69,7 @@ func documentFromTrailRecord(app core.App, r *core.Record, author *core.Record, "elevation_gain": r.GetFloat("elevation_gain"), "elevation_loss": r.GetFloat("elevation_loss"), "duration": r.GetFloat("duration"), - "difficulty": r.Get("difficulty"), + "difficulty": difficultyToNumber(r.GetString("difficulty")), "category": category, "completed": logCount > 0, "date": r.GetDateTime("date").Time().Unix(), @@ -97,6 +97,19 @@ func documentFromTrailRecord(app core.App, r *core.Record, author *core.Record, return document, nil } +func difficultyToNumber(difficulty string) int32 { + switch difficulty { + case "easy": + return 0 + case "moderate": + return 1 + case "difficult": + return 2 + } + + return 0 +} + func getPolyline(app core.App, r *core.Record) (string, error) { gpxPath := r.GetString("gpx") if len(gpxPath) == 0 { diff --git a/web/src/lib/components/trail/trail_filter_panel.svelte b/web/src/lib/components/trail/trail_filter_panel.svelte index 446bfc35..7979edfd 100644 --- a/web/src/lib/components/trail/trail_filter_panel.svelte +++ b/web/src/lib/components/trail/trail_filter_panel.svelte @@ -51,9 +51,9 @@ ]; const difficultyItems: SelectItem[] = [ - { text: $_("easy"), value: "easy" }, - { text: $_("moderate"), value: "moderate" }, - { text: $_("difficult"), value: "difficult" }, + { text: $_("easy"), value: 0 }, + { text: $_("moderate"), value: 1 }, + { text: $_("difficult"), value: 2 }, ]; let searchDropdownItems: SearchItem[] = $state([]); diff --git a/web/src/lib/models/trail.ts b/web/src/lib/models/trail.ts index 566c861b..e5946d93 100644 --- a/web/src/lib/models/trail.ts +++ b/web/src/lib/models/trail.ts @@ -111,7 +111,7 @@ interface TrailFilter { q: string, category: string[], tags: string[], - difficulty: ("easy" | "moderate" | "difficult")[] + difficulty: (0 | 1 | 2)[] author?: string; public?: boolean; shared?: boolean; @@ -169,7 +169,7 @@ interface TrailSearchResult { elevation_gain: number; elevation_loss: number; duration: number; - difficulty: "easy" | "moderate" | "difficult"; + difficulty: 0 | 1 | 2; category: string; completed: boolean; date: number; diff --git a/web/src/lib/stores/trail_store.ts b/web/src/lib/stores/trail_store.ts index 2f60d5f7..c6f90a37 100644 --- a/web/src/lib/stores/trail_store.ts +++ b/web/src/lib/stores/trail_store.ts @@ -457,7 +457,7 @@ export async function searchResultToTrailList(hits: Hits): Pr created: new Date(h.created * 1000).toISOString(), date: new Date(h.date * 1000).toISOString(), description: h.description, - difficulty: h.difficulty, + difficulty: h.difficulty == 0 ? "easy" : h.difficulty == 1 ? "moderate" : "difficult", distance: h.distance, duration: h.duration, elevation_gain: h.elevation_gain, diff --git a/web/src/routes/map/+page.ts b/web/src/routes/map/+page.ts index a5b5c7e4..aa53828a 100644 --- a/web/src/routes/map/+page.ts +++ b/web/src/routes/map/+page.ts @@ -11,7 +11,7 @@ export const load: ServerLoad = async ({ params, locals, fetch }) => { q: "", category: [], tags: [], - difficulty: ["easy", "moderate", "difficult"], + difficulty: [0, 1, 2], author: "", public: true, shared: true, diff --git a/web/src/routes/profile/[handle]/trails/+page.ts b/web/src/routes/profile/[handle]/trails/+page.ts index fead8afe..07adeb7a 100644 --- a/web/src/routes/profile/[handle]/trails/+page.ts +++ b/web/src/routes/profile/[handle]/trails/+page.ts @@ -12,7 +12,7 @@ export const load: Load = async ({ params, fetch, parent }) => { q: "", category: [], tags: [], - difficulty: ["easy", "moderate", "difficult"], + difficulty: [0, 1, 2], author: actor.id, public: true, shared: true, diff --git a/web/src/routes/trails/+page.svelte b/web/src/routes/trails/+page.svelte index feef0328..09bd7e55 100644 --- a/web/src/routes/trails/+page.svelte +++ b/web/src/routes/trails/+page.svelte @@ -25,7 +25,24 @@ export const snapshot: Snapshot = { capture: () => filter, restore: (value) => { - filter = value; + const difficultyMap: Record = { + easy: 0, + moderate: 1, + difficult: 2, + }; + // defensive copy + const migrated = { ...value }; + + if (Array.isArray(migrated.difficulty)) { + migrated.difficulty = migrated.difficulty.map((d: any) => { + if (typeof d === "string" && d in difficultyMap) { + return difficultyMap[d]; + } + return d; + }); + } + + filter = migrated; handleFilterUpdate(); }, }; diff --git a/web/src/routes/trails/+page.ts b/web/src/routes/trails/+page.ts index e9f8e12c..97e1d269 100644 --- a/web/src/routes/trails/+page.ts +++ b/web/src/routes/trails/+page.ts @@ -10,7 +10,7 @@ export const load: ServerLoad = async ({ params, locals, url, fetch }) => { q: "", category: [], tags: [], - difficulty: ["easy", "moderate", "difficult"], + difficulty: [0, 1, 2], author: "", public: true, shared: true,