How to add a page to the dashboard
This guide shows you how to show a page of your app in the Picked dashboard. The page can be a page of its own in the project, or a part of the publish modal of a workspace.
You need an app with a server that uses @picked/app-kit, and the private key of the app.
Build and install your first app makes these.
1. Declare a slot
Add a slot to slots in the manifest. The location of the slot tells the dashboard where
to show the page:
location | Where the page shows |
|---|---|
app-home | A page of its own in the project, at /<organization>/<project>/apps/<installation slug>. |
workspace-prepublish-actions | The publish modal, above the Publish button. |
workspace-postpublish-actions | The publish modal, after Picked publishes the workspace. |
import { defineConfig } from '@picked/config/define'
const endpoint = process.env.APP_PUBLIC_URL ?? 'http://localhost:4103'
export default defineConfig({
app: {
handle: 'my-app',
name: 'My App',
endpoint,
slots: [
{ location: 'app-home', url: `${endpoint}/`, title: 'Settings', height: 320 },
{
location: 'workspace-postpublish-actions',
url: `${endpoint}/publish`,
title: 'After publish',
height: 120,
},
],
},
})The url must be a URL that the rule for the endpoint permits.
The page must not have the origin of the dashboard. Slots
gives each field.
2. Serve the page
Serve the page with page from @picked/app-kit/server. page sets the
content-security-policy header to frame-ancestors with the origins that you give.
Therefore, only the dashboard can show the page in an iframe.
Give page the origins that dashboardOrigins() from readAppEnvironment gives. They are
PICKED_DASHBOARD_ORIGINS, or, when it is not set, the origins in the
app config of Picked. With no origins,
page sends frame-ancestors 'none', and the dashboard cannot show the page.
import { readFile } from 'node:fs/promises'
import { createAppHandler, json, page, readAppEnvironment } from '@picked/app-kit/server'
import { frameScript, serve } from '@picked/app-kit/node'
const port = process.env.APP_PORT ?? '4103'
const { pickedKeys, dashboardOrigins } = readAppEnvironment(process.env, port)
const handler = createAppHandler({
pickedKeys,
async fallback(request) {
const { pathname } = new URL(request.url)
if (pathname === '/') {
const html = await readFile(new URL('./home.html', import.meta.url), 'utf8')
return page(html, { frameAncestors: await dashboardOrigins() })
}
if (pathname === '/app-kit/frame.js') return frameScript()
return json({ error: 'not_found' }, 404)
},
})
serve(handler, Number(port))createAppHandler sends a request for a path that it does not route to fallback. Therefore,
your pages and the requests from Picked use one server.
App environment variables gives the value of
PICKED_DASHBOARD_ORIGINS for each Picked environment, and the value when you do not set it.
3. Serve the script of @picked/app-kit/frame
The page needs the browser build of @picked/app-kit/frame. Serve it in one of these ways:
-
From your Node server. Answer
/app-kit/frame.jswithframeScript()from@picked/app-kit/node, as in step 2. -
As a static file. Add
app-kit-copy-frame <dir>to the build script of your app. It copies the script to<dir>/app-kit/frame.js. Use the directory that your host serves as static files:package.json{ "scripts": { "build": "app-kit-copy-frame public" } } -
In a bundle. If a bundler builds the script of your page, import from
@picked/app-kit/framein place of/app-kit/frame.js.
4. Connect the page to the dashboard
In the page, call connect() when the page loads. It resolves with the context of the page. Then call autoResize(). It keeps the height of the iframe equal to the height of
your content.
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8" />
<style>
body {
font:
14px system-ui,
sans-serif;
margin: 0;
padding: 12px;
}
html[data-theme='dark'] body {
background: #111;
color: #eee;
}
</style>
</head>
<body>
<p id="status">Loading…</p>
<script type="module">
import { autoResize, connect } from '/app-kit/frame.js'
const context = await connect()
document.documentElement.dataset.theme = context.theme
autoResize()
const response = await fetch('/api/me', {
headers: { authorization: `Bearer ${context.sessionToken}` },
})
const body = await response.json()
document.getElementById('status').textContent = response.ok
? `Project ${body.projectId}, user ${body.userId}`
: 'Could not read your session'
</script>
</body>
</html>Use these fields of the context:
theme.lightordark. Show the page in the color scheme of the dashboard.location. Thelocationof the slot that shows the page.projectId. The project that has the installation.sessionToken. A token for your server. Step 5 shows how to use it.
Messages between the page and the dashboard gives each field of the context.
To show a different page for each slot, give each slot a different url. You can also read
context.location.
connect() works only in the iframe of the dashboard. In a page that is not in an
iframe, it returns an error. It accepts messages only from the window that contains the
iframe, and only from the origin of the dashboard. It reads that origin from
document.referrer. If the browser does not send the referrer, give the origin:
connect({ dashboardOrigin: 'https://dashboard.picked.app' }).
5. Identify the person on your server
Send the session token to your server in the Authorization header, as in step 4. On the
server, verify it with readSession. Add this branch to fallback:
if (pathname === '/api/me') {
const session = await readSession(request, pickedKeys)
if (session === null) return json({ error: 'unauthenticated' }, 401)
return json({ userId: session.userId, projectId: session.projectId })
}Import readSession from @picked/app-kit/server. It returns null when the request has
no token, or when the token is not from Picked or is expired. Otherwise it returns the
fields of the token, for example userId, installationId and projectId.
Session tokens gives each field.
A session token expires after 10 minutes. On a page that stays open, use
keepConnected: it gets a new token before the
old token expires.
Do not use the IDs in the URL or in the body of a request from the page. Use the IDs in the session.
To call the Picked API for the person who uses the page, give sessionTokenOf(request) to
the installation client.
How to read project data from an app
shows how.
6. Deploy and open the page
- Deploy a new version of the manifest.
- Upgrade the installation to that version.
- Open the page.
If the project does not have the app, install it in place of step 2. Picked shows the slots of the version that the installation uses.
Where you open the page depends on its location:
app-home. In the project, open Apps. On the tab Installed, choose Open on the app. The name of the app is the heading of the page. When the app has oneapp-homeslot, theiframefills the page, and the page ignores its height.workspace-prepublish-actions. Publish a workspace. The page shows in the publish modal, above the Publish button.workspace-postpublish-actions. Publish a workspace. The page shows in the modal after Picked publishes the workspace.
If two or more apps have a slot at one location, the dashboard shows all of them. In the
publish modal, the title of the slot is the heading above the page. Without a title, the
heading is the name of the app.
If the dashboard cannot show the page, it shows one of these messages:
| Message | Cause |
|---|---|
The error from Picked, or Could not start the app | The dashboard did not get a session token for the page. |
This app's page can't be shown: it is served from the dashboard's own address. | The url of the slot has the origin of the dashboard. |
This app's page can't be shown: its address is not a valid URL. | The url of the slot is not a valid URL. |
If the iframe stays empty, look at the console of the browser. A frame-ancestors error
tells you that the dashboard origins of the app do not have the origin of the dashboard.
Related
- App manifest — every field of a slot.
@picked/app-kit—page,readSession,connectandautoResize.- App protocol — the messages between the page and the dashboard, and the session token.
- How to read project data from an app — call Picked from a page request.
- How to connect a service with OAuth — open an authorization window from a page.
- How to upgrade or uninstall an app — move an installation to a new version.
