From 7ed95d5335da7489f67598e142a39e598b36fffa Mon Sep 17 00:00:00 2001
From: Xiao Gui <xgui3783@gmail.com>
Date: Mon, 12 Jun 2023 15:08:42 +0200
Subject: [PATCH] maint: use region name over ngId + label index

---
 docs/releases/v2.11.3.md                      |  6 ++++-
 .../routeStateTransform.service.ts            | 22 +++++++++----------
 src/routerModule/type.ts                      |  1 +
 src/util/fn.ts                                |  2 +-
 4 files changed, 17 insertions(+), 14 deletions(-)

diff --git a/docs/releases/v2.11.3.md b/docs/releases/v2.11.3.md
index 1070a19f2..834f0c7dd 100644
--- a/docs/releases/v2.11.3.md
+++ b/docs/releases/v2.11.3.md
@@ -2,4 +2,8 @@
 
 ## Bugfixes
 
-- Fixed point assignment for maps that do not have statistical maps. Also fixed error messages.
\ No newline at end of file
+- Fixed point assignment for maps that do not have statistical maps. Also fixed error messages.
+
+## Behind the scenes
+
+- Using hashed region name to encode selected region, rather than the archaic ngId & label index
diff --git a/src/routerModule/routeStateTransform.service.ts b/src/routerModule/routeStateTransform.service.ts
index 1032448ec..b01ac54be 100644
--- a/src/routerModule/routeStateTransform.service.ts
+++ b/src/routerModule/routeStateTransform.service.ts
@@ -9,6 +9,7 @@ import { getParcNgId } from "src/viewerModule/nehuba/config.service";
 import { decodeToNumber, encodeNumber, encodeURIFull, separator } from "./cipher";
 import { TUrlAtlas, TUrlPathObj, TUrlStandaloneVolume } from "./type";
 import { decodePath, encodeId, decodeId, encodePath } from "./util";
+import { QuickHash } from "src/util/fn";
 
 @Injectable()
 export class RouteStateTransformSvc {
@@ -26,6 +27,7 @@ export class RouteStateTransformSvc {
     const selectedTemplateId = decodeId( RouteStateTransformSvc.GetOneAndOnlyOne(obj.t) )
     const selectedParcellationId = decodeId( RouteStateTransformSvc.GetOneAndOnlyOne(obj.p) )
     const selectedRegionIds = obj.r
+    const selectedRegionNames = obj.rn
     
     if (!selectedAtlasId || !selectedTemplateId || !selectedParcellationId) {
       return {}
@@ -60,7 +62,11 @@ export class RouteStateTransformSvc {
     const userViewer = await this.sapi.useViewer(selectedTemplate).toPromise()
     
     const selectedRegions = await (async () => {
-      if (!selectedRegionIds) return []
+      if (!selectedRegionIds && !selectedRegionNames) return []
+
+      if (selectedRegionNames && selectedRegionNames.length > 0) {
+        return allParcellationRegions.filter(region => selectedRegionNames.includes(QuickHash.GetHash(region.name)))
+      }
 
       /**
        * should account for 
@@ -268,16 +274,7 @@ export class RouteStateTransformSvc {
       }
     }
   
-    // encoding selected regions
-    let selectedRegionsString: string
-    if (selectedRegions.length === 1) {
-      const region = selectedRegions[0]
-      const labelIndex = await this.sapi.getRegionLabelIndices(selectedTemplate, selectedParcellation, region)
-      
-      const ngId = getParcNgId(selectedAtlas, selectedTemplate, selectedParcellation, region)
-      selectedRegionsString = `${ngId}::${encodeNumber(labelIndex, { float: false })}`
-    }
-    let routes: TUrlPathObj<string, TUrlAtlas<string>> | TUrlPathObj<string, TUrlStandaloneVolume<string>>
+    let routes: TUrlPathObj<string|string[], TUrlAtlas<string|string[]>> | TUrlPathObj<string, TUrlStandaloneVolume<string>>
     
     routes = {
       // for atlas
@@ -287,7 +284,8 @@ export class RouteStateTransformSvc {
       // for parcellation
       p: selectedParcellation && encodeId(selectedParcellation.id),
       // for regions
-      r: selectedRegionsString && encodeURIFull(selectedRegionsString),
+      // r: selectedRegionsString && encodeURIFull(selectedRegionsString),
+      rn: selectedRegions[0] && selectedRegions.map(r => QuickHash.GetHash(r.name)),
       // nav
       ['@']: cNavString,
       // showing dataset
diff --git a/src/routerModule/type.ts b/src/routerModule/type.ts
index 13205ad3e..284b5e7a9 100644
--- a/src/routerModule/type.ts
+++ b/src/routerModule/type.ts
@@ -7,6 +7,7 @@ export type TUrlAtlas<T> = {
   t: T   // template
   p: T   // parcellation
   r?: T  // region selected
+  rn?: T
 }
 
 export type TUrlPlugin<T> = {
diff --git a/src/util/fn.ts b/src/util/fn.ts
index 9c1d188f7..6457ccf78 100644
--- a/src/util/fn.ts
+++ b/src/util/fn.ts
@@ -134,7 +134,7 @@ export const CachedFunction = (config?: TCacheFunctionArg) => {
   }
 }
 
-// A quick, non security hash function
+// A quick, non secure hash function
 export class QuickHash {
   private length = 6
   constructor(opts?: any){
-- 
GitLab