AI-native design editor. Open-source Figma alternative.
fix(cloud): open the server's editor on Home and fix the portal sign-in loop (#1417)
Danila Poyarkov1a5008c1e766
diff --git a/packages/docs/user-guide/openpencil-cloud.md b/packages/docs/user-guide/openpencil-cloud.mdindex 8a218b4db..b23d7f62d 100644--- a/packages/docs/user-guide/openpencil-cloud.md+++ b/packages/docs/user-guide/openpencil-cloud.md@@ -22,14 +22,14 @@ Each server in **Settings → Cloud** shows who is signed in to it:- **Sign-in expired** keeps your account, so **Sign in** goes back to the same one.- **Account and security** in the server's **…** menu opens the server's own account page, where you change your password, two-step sign-in, and passkeys.-- **Sign out** ends the session on the server. If the server can't be reached, you stay signed in so you can try again.+- **Sign out** ends the session on the server. If the server can't be reached, you stay signed in so you can try again. At the server's own editor address, signing out of that server goes back to its welcome page, unless the server lets you use its editor without an account and you chose to on this browser.- **Disconnect server** signs you out and forgets the server on this device; your documents stay on it, and you can connect again anytime. It warns you when documents still have changes that haven't reached the server, because disconnecting discards them.The desktop app keeps its sign-in in the system credential store. In the browser, a sign-in through the server's page lasts as long as the server's session; a sign-in with a code follows **Settings → General → Remember API keys on this device**.## Find Documents on Home-Home lists places in a sidebar: **Recent**, the workspaces of the server Home shows, **Shared with you**, and your storage bucket. On a phone the same places are in a menu above the documents. With more than one server connected, choose which one Home shows with **Show on Home** in Settings.+Home lists places in a sidebar: **Recent**, the workspaces of the server Home shows, **Shared with you**, and your storage bucket. On a phone the same places are in a menu above the documents. With more than one server connected, choose which one Home shows with **Show on Home** in Settings. At the server's own editor address, OpenPencil opens on Home, in your first workspace until you pick another place.A workspace lists its documents with where each one stands, how much of the workspace's storage they use, and your role in it. Every account starts with a workspace of its own. **New design** creates a document in the workspace you are looking at, and says which one; in **Shared with you** it creates in your own workspace, and in Recent, or a workspace you can only view, it creates a file on your device. **Shared with you** lists documents people invited you to in workspaces you are not a member of.diff --git a/src/app/cloud-portal/site.ts b/src/app/cloud-portal/site.tsindex 21f07b667..2fa7d256b 100644--- a/src/app/cloud-portal/site.ts+++ b/src/app/cloud-portal/site.ts@@ -35,9 +35,9 @@ const localEditor = useLocalStorage('open-pencil:cloud-site-local', false, {})/** Opens the server's editor without an account, now and on later visits from this browser. */-export function openLocalEditor(): void {+export function openLocalEditor(location: Pick<Location, 'assign'> = globalThis.location): void {localEditor.value = true- globalThis.location.assign('/')+ location.assign('/')}/** Who is signed in to the server, or `unreachable` when it cannot say. */@@ -62,6 +62,23 @@ export function siteDestination(visit: {return WELCOME_PATH}+/**+ * After signing out of the server that serves this editor: the welcome page, as for any visit+ * without an account, unless the server allows the local editor and this browser chose it.+ */+export function leaveAfterSignOut(+ serverAllowsLocalEditor: boolean,+ location: Pick<Location, 'assign'> = globalThis.location+): void {+ const destination = siteDestination({+ path: '/',+ account: null,+ localEditor: serverAllowsLocalEditor,+ choseLocalEditor: localEditor.value+ })+ if (destination) location.assign(destination)+}+async function signedInAccount(api: CloudPortalClient): Promise<SiteAccount> {try {return await api.accountStatus()diff --git a/src/app/cloud/home.ts b/src/app/cloud/home.tsindex 42433e14f..d520f9b6d 100644--- a/src/app/cloud/home.ts+++ b/src/app/cloud/home.ts@@ -1,5 +1,7 @@import { computed } from 'vue'+import { IS_CLOUD_SITE } from '@/constants'+import { openCloudConnect } from './connect/flow'import { cloudServerHost } from './servers/address'import { homeCloudServer } from './servers/store'@@ -18,6 +20,10 @@ export function useHomeCloud() {)return {signedIn: computed(() => signedIn.value !== null),+ // On a Cloud site the server's own connection is on its way before startup adds it.+ checking: computed(() =>+ connection.value ? connection.value.state === 'checking' : IS_CLOUD_SITE+ ),account: computed(() => {const current = signedIn.valueif (!current?.account) return nulldiff --git a/src/app/cloud/integration/off/index.ts b/src/app/cloud/integration/off/index.tsindex 57cd51101..fb2450640 100644--- a/src/app/cloud/integration/off/index.ts+++ b/src/app/cloud/integration/off/index.ts@@ -61,6 +61,7 @@ export async function cloudUploadFailure(): Promise<SaveFailure | null> {export function useHomeCloud() {return {signedIn: computed(() => false),+ checking: computed(() => false),account: computed<(CloudAccount & { host: string }) | null>(() => null),workspaces: computed<WorkspaceSummary[]>(() => []),serverId: computed<string | null>(() => null),diff --git a/src/app/cloud/servers/store.ts b/src/app/cloud/servers/store.tsindex 122edd793..a6eacf198 100644--- a/src/app/cloud/servers/store.ts+++ b/src/app/cloud/servers/store.ts@@ -116,6 +116,11 @@ export async function addCloudSiteServer(: addCloudServer({ kind: 'self-hosted', url: origin })}+/** Whether a server is the one that serves this editor on its own address. */+export function isCloudSiteServer(id: string, origin = globalThis.location.origin): boolean {+ return findCloudServer(id)?.url === normalizeCloudServerURL(origin)+}+/*** The server a link names: one this app knows, or the official one, which a link may add. Null* for any other, so a link alone never connects the app anywhere new.diff --git a/src/app/cloud/sessions/sign-out.ts b/src/app/cloud/sessions/sign-out.tsindex 576d897fc..93b598a99 100644--- a/src/app/cloud/sessions/sign-out.ts+++ b/src/app/cloud/sessions/sign-out.ts@@ -1,7 +1,10 @@import { signOutFromCloud } from '@open-pencil/cloud/client'import type { CloudDiscovery } from '@open-pencil/cloud/contract'-import { rememberCloudAccount } from '../servers/store'+import { leaveAfterSignOut } from '@/app/cloud-portal/site'+import { IS_CLOUD_SITE } from '@/constants'++import { isCloudSiteServer, rememberCloudAccount } from '../servers/store'import { cloudServerFetch, forgetCloudConnection } from './connection'import { clearCloudSessionToken } from './token'@@ -17,7 +20,8 @@ const defaults: CloudSignOutDependencies = {/*** Ends the session on the server first, so a failed sign-out keeps the token to try again- * rather than leaving a live session behind, then forgets it here.+ * rather than leaving a live session behind, then forgets it here. Signing out of the server+ * that serves this editor leaves it as a visit without an account would (`leaveAfterSignOut`).*/export async function signOutOfCloud(serverId: string,@@ -28,4 +32,5 @@ export async function signOutOfCloud(await dependencies.clear(serverId)rememberCloudAccount(serverId, null)forgetCloudConnection(serverId)+ if (IS_CLOUD_SITE && isCloudSiteServer(serverId)) leaveAfterSignOut(discovery.localEditor)}diff --git a/src/app/home/location.ts b/src/app/home/location.tsindex 82b045cf7..ac25d48c7 100644--- a/src/app/home/location.ts+++ b/src/app/home/location.ts@@ -33,3 +33,6 @@ export const homeLocation = computed<HomeLocation>({stored.value = location}})++/** Whether the person picked a place on Home, rather than Home showing its default. */+export const homeLocationChosen = computed(() => v.is(homeLocationSchema, stored.value))diff --git a/src/app/home/locations.ts b/src/app/home/locations.tsindex c72cd1d45..cdaf2b60e 100644--- a/src/app/home/locations.ts+++ b/src/app/home/locations.ts@@ -1,9 +1,10 @@import { computed } from 'vue'import { useHomeCloud } from '@/app/cloud/integration'+import { recentFilesAvailable } from '@/app/recent-files/preferences'import { openSettingsDialog } from '@/app/settings/dialog'-import { homeLocation, type HomeLocation } from './location'+import { homeLocation, homeLocationChosen, type HomeLocation } from './location'/** A Home location as the sidebar and the phone menu name it. */export type HomeLocationView =@@ -28,14 +29,18 @@ function fromView(location: HomeLocationView): HomeLocation {/*** Where Home can list documents from: Recent, the workspaces and shared documents of the server- * Home shows when signed in to it, and storage. A Cloud location the account can no longer- * reach falls back to Recent.+ * Home shows when signed in to it, and storage. Until the person picks a place, Home shows+ * Recent, or the first workspace where this app keeps no recent files. A Cloud location the+ * account can no longer reach falls back to Recent.*/export function useHomeLocations() {const cloud = useHomeCloud()const workspaces = cloud.workspaces+ const defaultsToWorkspace = computed(() => !homeLocationChosen.value && !recentFilesAvailable())const location = computed<HomeLocation>(() => {+ const first = workspaces.value.at(0)+ if (defaultsToWorkspace.value && first) return { kind: 'workspace', id: first.id }const chosen = homeLocation.valueif (chosen.kind === 'shared' && !cloud.signedIn.value) return { kind: 'recent' }if (chosen.kind === 'workspace' && !workspaces.value.some((entry) => entry.id === chosen.id)) {@@ -54,6 +59,8 @@ export function useHomeLocations() {account: cloud.account,workspaces,location,+ /** Whether Home waits for Cloud to say which workspace it opens on, rather than show Recent. */+ settling: computed(() => defaultsToWorkspace.value && cloud.checking.value),workspace,serverId: cloud.serverId,active: computed(() => toView(location.value)),diff --git a/src/app/shell/menu/navigation.ts b/src/app/shell/menu/navigation.tsindex 98f0fa86c..d711786df 100644--- a/src/app/shell/menu/navigation.ts+++ b/src/app/shell/menu/navigation.ts@@ -2,16 +2,20 @@ import type { Router } from 'vue-router'import { homeLocation, type HomeLocation } from '@/app/home/location'-/** Shows Home at one of its places, in a new tab when a document is open. */-export function openHomeLocation(router: Router, location: HomeLocation): void {- homeLocation.value = location+function showHome(router: Router): void {void router.push('/').then(() => import('@/app/tabs')).then(({ showNewTab }) => showNewTab())}+/** Shows Home at one of its places, in a new tab when a document is open. */+export function openHomeLocation(router: Router, location: HomeLocation): void {+ homeLocation.value = location+ showHome(router)+}+/** Home at the place the person browsed last: a Cloud workspace, the bucket, or Recent. */export function browseFiles(router: Router): void {- openHomeLocation(router, homeLocation.value)+ showHome(router)}diff --git a/src/cloud/router.ts b/src/cloud/router.tsindex 2aab2a2c0..1b2d142ff 100644--- a/src/cloud/router.ts+++ b/src/cloud/router.ts@@ -91,16 +91,20 @@ export function createCloudPortalRouter(context: PortalContext) {name: 'admin',component: () => import('@/views/cloud/ConsoleView.vue'),meta: { account: true, admin: true }- },- // A stale or mistyped portal address, which the server still answers with this page.- { path: '/:pathMatch(.*)*', redirect: PORTAL_HOME }+ }]})+ // Guards rather than a catch-all route: a redirect record runs before them, and the editor's+ // own address, which they send on, would match it again.router.beforeEach((to) => {- if (isCloudPortalPage(to.path)) return portalDestination(to, context.account.value)- globalThis.location.assign(to.fullPath)- return false+ if (!isCloudPortalPage(to.path)) {+ globalThis.location.assign(to.fullPath)+ return false+ }+ // A stale or mistyped portal address, which the server still answers with this page.+ if (to.matched.length === 0) return PORTAL_HOME+ return portalDestination(to, context.account.value)})return routerdiff --git a/src/components/home/HomeWorkspace.vue b/src/components/home/HomeWorkspace.vueindex 4f5cb5884..d317c0eee 100644--- a/src/components/home/HomeWorkspace.vue+++ b/src/components/home/HomeWorkspace.vue@@ -265,8 +265,10 @@ function formattedDate(updatedAt: string): string {/><p v-if="openError" class="mb-4 text-xs text-error" role="alert">{{ openError }}</p>+ <!-- Nothing until Cloud says which workspace Home opens on. -->+ <template v-if="places.settling.value" /><CloudWorkspaceSection- v-if="places.workspace.value && places.serverId.value"+ v-else-if="places.workspace.value && places.serverId.value":server-id="places.serverId.value":workspace="places.workspace.value":query="normalizedQuery"diff --git a/src/views/WorkspaceView.vue b/src/views/WorkspaceView.vueindex be221a1c1..49a59c503 100644--- a/src/views/WorkspaceView.vue+++ b/src/views/WorkspaceView.vue@@ -53,7 +53,7 @@ import FontStatusBanner from '@/components/font-status/FontStatusBanner.vue'import HomeWorkspace from '@/components/home/HomeWorkspace.vue'import RenameSelectionDialog from '@/components/selection/RenameSelectionDialog.vue'import TabBar from '@/components/TabBar.vue'-import { IS_BROWSER } from '@/constants'+import { IS_BROWSER, IS_CLOUD_SITE } from '@/constants'const route = useRoute()/**@@ -77,8 +77,13 @@ watch()const router = useRouter()const createdInitialTab = tabCount() === 0+// Home opens first where it lists something: recent files, or the workspaces of the Cloud server+// that serves the editor.const shouldCreateHome =- route.path === '/' && !appRuntimeConfig.test && !route.meta.demo && recentFilesAvailable()+ route.path === '/' &&+ !appRuntimeConfig.test &&+ !route.meta.demo &&+ (recentFilesAvailable() || IS_CLOUD_SITE)const { collaboration } = useI18n()const joinRoomFromInput = useJoinRoom()diff --git a/src/views/cloud/StandingView.vue b/src/views/cloud/StandingView.vueindex aa65f01a1..1570db54e 100644--- a/src/views/cloud/StandingView.vue+++ b/src/views/cloud/StandingView.vue@@ -40,7 +40,7 @@ async function signOut() {size="lg"variant="outline"class="w-full"- @click="openLocalEditor"+ @click="openLocalEditor()">{{ messages.useWithoutAccount }}</AppButton>diff --git a/src/views/cloud/WelcomeView.vue b/src/views/cloud/WelcomeView.vueindex be8116055..0dee8bb22 100644--- a/src/views/cloud/WelcomeView.vue+++ b/src/views/cloud/WelcomeView.vue@@ -40,7 +40,7 @@ const ui = portalPublic()size="lg"variant="outline"class="w-full"- @click="openLocalEditor"+ @click="openLocalEditor()">{{ messages.useWithoutAccount }}</AppButton>diff --git a/tests/app/cloud-portal/site.test.ts b/tests/app/cloud-portal/site.test.tsindex 9c532870d..b8e0fe1cf 100644--- a/tests/app/cloud-portal/site.test.ts+++ b/tests/app/cloud-portal/site.test.ts@@ -2,7 +2,13 @@ import { describe, expect, test } from 'bun:test'import type { CloudAccountStatus } from '@open-pencil/cloud/contract'-import { isCloudLink, siteDestination, type SiteAccount } from '@/app/cloud-portal/site'+import {+ isCloudLink,+ leaveAfterSignOut,+ openLocalEditor,+ siteDestination,+ type SiteAccount+} from '@/app/cloud-portal/site'function account(state: CloudAccountStatus['state']): CloudAccountStatus {return {@@ -60,3 +66,20 @@ describe('Cloud server editor pages', () => {expect(visit('/', 'unreachable', { localEditor: false })).toBeNull()})})++describe('signing out of the server that serves the editor', () => {+ function leave(serverAllowsLocalEditor: boolean): string[] {+ const assigned: string[] = []+ leaveAfterSignOut(serverAllowsLocalEditor, { assign: (url) => assigned.push(String(url)) })+ return assigned+ }++ // One test, since the browser's choice of the local editor is kept for the module's lifetime.+ test('leaves for the welcome page unless this browser chose the local editor the server allows', () => {+ expect(leave(false)).toEqual(['/welcome'])+ expect(leave(true)).toEqual(['/welcome'])+ openLocalEditor({ assign: () => undefined })+ expect(leave(true)).toEqual([])+ expect(leave(false)).toEqual(['/welcome'])+ })+})