From f38926c4a38babce5e41b188913719b643d18a31 Mon Sep 17 00:00:00 2001
From: imperosol <thgirod@hotmail.com>
Date: Fri, 20 Jun 2025 16:22:15 +0200
Subject: [PATCH] fix: user pictures ordering

---
 package-lock.json                             |  2 +-
 package.json                                  |  1 +
 sas/static/bundled/sas/user/pictures-index.ts | 35 +++++++++++--------
 sas/templates/sas/macros.jinja                |  2 +-
 sas/templates/sas/user_pictures.jinja         |  6 ++--
 tsconfig.json                                 |  2 +-
 6 files changed, 28 insertions(+), 20 deletions(-)

diff --git a/package-lock.json b/package-lock.json
index 31bddd32..5ab0eeb9 100644
--- a/package-lock.json
+++ b/package-lock.json
@@ -48,6 +48,7 @@
         "@types/cytoscape-cxtmenu": "^3.4.4",
         "@types/cytoscape-klay": "^3.1.4",
         "@types/jquery": "^3.5.31",
+        "typescript": "^5.8.3",
         "vite": "^6.2.5",
         "vite-bundle-visualizer": "^1.2.1",
         "vite-plugin-static-copy": "^3.0.2"
@@ -5587,7 +5588,6 @@
       "integrity": "sha512-p1diW6TqL9L07nNxvRMM7hMMw4c5XOo/1ibL4aAIGmSAt9slTE1Xgw5KWuof2uTOvCg9BY7ZRi+GaF+7sfgPeQ==",
       "dev": true,
       "license": "Apache-2.0",
-      "peer": true,
       "bin": {
         "tsc": "bin/tsc",
         "tsserver": "bin/tsserver"
diff --git a/package.json b/package.json
index b1b9f442..3abaca2b 100644
--- a/package.json
+++ b/package.json
@@ -33,6 +33,7 @@
     "@types/jquery": "^3.5.31",
     "@types/cytoscape-cxtmenu": "^3.4.4",
     "@types/cytoscape-klay": "^3.1.4",
+    "typescript": "^5.8.3",
     "vite": "^6.2.5",
     "vite-bundle-visualizer": "^1.2.1",
     "vite-plugin-static-copy": "^3.0.2"
diff --git a/sas/static/bundled/sas/user/pictures-index.ts b/sas/static/bundled/sas/user/pictures-index.ts
index f5f2fcbc..0a77159b 100644
--- a/sas/static/bundled/sas/user/pictures-index.ts
+++ b/sas/static/bundled/sas/user/pictures-index.ts
@@ -9,28 +9,35 @@ interface PagePictureConfig {
   userId: number;
 }
 
+interface Album {
+  id: number;
+  name: string;
+  pictures: PictureSchema[];
+}
+
 document.addEventListener("alpine:init", () => {
   Alpine.data("user_pictures", (config: PagePictureConfig) => ({
     loading: true,
-    pictures: [] as PictureSchema[],
-    albums: {} as Record<string, PictureSchema[]>,
+    albums: [] as Album[],
 
     async init() {
-      this.pictures = await paginated(picturesFetchPictures, {
+      const pictures = await paginated(picturesFetchPictures, {
         // biome-ignore lint/style/useNamingConvention: from python api
         query: { users_identified: [config.userId] },
       } as PicturesFetchPicturesData);
-
-      this.albums = this.pictures.reduce(
-        (acc: Record<number, PictureSchema[]>, picture: PictureSchema) => {
-          if (!acc[picture.album.id]) {
-            acc[picture.album.id] = [];
-          }
-          acc[picture.album.id].push(picture);
-          return acc;
-        },
-        {},
-      );
+      const groupedAlbums = Object.groupBy(pictures, (i: PictureSchema) => i.album.id);
+      this.albums = Object.values(groupedAlbums).map((pictures: PictureSchema[]) => {
+        return {
+          id: pictures[0].album.id,
+          name: pictures[0].album.name,
+          pictures: pictures,
+        };
+      });
+      this.albums.sort((a: Album, b: Album) => b.id - a.id);
+      const hash = document.location.hash.replace("#", "");
+      if (hash.startsWith("album-")) {
+        this.$nextTick(() => document.getElementById(hash)?.scrollIntoView()).then();
+      }
       this.loading = false;
     },
   }));
diff --git a/sas/templates/sas/macros.jinja b/sas/templates/sas/macros.jinja
index aa4afa48..a00c2b6c 100644
--- a/sas/templates/sas/macros.jinja
+++ b/sas/templates/sas/macros.jinja
@@ -50,7 +50,7 @@
  #}
 {% macro download_button(name) %}
   <div x-data="pictures_download">
-    <div x-show="pictures.length > 0" x-cloak>
+    <div x-show="albums.length > 0" x-cloak>
       <button
         :disabled="isDownloading"
         class="btn btn-blue {% if name == "" %}btn-no-text{% endif %}"
diff --git a/sas/templates/sas/user_pictures.jinja b/sas/templates/sas/user_pictures.jinja
index 76c74bc7..7fdaeaaf 100644
--- a/sas/templates/sas/user_pictures.jinja
+++ b/sas/templates/sas/user_pictures.jinja
@@ -20,17 +20,17 @@
       {{ download_button(_("Download all my pictures")) }}
     {% endif %}
 
-    <template x-for="[album_id, pictures] in Object.entries(albums)" x-cloak>
+    <template x-for="album in albums" x-cloak>
       <section>
         <br />
         <div class="row">
-          <h4 x-text="pictures[0].album.name" :id="`album-${album_id}`"></h4>
+          <h4 x-text="album.name" :id="`album-${album.id}`"></h4>
           {% if user.id == object.id %}
             &nbsp;{{ download_button("") }}
           {% endif %}
         </div>
         <div class="photos">
-          <template x-for="picture in pictures">
+          <template x-for="picture in album.pictures">
             <a :href="picture.sas_url">
               <div
                 class="photo"
diff --git a/tsconfig.json b/tsconfig.json
index d0741918..1f47a39e 100644
--- a/tsconfig.json
+++ b/tsconfig.json
@@ -4,7 +4,7 @@
     "sourceMap": true,
     "noImplicitAny": true,
     "module": "esnext",
-    "target": "es2022",
+    "target": "es2024",
     "allowJs": true,
     "moduleResolution": "node",
     "experimentalDecorators": true,