ui: Fix permalink not setting currentEngineId

Bug: 230364687

aosp/2027184 didn't correctly deal with loading a trace from permalink,
from a previous state version.

It didn't update the currentEngineId based upon the values of previous
engine ids.

Change-Id: I008b58f97b633ff7a6ee9ac29b9cc6f2bc784d98
diff --git a/ui/src/controller/permalink_controller.ts b/ui/src/controller/permalink_controller.ts
index b922e8b..dc51d10 100644
--- a/ui/src/controller/permalink_controller.ts
+++ b/ui/src/controller/permalink_controller.ts
@@ -94,10 +94,16 @@
   private static upgradeState(state: State): State {
     if (state.version !== STATE_VERSION) {
       const newState = createEmptyState();
+      let maxEngineId = Number.MIN_SAFE_INTEGER;
       // Copy the URL of the trace into the empty state.
       for (const cfg of Object.values(state.engines)) {
         newState
             .engines[cfg.id] = {id: cfg.id, ready: false, source: cfg.source};
+        maxEngineId = Math.max(maxEngineId, Number(cfg.id));
+      }
+      if (maxEngineId !== Number.MIN_SAFE_INTEGER) {
+        // set the current engine Id to the maximum engine Id in the permalink
+        newState.currentEngineId = String(maxEngineId);
       }
       const message = `Unable to parse old state version. Discarding state ` +
           `and loading trace.`;