From 1b5e588241cedd1b4e8db7c847fb3f137b72d211 Mon Sep 17 00:00:00 2001 From: Nariman Jelveh Date: Sat, 14 Nov 2026 11:03:55 -0800 Subject: [PATCH] Bring back the dashboard's Open Desktop card, pointed at desktop.puter.com (#4648) The Home tab's Open Desktop card returns with its markup, styles and strings, and links to https://desktop.puter.com instead of /desktop. A plain click opens the desktop the way the Apps tab opens an app in its own tab: the launch params (app instance id, app id, Puter origin, API origin, locale) go in the URL, and the app token is handed to the tab through the opener rather than placed in the URL. The token is requested by origin, so fetchUserAppTokenForLaunch accepts an origin and returns the app uid the server resolved. Without a token the tab still opens at the plain URL. A modified click, or a refused tab, stays a plain link. --- src/gui/src/UI/Dashboard/TabHome.js | 32 ++++++- src/gui/src/UI/Dashboard/TabHome.test.js | 43 ++++++++- src/gui/src/css/dashboard.css | 54 ++++++++++++ src/gui/src/helpers/appLaunchApi.js | 12 ++- src/gui/src/helpers/dashboardAppLaunch.js | 48 ++++++++++ .../src/helpers/dashboardAppLaunch.test.js | 88 ++++++++++++++++++- src/gui/src/i18n/translations/cs.js | 1 + src/gui/src/i18n/translations/en.js | 2 + 8 files changed, 272 insertions(+), 8 deletions(-) diff --git a/src/gui/src/UI/Dashboard/TabHome.js b/src/gui/src/UI/Dashboard/TabHome.js index 8a50ec329..02926d152 100644 --- a/src/gui/src/UI/Dashboard/TabHome.js +++ b/src/gui/src/UI/Dashboard/TabHome.js @@ -23,7 +23,11 @@ import { addonCreditsRemaining, meterPercent, monthlyUsageOrThrow, usageBudget } import { getMonthlyUsage, getStorageSpace } from './usageRequests.js'; import { appIconAttrs } from '../../helpers/appIcon.js'; import { filterLaunchableApps } from '../../helpers/launchAppsList.js'; -import { openAppInNewTab } from '../../helpers/dashboardAppLaunch.js'; +import { + DESKTOP_URL, + openAppInNewTab, + openDesktopInNewTab, +} from '../../helpers/dashboardAppLaunch.js'; import { isOrgSeat, orgSeatTeamName } from './orgSeat.js'; import { intlLocale } from '../../i18n/intlLocale.js'; @@ -296,6 +300,25 @@ const TabHome = { h += ''; h += ''; + // Open Desktop card (spans full width, links to the desktop interface) + h += + ``; + h += + '
'; + h += + ''; + h += '
'; + h += '
'; + h += `

${i18n('open_desktop')}

`; + h += ''; + h += + ''; + h += i18n('open_desktop_hint'); + h += ''; + h += '
'; + h += '›'; + h += '
'; + h += ''; return h; }, @@ -383,6 +406,13 @@ const TabHome = { } }); + // The desktop opens signed in, like an app's tab. A modified click + // stays a plain link. + $el_window.on('click', '.bento-desktop', function (e) { + if (e.metaKey || e.ctrlKey || e.shiftKey) return; + if (openDesktopInNewTab()) e.preventDefault(); + }); + // Handle "View details" link clicks $el_window.on( 'click', diff --git a/src/gui/src/UI/Dashboard/TabHome.test.js b/src/gui/src/UI/Dashboard/TabHome.test.js index 2d2c79ac4..dd9c7d49b 100644 --- a/src/gui/src/UI/Dashboard/TabHome.test.js +++ b/src/gui/src/UI/Dashboard/TabHome.test.js @@ -22,7 +22,11 @@ import { describe, expect, it, vi } from 'vitest'; import jQuery from '../../lib/jquery-3.6.1/jquery-3.6.1.min.js'; vi.mock('../UIWindowSaveAccount.js', () => ({ default: vi.fn() })); -vi.mock('../../helpers/dashboardAppLaunch.js', () => ({ openAppInNewTab: vi.fn() })); +vi.mock('../../helpers/dashboardAppLaunch.js', () => ({ + DESKTOP_URL: 'https://desktop.puter.com/', + openAppInNewTab: vi.fn(), + openDesktopInNewTab: vi.fn(), +})); globalThis.$ = jQuery; globalThis.jQuery = jQuery; @@ -32,7 +36,7 @@ window.icons = {}; window.launch_apps = { recent: [] }; const { default: TabHome, formatTrialEnd } = await import('./TabHome.js'); -const { openAppInNewTab } = await import('../../helpers/dashboardAppLaunch.js'); +const { openAppInNewTab, openDesktopInNewTab } = await import('../../helpers/dashboardAppLaunch.js'); describe('TabHome recently used', () => { const mount = () => { @@ -97,6 +101,41 @@ describe('TabHome recent app clicks', () => { }); }); +describe('TabHome Open Desktop card', () => { + const mount = () => { + document.body.innerHTML = `
${TabHome.html()}
`; + const $el_window = $('.window'); + $.ajax = vi.fn(() => new Promise(() => {})); + vi.spyOn(TabHome, 'loadUsageData').mockImplementation(async () => {}); + TabHome.init($el_window); + return $el_window.find('.bento-desktop')[0]; + }; + const click = (card, init) => { + const event = new MouseEvent('click', { bubbles: true, cancelable: true, ...init }); + card.dispatchEvent(event); + return event; + }; + + it('links to the desktop and opens it signed in on a plain click', () => { + openDesktopInNewTab.mockReturnValue(Promise.resolve()); + const card = mount(); + expect(card.getAttribute('href')).toBe('https://desktop.puter.com/'); + + expect(click(card).defaultPrevented).toBe(true); + expect(openDesktopInNewTab).toHaveBeenCalledTimes(1); + }); + + it('stays a plain link on a modified click, or when the tab was refused', () => { + openDesktopInNewTab.mockReset(); + const card = mount(); + expect(click(card, { metaKey: true }).defaultPrevented).toBe(false); + expect(openDesktopInNewTab).not.toHaveBeenCalled(); + + openDesktopInNewTab.mockReturnValue(null); + expect(click(card).defaultPrevented).toBe(false); + }); +}); + describe('formatTrialEnd', () => { const at = (day, hour) => new Date(2026, 10, day, hour).getTime(); diff --git a/src/gui/src/css/dashboard.css b/src/gui/src/css/dashboard.css index 8dea4d827..ed8ad7c41 100644 --- a/src/gui/src/css/dashboard.css +++ b/src/gui/src/css/dashboard.css @@ -117,6 +117,11 @@ --dashboard-bento-usage-surface: #eef2ff; --dashboard-bento-usage-header-bg: #e0e7ff; --dashboard-bento-usage-bar-fill: #ea580c; + /* Emerald (matches the green icon accent used elsewhere) */ + --dashboard-bento-desktop-icon-bg: #059669; + --dashboard-bento-desktop-icon-shadow: rgba(5, 150, 105, 0.22); + --dashboard-bento-desktop-surface: #ecfdf5; + --dashboard-bento-desktop-header-bg: #d1fae5; --dashboard-shadow-subtle: rgba(0, 0, 0, 0.04); --dashboard-shadow-light: rgba(0, 0, 0, 0.06); @@ -239,6 +244,10 @@ body { --dashboard-bento-usage-surface: #1c1d2e; --dashboard-bento-usage-header-bg: #252a42; --dashboard-bento-usage-bar-fill: #fb923c; + --dashboard-bento-desktop-icon-bg: #10b981; + --dashboard-bento-desktop-icon-shadow: rgba(16, 185, 129, 0.32); + --dashboard-bento-desktop-surface: #16241f; + --dashboard-bento-desktop-header-bg: #172e26; --dashboard-shadow-subtle: rgba(0, 0, 0, 0.2); --dashboard-shadow-light: rgba(0, 0, 0, 0.3); @@ -7375,6 +7384,11 @@ body.myapps-reordering .myapps-tile { color: #fff; } +.dashboard .bento-card-fancy-icon-desktop { + background-color: var(--dashboard-bento-desktop-icon-bg); + color: #fff; +} + .dashboard .bento-card-fancy-text { display: flex; flex-direction: column; @@ -7537,6 +7551,41 @@ body.myapps-reordering .myapps-tile { color: var(--dashboard-text-tertiary); } +/* Open Desktop bento card (full width; links to the desktop interface) */ +.dashboard .bento-desktop { + grid-column: 1 / -1; + display: flex; + align-items: center; + gap: 14px; + padding: 18px 24px; + background-color: var(--dashboard-bento-desktop-header-bg); + text-decoration: none; + color: inherit; + transition: box-shadow 0.15s ease; +} + +@media (hover: hover) { + .dashboard .bento-desktop:hover { + box-shadow: + 0 2px 4px var(--dashboard-shadow-subtle), + 0 10px 28px var(--dashboard-bento-desktop-icon-shadow); + } + + .dashboard .bento-desktop:hover .bento-desktop-arrow { + transform: translateX(3px); + color: var(--dashboard-text-primary); + } +} + +.dashboard .bento-desktop-arrow { + margin-left: auto; + font-size: 22px; + font-weight: 300; + line-height: 1; + color: var(--dashboard-text-hint); + transition: transform 0.15s ease, color 0.15s ease; +} + /* Usage bento card (cool indigo surfaces; orange bars stay as progress accent) */ .dashboard .bento-usage { grid-column: 1 / -1; @@ -7859,6 +7908,11 @@ body.myapps-reordering .myapps-tile { gap: 12px; } + .dashboard .bento-desktop { + padding: 16px 20px; + gap: 12px; + } + .dashboard .bento-card-fancy-icon { width: 40px; height: 40px; diff --git a/src/gui/src/helpers/appLaunchApi.js b/src/gui/src/helpers/appLaunchApi.js index 125fe163b..515388a91 100644 --- a/src/gui/src/helpers/appLaunchApi.js +++ b/src/gui/src/helpers/appLaunchApi.js @@ -26,16 +26,19 @@ */ /** - * Fetches the token the app signs in with. + * Fetches the token the app signs in with. An app without a known uid is + * named by its `origin`, and the uid the server resolved comes back with the + * token. * - * @param {{ appUid?: string } & AppLaunchServer} options + * @param {{ appUid?: string, origin?: string } & AppLaunchServer} options */ export const fetchUserAppTokenForLaunch = async ({ appUid, + origin, apiOrigin = window.api_origin, authToken = window.auth_token, } = {}) => { - if ( ! appUid ) { + if ( !appUid && !origin ) { return { token: null, reason: 'missing-app-uid', @@ -48,7 +51,7 @@ export const fetchUserAppTokenForLaunch = async ({ 'Content-Type': 'application/json', 'Authorization': `Bearer ${authToken}`, }, - body: JSON.stringify({ app_uid: appUid }), + body: JSON.stringify(appUid ? { app_uid: appUid } : { origin }), method: 'POST', }); @@ -62,6 +65,7 @@ export const fetchUserAppTokenForLaunch = async ({ if ( response.ok && responseBody?.token ) { return { token: responseBody.token, + appUid: responseBody.app_uid ?? appUid, }; } diff --git a/src/gui/src/helpers/dashboardAppLaunch.js b/src/gui/src/helpers/dashboardAppLaunch.js index 09c5f55a9..a15e26cd1 100644 --- a/src/gui/src/helpers/dashboardAppLaunch.js +++ b/src/gui/src/helpers/dashboardAppLaunch.js @@ -20,6 +20,13 @@ import UIAlert from '../UI/UIAlert.js'; import launch_app from './launchApp.js'; import { focusExistingAppWindow } from './focusAppWindow.js'; +import { offerAppTabLaunch } from './appTabLaunch.js'; +import { appendLaunchContext } from './appLaunchUrl.js'; +import { fetchUserAppTokenForLaunch } from './appLaunchApi.js'; +import { uuidv4 } from './uuidv4.js'; + +/** The desktop interface, served as its own site. */ +export const DESKTOP_URL = 'https://desktop.puter.com/'; // Apps whose launch is still fetching; a second click would open a second tab. const launching = new Set(); @@ -47,3 +54,44 @@ export function openAppInNewTab (appName) { }) .finally(() => launching.delete(appName)); } + +let launchingDesktop = false; + +/** + * Opens the desktop in a new browser tab, launched the way an app's tab is: + * the launch params go in its URL and its token is handed over through the + * opener. Without a token the tab still opens, and the desktop signs in on + * its own. Call it inside the click, so popup blockers allow the tab. + * + * @returns {Promise | null} null when the browser refused the tab, + * leaving the click to the link itself + */ +export function openDesktopInNewTab () { + if ( launchingDesktop ) return Promise.resolve(); + // Without 'noopener': the desktop's SDK asks this tab for its token. + const tab = window.open('', '_blank'); + if ( ! tab ) return null; + launchingDesktop = true; + const url = new URL(DESKTOP_URL); + return fetchUserAppTokenForLaunch({ origin: url.origin }) + .then(({ token, appUid }) => { + if ( tab.closed ) return; + if ( token && appUid ) { + const appInstanceID = uuidv4(); + url.searchParams.append('puter.app_instance_id', appInstanceID); + url.searchParams.append('puter.app.id', appUid); + appendLaunchContext(url, { + appDomain: window.app_domain, + pageUrl: window.location.href, + apiOrigin: window.api_origin, + locale: window.locale, + surface: 'dashboard', + }); + offerAppTabLaunch({ appInstanceID, tab, origin: url.origin, token, item: null }); + } + tab.location.href = url.href; + }) + .finally(() => { + launchingDesktop = false; + }); +} diff --git a/src/gui/src/helpers/dashboardAppLaunch.test.js b/src/gui/src/helpers/dashboardAppLaunch.test.js index 8e4762bf0..a7cf5ed1e 100644 --- a/src/gui/src/helpers/dashboardAppLaunch.test.js +++ b/src/gui/src/helpers/dashboardAppLaunch.test.js @@ -31,7 +31,7 @@ globalThis.i18n = key => key; const { default: launch_app } = await import('./launchApp.js'); const { default: UIAlert } = await import('../UI/UIAlert.js'); -const { openAppInNewTab } = await import('./dashboardAppLaunch.js'); +const { openAppInNewTab, openDesktopInNewTab } = await import('./dashboardAppLaunch.js'); describe('openAppInNewTab', () => { let opened; @@ -92,3 +92,89 @@ describe('openAppInNewTab', () => { expect(UIAlert).toHaveBeenCalledWith('something_went_wrong'); }); }); + +describe('openDesktopInNewTab', () => { + const DESKTOP = 'https://desktop.puter.com'; + let tab; + + const answerTokenRequest = (status, body) => { + vi.stubGlobal('fetch', vi.fn(async () => ({ + ok: status === 200, + status, + json: async () => body, + }))); + }; + + /** Asks for the launch as the desktop's SDK would, from `source` at `origin`. */ + const requestLaunch = (appInstanceID, { source = tab, origin = DESKTOP } = {}) => { + window.dispatchEvent(new MessageEvent('message', { + origin, + source, + data: { msg: 'puter.requestLaunch', appInstanceID }, + })); + }; + + beforeEach(() => { + tab = { closed: false, location: { href: '' }, postMessage: vi.fn() }; + vi.spyOn(window, 'open').mockImplementation(() => tab); + window.api_origin = 'https://api.puter.test'; + window.auth_token = 'session-token'; + window.app_domain = 'puter.site'; + window.locale = 'en'; + }); + + afterEach(() => { + vi.unstubAllGlobals(); + vi.restoreAllMocks(); + }); + + it('sends the tab to the desktop with the launch params and hands its token over on request', async () => { + answerTokenRequest(200, { token: 'app-token', app_uid: 'app-desktop' }); + await openDesktopInNewTab(); + + expect(window.open).toHaveBeenCalledWith('', '_blank'); + const [endpoint, request] = fetch.mock.calls[0]; + expect(endpoint).toBe('https://api.puter.test/auth/get-user-app-token'); + expect(JSON.parse(request.body)).toEqual({ origin: DESKTOP }); + + const url = new URL(tab.location.href); + expect(url.origin).toBe(DESKTOP); + expect(url.searchParams.get('puter.app.id')).toBe('app-desktop'); + expect(url.searchParams.get('puter.api_origin')).toBe('https://api.puter.test'); + expect(url.searchParams.get('puter.origin')).toBe(window.location.origin); + expect(url.searchParams.get('utm_campaign')).toBe('dashboard'); + expect(url.href).not.toContain('app-token'); + + const appInstanceID = url.searchParams.get('puter.app_instance_id'); + requestLaunch(appInstanceID, { origin: 'https://evil.example' }); + requestLaunch(appInstanceID, { source: {} }); + expect(tab.postMessage).not.toHaveBeenCalled(); + + requestLaunch(appInstanceID); + expect(tab.postMessage).toHaveBeenCalledWith( + { msg: 'puter.launch', appInstanceID, token: 'app-token', item: null }, + DESKTOP, + ); + }); + + it('opens the desktop without launch params when no token came back', async () => { + answerTokenRequest(500, null); + await openDesktopInNewTab(); + expect(tab.location.href).toBe(`${DESKTOP}/`); + }); + + it('leaves a tab the user closed alone', async () => { + answerTokenRequest(200, { token: 'app-token', app_uid: 'app-desktop' }); + const launch = openDesktopInNewTab(); + tab.closed = true; + await launch; + expect(tab.location.href).toBe(''); + }); + + it('returns null when the browser refuses the tab', () => { + window.open.mockReturnValue(null); + vi.stubGlobal('fetch', vi.fn()); + expect(openDesktopInNewTab()).toBeNull(); + expect(fetch).not.toHaveBeenCalled(); + }); +}); diff --git a/src/gui/src/i18n/translations/cs.js b/src/gui/src/i18n/translations/cs.js index e50911f76..8472463e6 100644 --- a/src/gui/src/i18n/translations/cs.js +++ b/src/gui/src/i18n/translations/cs.js @@ -183,6 +183,7 @@ const cs = { "open_in_new_tab": "Otevřít na nové kartě", "open_in_new_window": "Otevřít v novém okně", "new_email": "Nový e-mail", + "open_desktop": "Otevřít plochu", "new_username": "Nové uživatelské jméno", "open_with_ai": "Otevřít pomocí AI", "os": "Operační systém", diff --git a/src/gui/src/i18n/translations/en.js b/src/gui/src/i18n/translations/en.js index 6dc2c0638..e0664f07e 100644 --- a/src/gui/src/i18n/translations/en.js +++ b/src/gui/src/i18n/translations/en.js @@ -322,6 +322,7 @@ const en = { ok: 'OK', or: 'or', open: 'Open', + open_desktop: 'Open Desktop', open_with_ai: 'Open with AI', open_in_new_tab: 'Open in New Tab', app_pages: 'App pages', @@ -670,6 +671,7 @@ const en = { no_recent_apps_hint: 'Apps you use will appear here', recently_used: 'Recently used', monthly_overview: 'Monthly overview', + open_desktop_hint: 'Switch to the desktop interface', taking_longer_than_usual: 'Taking a little longer than usual. Please wait...', email_needed_to_create_token: 'This account has no email address, which tokens require.', payments_not_configured: 'Payments are not configured on this deployment.',