From f348f1862123933434457eb85831fe4600caca19 Mon Sep 17 00:00:00 2001
From: Martin McKeaveney <martin@shogunsystems.co.uk>
Date: Fri, 28 May 2021 12:44:29 +0100
Subject: [PATCH] go straight to app if you only have one published app

---
 .../src/pages/builder/apps/index.svelte        | 13 ++++++++++---
 .../src/pages/builder/portal/index.svelte      | 18 ++++++++++++++++--
 2 files changed, 26 insertions(+), 5 deletions(-)

diff --git a/packages/builder/src/pages/builder/apps/index.svelte b/packages/builder/src/pages/builder/apps/index.svelte
index 6f8827677b..c4224b9f15 100644
--- a/packages/builder/src/pages/builder/apps/index.svelte
+++ b/packages/builder/src/pages/builder/apps/index.svelte
@@ -13,7 +13,7 @@
   } from "@budibase/bbui"
   import { onMount } from "svelte"
   import { apps, organisation, auth } from "stores/portal"
-  import { goto } from "@roxi/routify"
+  import { goto, redirect } from "@roxi/routify"
   import { AppStatus } from "constants"
   import { gradient } from "actions"
   import UpdateUserInfoModal from "components/settings/UpdateUserInfoModal.svelte"
@@ -28,10 +28,17 @@
   onMount(async () => {
     await organisation.init()
     await apps.load()
-    loaded = true
+    // Skip the portal if you only have one app
+    if (!$auth.isBuilder && $apps.filter(publishedAppsOnly).length === 1) {
+      window.location = `/${publishedApps[0].prodId}`
+    } else {
+      loaded = true
+    }
   })
 
-  $: publishedApps = $apps.filter(app => app.status === AppStatus.DEPLOYED)
+  const publishedAppsOnly = app => app.status === AppStatus.DEPLOYED
+
+  $: publishedApps = $apps.filter(publishedAppsOnly)
 </script>
 
 {#if $auth.user && loaded}
diff --git a/packages/builder/src/pages/builder/portal/index.svelte b/packages/builder/src/pages/builder/portal/index.svelte
index 5426d52fae..bac4a762ea 100644
--- a/packages/builder/src/pages/builder/portal/index.svelte
+++ b/packages/builder/src/pages/builder/portal/index.svelte
@@ -1,4 +1,18 @@
 <script>
-  import { redirect } from "@roxi/routify"
-  $redirect("./apps")
+  import { onMount } from "svelte"
+  import { redirect, goto } from "@roxi/routify"
+  import { apps, organisation, auth } from "stores/portal"
+  import { AppStatus } from "constants"
+
+  onMount(async () => {
+    await apps.load()
+    // Skip the portal if you only have one app
+    if (!$auth.isBuilder && $apps.filter(app => app.status === AppStatus.DEPLOYED).length === 1) {
+      // window.location = `/${publishedApps[0].prodId}`
+      $redirect(`/${publishedApps[0].prodId}`)
+    } else {
+      $goto("./apps")
+    }
+  })
+
 </script>