Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 19 additions & 0 deletions .github/scripts/ci/fixtures/remote-ui-browser.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
#!/usr/bin/env bash
# Install the locked platform and fixture dependencies and Chromium, then check remote rendering
# over the real frame transport. The fixture keeps its own package.json and lockfile.
set -euo pipefail

report=${1:?Expected an absolute report path}
fixture=web/src/tests/fixtures/remote-ui-browser

bun install --no-env-file --frozen-lockfile --ignore-scripts
(
cd "$fixture"
bun install --no-env-file --frozen-lockfile
node node_modules/@playwright/test/cli.js install --only-shell chromium
shell=$(find "$HOME/.cache/ms-playwright" -type f \( -name chrome-headless-shell -o -name headless_shell \) -print -quit)
if [ -z "$shell" ] || ldd "$shell" | grep -q 'not found'; then
node node_modules/@playwright/test/cli.js install-deps chromium
fi
)
bun --no-env-file "$fixture/browser.mts" "$report"
45 changes: 45 additions & 0 deletions .github/workflows/app-runtime-reference.yml
Original file line number Diff line number Diff line change
Expand Up @@ -220,3 +220,48 @@ jobs:
${{ runner.temp }}/app-runtime-reference/greeting-grants.json
${{ runner.temp }}/app-runtime-reference/greeting-recovery.json
${{ runner.temp }}/app-runtime-reference/greeting-offline-restore.json

remote-ui:
name: Remote UI browser fixture
needs: changes
if: ${{ !cancelled() }}
runs-on: ubuntu-24.04
timeout-minutes: 20
env:
# The platform's renderer, its shared components and the fixture are web inputs.
RUN_CHECK: ${{ needs.changes.outputs.web != 'false' }}
steps:
- name: Require successful change detection
env:
CHANGE_RESULT: ${{ needs.changes.result }}
run: test "$CHANGE_RESULT" = success

- name: Report unaffected check
if: env.RUN_CHECK != 'true'
run: echo 'No web input changed; this check does not need to run.'

- name: Check out exact tested commit
if: env.RUN_CHECK == 'true'
uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1
with:
persist-credentials: false
ref: ${{ github.sha }}

- name: Set up Bun
if: env.RUN_CHECK == 'true'
uses: oven-sh/setup-bun@0c5077e51419868618aeaa5fe8019c62421857d6 # v2.2.0
with:
bun-version: 1.4.5

- name: Verify remote rendering over the real frame transport
if: env.RUN_CHECK == 'true'
run: bash .github/scripts/ci/fixtures/remote-ui-browser.sh "$RUNNER_TEMP/remote-ui-browser.json"

- name: Upload browser evidence
if: env.RUN_CHECK == 'true'
uses: actions/upload-artifact@cf430e030ddbb5b0abf93d22962f4752f3646cd9 # v7.0.2
with:
name: remote-ui-browser-${{ github.run_attempt }}
if-no-files-found: error
retention-days: 14
path: ${{ runner.temp }}/remote-ui-browser.json
148 changes: 30 additions & 118 deletions web/src/features/AppUi/AppUiFrame.tsx
Original file line number Diff line number Diff line change
@@ -1,27 +1,16 @@
import { toast } from '@rentnerkev/toasts'
import { useCallback, useEffect, useEffectEvent, useLayoutEffect, useRef, useState } from 'react'
import { useEffect, useEffectEvent, useState } from 'react'

import {
appUiDefaultRoute,
type AppUiDialogRequest,
type AppUiRendering,
type AppUiToast,
} from '@/lib/AppUi/bridge.ts'
import { appUiDefaultRoute } from '@/lib/AppUi/bridge.ts'
import type { AppUiSlot, AppUiSummary } from '@/lib/AppUi/contract.ts'
import { APP_UI_PREFIX } from '@/lib/AppUi/paths.ts'
import { createRemoteUiStore, type RemoteUiEvent } from '@/lib/AppUi/remote-tree.ts'
import {
bindAppUiFrame,
supportsRemoteRendering,
type AppUiRemoteEventDetail,
type AppUiRevocation,
} from '@/lib/AppUi/session.ts'
import type { AppUiRevocation } from '@/lib/AppUi/session.ts'
import useTranslationStore from '@/shared/Language/Hooks/useTranslationStore.ts'
import { ModalActionButton } from '@/shared/Modal/Components/ModalActions.tsx'
import { useAppUi } from './AppUiProvider.tsx'
import { callAppUi } from '@/lib/AppUi/client.ts'
import { openAppUiHandler } from './middleware.ts'
import RemoteAppView from './Components/RemoteAppView/index.tsx'
import AppFrameView from './Components/AppFrameView.tsx'
import type { AppFrameSource as OpenedSource } from './Types/app-frame-view.types.ts'

type FrameProps = {
app: AppUiSummary
Expand All @@ -33,12 +22,7 @@ type FrameProps = {
fill?: boolean | undefined
}

type AppFrameSource = { nonce: string; url: string; frameToken: string }

/** A toast an app asked for, always titled with the app's name so it never looks like the platform's. */
function showAppToast(appName: string, { tone, heading, text }: AppUiToast) {
toast[tone](text, { title: heading ? `${appName} · ${heading}` : appName })
}
type AppFrameSource = OpenedSource & { frameToken: string }

export default function AppUiFrame({ app, slot, hostId, route, onNavigate, fill }: FrameProps) {
const sessionId = useAppUi()?.sessionId
Expand Down Expand Up @@ -182,57 +166,18 @@ function OpenedAppFrame({
onDisconnect: (reason: Exclude<AppUiRevocation, 'unmounted'>) => void
}) {
const ui = useAppUi()
const { t, locale } = useTranslationStore()
const theme = ui?.theme ?? 'light'
const iframe = useRef<HTMLIFrameElement>(null)
const bridge = useRef<ReturnType<typeof bindAppUiFrame> | null>(null)
// An app that may draw remotely shows nothing until its handshake says how it renders.
const [rendering, setRendering] = useState<AppUiRendering | 'pending'>(() =>
supportsRemoteRendering(app) ? 'pending' : 'frame',
)
const [remoteTree] = useState(createRemoteUiStore)
const currentContext = useEffectEvent(() => ({
theme,
locale,
route,
permissionKeys: app.permissionKeys,
}))
// Keys match the manifest pattern, so the joined list changes exactly when the keys do.
const permissionScope = app.permissionKeys.join(' ')
const navigate = useEffectEvent(onNavigate)
const dialog = useEffectEvent(
(request: AppUiDialogRequest, settle: (outcome: 'confirmed' | 'cancelled') => void) => {
ui?.openDialog({
appName: app.name,
dialogId: request.dialogId,
mount: source.nonce,
settle,
})
},
)
const revoke = useEffectEvent((reason: AppUiRevocation) => {
ui?.cancelDialog(source.nonce)
if (reason !== 'unmounted') onDisconnect(reason)
})

const onRemoteEvent = useCallback(
(node: string, event: RemoteUiEvent, detail: AppUiRemoteEventDetail) =>
bridge.current?.sendRemoteEvent(node, event, detail),
[],
)

const bind = useEffectEvent((element: HTMLIFrameElement) =>
bindAppUiFrame(element, source.nonce, app, {
onConnected: (mode) => {
setRendering(mode)
return currentContext()
},
onRemoteMutate: (records) => remoteTree.apply(records),
onToast: (message) => showAppToast(app.name, message),
onNavigate: (target) => navigate(target),
onDialog: (request, settle) => dialog(request, settle),
onRevoked: (reason) => revoke(reason),
onApiRequest: ({ requestId, operationId, idempotencyKey, payload }, signal) =>
// Operations go through the gateway with this frame's token; dialogs open in the platform.
return (
<AppFrameView
app={app}
source={source}
slot={slot}
route={route}
fill={fill}
theme={ui?.theme ?? 'light'}
onNavigate={onNavigate}
onDisconnect={onDisconnect}
onApiRequest={({ requestId, operationId, idempotencyKey, payload }, signal) =>
callAppUi({
data: {
installationId: app.installationId,
Expand All @@ -244,50 +189,17 @@ function OpenedAppFrame({
payload,
},
signal,
}),
}),
)

useLayoutEffect(() => {
const element = iframe.current
if (!element) return
const session = bind(element)
bridge.current = session
return () => {
bridge.current = null
session.close()
}
}, [])

// The inventory poll updates the user's keys while the frame stays open.
useEffect(() => {
const permissionKeys = permissionScope === '' ? [] : permissionScope.split(' ')
bridge.current?.sendContext({ theme, locale, route, permissionKeys })
}, [locale, permissionScope, route, theme])

// A remotely drawn app keeps running in its hidden frame, which never takes focus.
return (
<>
<iframe
ref={iframe}
src={source.url}
title={app.name}
sandbox="allow-scripts"
referrerPolicy="no-referrer"
aria-hidden={rendering === 'frame' ? undefined : true}
tabIndex={rendering === 'frame' ? undefined : -1}
className={
rendering === 'frame'
? `block w-full rounded-xl border border-border bg-surface ${slot === 'dashboardWidget' ? 'min-h-64' : 'min-h-96'} ${fill ? 'h-full' : ''}`
: 'hidden'
}
/>
{rendering === 'pending' ? (
<output className="block p-3 text-sm text-muted">{t('appUi.loading')}</output>
) : null}
{rendering === 'remote' ? (
<RemoteAppView store={remoteTree} onEvent={onRemoteEvent} fill={fill} />
) : null}
</>
})
}
onDialog={(request, settle) =>
ui?.openDialog({
appName: app.name,
dialogId: request.dialogId,
mount: source.nonce,
settle,
})
}
onEnded={() => ui?.cancelDialog(source.nonce)}
/>
)
}
126 changes: 126 additions & 0 deletions web/src/features/AppUi/Components/AppFrameView.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,126 @@
import { toast } from '@rentnerkev/toasts'
import { useCallback, useEffect, useEffectEvent, useLayoutEffect, useRef, useState } from 'react'

import type { AppUiRendering, AppUiToast } from '@/lib/AppUi/bridge.ts'
import { createRemoteUiStore, type RemoteUiEvent } from '@/lib/AppUi/remote-tree.ts'
import {
bindAppUiFrame,
supportsRemoteRendering,
type AppUiRemoteEventDetail,
type AppUiRevocation,
} from '@/lib/AppUi/session.ts'
import useTranslationStore from '@/shared/Language/Hooks/useTranslationStore.ts'
import type { AppFrameViewProps } from '../Types/app-frame-view.types.ts'
import RemoteAppView from './RemoteAppView/index.tsx'

/** A toast an app asked for, always titled with the app's name so it never looks like the platform's. */
function showAppToast(appName: string, { tone, heading, text }: AppUiToast) {
toast[tone](text, { title: heading ? `${appName} · ${heading}` : appName })
}

/**
* An opened app frame and its bridge session. An app that renders remotely keeps running in the
* frame, which stays hidden and out of the focus order, while the platform draws its tree.
*/
export default function AppFrameView({
app,
source,
slot,
route,
fill,
theme,
onNavigate,
onDisconnect,
onApiRequest,
onDialog,
onEnded,
}: AppFrameViewProps) {
const { t, locale } = useTranslationStore()
const iframe = useRef<HTMLIFrameElement>(null)
const bridge = useRef<ReturnType<typeof bindAppUiFrame> | null>(null)
// An app that may draw remotely shows nothing until its handshake says how it renders.
const [rendering, setRendering] = useState<AppUiRendering | 'pending'>(() =>
supportsRemoteRendering(app) ? 'pending' : 'frame',
)
const [remoteTree] = useState(createRemoteUiStore)
const currentContext = useEffectEvent(() => ({
theme,
locale,
route,
permissionKeys: app.permissionKeys,
}))
// Keys match the manifest pattern, so the joined list changes exactly when the keys do.
const permissionScope = app.permissionKeys.join(' ')
const navigate = useEffectEvent(onNavigate)
const dialog = useEffectEvent(onDialog)
const request = useEffectEvent(onApiRequest)
const revoke = useEffectEvent((reason: AppUiRevocation) => {
onEnded?.()
if (reason !== 'unmounted') onDisconnect(reason)
})
const onRemoteEvent = useCallback(
(node: string, event: RemoteUiEvent, detail: AppUiRemoteEventDetail) =>
bridge.current?.sendRemoteEvent(node, event, detail),
[],
)

const bind = useEffectEvent((element: HTMLIFrameElement) =>
bindAppUiFrame(element, source.nonce, app, {
onConnected: (mode) => {
setRendering(mode)
return currentContext()
},
onRemoteMutate: (records) => remoteTree.apply(records),
onToast: (message) => showAppToast(app.name, message),
onNavigate: (target) => navigate(target),
onDialog: (next, settle) => dialog(next, settle),
onRevoked: (reason) => revoke(reason),
onApiRequest: (next, signal) => request(next, signal),
}),
)

useLayoutEffect(() => {
const element = iframe.current
if (!element) return
const session = bind(element)
bridge.current = session
return () => {
bridge.current = null
session.close()
}
}, [])

// The inventory poll updates the user's keys while the frame stays open.
useEffect(() => {
const permissionKeys = permissionScope === '' ? [] : permissionScope.split(' ')
bridge.current?.sendContext({ theme, locale, route, permissionKeys })
}, [locale, permissionScope, route, theme])

const shown = rendering === 'frame'
// The hidden attribute hides the frame even where no stylesheet applies.
return (
<>
<iframe
ref={iframe}
src={source.url}
title={app.name}
sandbox="allow-scripts"
referrerPolicy="no-referrer"
hidden={!shown}
aria-hidden={shown ? undefined : true}
tabIndex={shown ? undefined : -1}
className={
shown
? `block w-full rounded-xl border border-border bg-surface ${slot === 'dashboardWidget' ? 'min-h-64' : 'min-h-96'} ${fill ? 'h-full' : ''}`
: undefined
}
/>
{rendering === 'pending' ? (
<output className="block p-3 text-sm text-muted">{t('appUi.loading')}</output>
) : null}
{rendering === 'remote' ? (
<RemoteAppView store={remoteTree} onEvent={onRemoteEvent} fill={fill} />
) : null}
</>
)
}
Loading