From 673dc95b0759a5a1ba67d2054aa639f30a4b746c Mon Sep 17 00:00:00 2001
From: Chris Jordan <jordanchriss@gmail.com>
Date: Mon, 21 Oct 2024 23:45:19 -0400
Subject: [PATCH] added ability to upgrade state in
 UrlHashBinding.updateFromUrlHash for backwards compatibilty

---
 src/ui/default_viewer_setup.ts | 17 ++++++++++++++++-
 src/ui/url_hash_binding.ts     |  4 ++--
 2 files changed, 18 insertions(+), 3 deletions(-)

diff --git a/src/ui/default_viewer_setup.ts b/src/ui/default_viewer_setup.ts
index 405113b63e..33b5743e46 100644
--- a/src/ui/default_viewer_setup.ts
+++ b/src/ui/default_viewer_setup.ts
@@ -161,7 +161,22 @@ export function setupDefaultViewer() {
       hashBinding.parseError;
     }),
   );
-  hashBinding.updateFromUrlHash();
+  hashBinding.updateFromUrlHash((state) => {
+    // convert graphene state timestamp to layer timestamp
+    const fixTimestamp = (layer: any) => {
+      if (layer.source?.state?.timestamp) {
+        layer.timestamp = layer.source.state.timestamp;
+        layer.source.state.timestamp = undefined;
+      }
+    };
+    if (state.layers) {
+      const layers = Array.isArray(state.layers)
+        ? state.layers
+        : Object.values(state.layers);
+      layers.map(fixTimestamp);
+    }
+    return state;
+  });
   viewer.registerDisposer(bindTitle(viewer.title));
 
   bindDefaultCopyHandler(viewer);
diff --git a/src/ui/url_hash_binding.ts b/src/ui/url_hash_binding.ts
index 5c338cb5e7..ef09b27c39 100644
--- a/src/ui/url_hash_binding.ts
+++ b/src/ui/url_hash_binding.ts
@@ -113,7 +113,7 @@ export class UrlHashBinding extends RefCounted {
    * Sets the current state to match the URL hash.  If it is desired to initialize the state based
    * on the URL hash, then this should be called immediately after construction.
    */
-  updateFromUrlHash() {
+  updateFromUrlHash(upgradeState: (a: any) => any = (x) => x) {
     try {
       let s = location.href.replace(/^[^#]+/, "");
       if (s === "" || s === "#" || s === "#!") {
@@ -160,7 +160,7 @@ export class UrlHashBinding extends RefCounted {
         this.root.reset();
         const state = urlSafeParse(s);
         verifyObject(state);
-        this.root.restoreState(state);
+        this.root.restoreState(upgradeState(state));
       } else {
         throw new Error(
           `URL hash is expected to be of the form "#!{...}" or "#!+{...}".`,