Skip to content
18 changes: 18 additions & 0 deletions .changeset/36372-dashboard-in-page-edit.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
---
'@object-ui/core': patch
'@object-ui/i18n': patch
'@object-ui/plugin-dashboard': patch
'@object-ui/app-shell': patch
---

feat(console): a dashboard is edited on its own page by the people who may design its package, and every card marks the page filters it does not follow

Clause-②: yes (widening) — one new exported function (`dashboardFiltersNotApplied` in `@object-ui/core`), two new optional `DashboardRenderer` props (`onWidgetResize`, `onAddFilter`), and new `@object-ui/i18n` keys (`dashboard.inPageEdit.*`, `dashboard.filters.notApplied`, `dashboard.filters.addFilter`); nothing is removed or renamed
Metadata-protocol: none

A dashboard page draws an **Edit** button for a workspace admin who may author metadata, on a package Studio lists (the gate of the app's Studio entries). Edit opens the dashboard as stored (never the translated copy the page shows) in an edit state: the header says **Editing**, with **Add card**, **Discard** and **Save**; every card draws a dashed outline and can be dragged to move it, resized from its corner by whole grid tracks (or with the arrow keys on the corner handle) and clicked to select it. The panel at the right is Studio's own dashboard panels, as they stand: a selected card's (its data, its "follow page filters" switches, Remove) and otherwise the dashboard's (label, cards, filters, layout). The filter bar's **Add filter** opens the panel's Filters section. Add card appends a card at the end and selects it.

Save writes where Studio's Customize would and publishes that one item in the same act, so everyone sees the change at once without the app's package being published: into the owning package when it is writable, else into the app's customization package (`<base>.custom`, declaring `overrides: { package, basedOn }`), so an installed app's dashboard is edited too. Opening the edit state writes nothing: the pair is asked of the ensure door, which creates it on first use, at Save only. As with Studio's Customize, an installed app's dashboard is not opened where that door is walled for the caller or the package list gives no installed version to declare the override at. A dashboard with a draft already pending there (a Studio edit not yet published) is not opened for editing, and a save over one writes nothing: that draft is never published on its saver's behalf. Discard drops every change and writes nothing.

Every card, in every mode, carries a small mark naming the page filters that do not reach it ("Not filtered by Region"), asked of the same binding its query is built from (`dashboardFiltersNotApplied` over `resolveBoundField`): a filter the card opts out of, or one whose `targetWidgets` leaves it out (an explicit string binding wins over `targetWidgets`). A card every filter reaches carries none.

31 changes: 31 additions & 0 deletions content/docs/frontend/plugins/plugin-dashboard.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -143,6 +143,37 @@ request draws it, and a widget re-reads on its own when a write invalidates the
object it reads. A query asked alone, and every query of an adapter without
`queryDatasetBatch`, takes `queryDataset` unchanged.

## Filters a widget does not follow

Every widget, in every mode, carries a small mark naming the dashboard
filters that do not reach it, for example "Not filtered by Region": a filter
it opts out of (`filterBindings[name]: false`) or one whose `targetWidgets`
leaves it out. An explicit string binding (`filterBindings[name]: 'field'`)
wins over `targetWidgets`, so a widget that names its field is reached even
when the list leaves it out. A widget every filter reaches carries none. The list is
`dashboardFiltersNotApplied(widget, defs)` from `@object-ui/core`, which asks
`resolveBoundField`, the binding the widget's query is built from, so the
mark and the data cannot disagree.

## Design mode: resizing and "Add filter"

In `designMode` two more affordances are drawn when the host hands their
handler:

- `onWidgetResize(widgetId, { w, h })`: a corner handle on each widget
resizes it by whole grid tracks (it snaps as the pointer crosses half a
track; the arrow keys on the handle grow or shrink it by one). Width stays
within the grid's columns. The host writes the span into the widget's
`layout`. Not drawn below the phone breakpoint.
- `onAddFilter()`: an "Add filter" button at the end of the filter bar, which
is drawn for it even before any filter is declared.

The console's dashboard page uses both in its in-page edit state: an admin
who may design the dashboard's package opens it with **Edit**, changes it with
Studio's own dashboard panels at the right, and **Save** publishes that one
dashboard (into the package, or into the app's customization package for an
installed app), so everyone sees it at once.

## TypeScript Support

```ts
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,8 @@ vi.mock('../../layout/ConsoleLayout', () => ({
ConsoleLayout: ({ children }: { children?: React.ReactNode }) => <div data-testid="console-layout">{children}</div>,
}));
vi.mock('../../chrome/CommandPalette', () => ({ CommandPalette: () => null }));
vi.mock('../../hooks/useStudioDesignGate', () => ({ useStudioDesignGate: () => null }));
// `useStudioEntryGate`: the dashboard page's Edit button reads it (objectstack#36372).
vi.mock('../../hooks/useStudioDesignGate', () => ({ useStudioDesignGate: () => null, useStudioEntryGate: () => null }));
vi.mock('../../chrome/KeyboardShortcutsDialog', () => ({ KeyboardShortcutsDialog: () => null }));
vi.mock('../../chrome/OnboardingWalkthrough', () => ({ OnboardingWalkthrough: () => null }));
vi.mock('@object-ui/i18n', async (importOriginal) => ({
Expand Down
153 changes: 153 additions & 0 deletions ui/packages/app-shell/src/views/DashboardInPageEditor.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,153 @@
/**
* ObjectUI
* Copyright (c) 2024-present ObjectStack Inc.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/

import { describe, it, expect, vi } from 'vitest';
// Paid during collection, not inside a case's clock: Studio's dashboard panels
// reach both through dynamic imports (check:test-source-alias).
import '@objectstack/lint';
import '@objectstack/formula';
import {
filtersSectionLabel,
openDashboardForEdit,
withAddedWidget,
withResizedWidget,
} from './DashboardInPageEditor';

/**
* objectstack#36372 — opening a dashboard for in-page editing, and the two
* buffer edits the page makes itself (add a card, resize a card). The rest of
* the buffer's edits are Studio's panels' own `onPatch`.
*/

const listed = (entries: Record<string, { writable: boolean; version?: string }>) => ({
listPackages: vi.fn(async () =>
new Map(Object.entries(entries).map(([id, e]) => [id, { writable: e.writable, manifest: { id, version: e.version } } as any])),
),
});
const doors = (writable: boolean) => listed({ crm: { writable, version: '3.0.0' } });

const stored = { name: 'exec', label: 'Exec', widgets: [{ id: 'a', type: 'bar' }] };

describe('openDashboardForEdit', () => {
it('opens the layered effective document of the owning package', async () => {
const client = {
getDraft: vi.fn(async () => null),
layered: vi.fn(async () => ({ code: { name: 'exec' }, effective: stored })),
};
await expect(openDashboardForEdit('exec', 'crm', client, { doors: doors(true) })).resolves.toEqual({
kind: 'ready',
target: { kind: 'package', packageId: 'crm' },
doc: stored,
});
expect(client.getDraft).toHaveBeenCalledWith('dashboard', 'exec', { packageId: 'crm' });
});

it('an installed app with no pair yet opens its base\'s document, reading no pair and creating none', async () => {
const client = {
getDraft: vi.fn(async () => null),
layered: vi.fn(async () => ({ code: stored, effective: stored })),
};
// The doors carry no ensure door at all: opening could not create the pair if it tried.
const opened = await openDashboardForEdit('exec', 'crm', client, { doors: doors(false) });
expect(opened).toEqual({ kind: 'ready', target: { kind: 'customization', base: 'crm', basedOn: '3.0.0', pair: null }, doc: stored });
expect(client.getDraft).not.toHaveBeenCalled();
expect(client.layered).toHaveBeenCalledTimes(1);
expect(client.layered).toHaveBeenCalledWith('dashboard', 'exec', { packageId: 'crm' });
});

it('into a listed pair that does not hold it yet, opens its base\'s document', async () => {
const client = {
getDraft: vi.fn(async () => null),
layered: vi.fn(async (_t: string, _n: string, o?: { packageId?: string }) =>
o?.packageId === 'crm' ? { code: stored, effective: stored } : { code: null, effective: null },
),
};
const pairListed = listed({ crm: { writable: false, version: '3.0.0' }, 'crm.custom': { writable: true, version: '0.0.0' } });
const opened = await openDashboardForEdit('exec', 'crm', client, { doors: pairListed });
expect(opened).toMatchObject({ kind: 'ready', target: { kind: 'customization', base: 'crm', pair: 'crm.custom' }, doc: stored });
expect(client.getDraft).toHaveBeenCalledWith('dashboard', 'exec', { packageId: 'crm.custom' });
});

it('is refused for an installed app where Studio\'s Customize is: the ensure door walled, or no version to declare', async () => {
const client = { getDraft: vi.fn(async () => null), layered: vi.fn(async () => ({ code: stored, effective: stored })) };
await expect(openDashboardForEdit('exec', 'crm', client, { doors: doors(false), walled: true })).resolves.toEqual({
kind: 'refused',
reason: 'walled',
});
expect(client.layered).not.toHaveBeenCalled();
// A writable package is not customized, so the wall does not apply to it.
await expect(openDashboardForEdit('exec', 'crm', client, { doors: doors(true), walled: true })).resolves.toMatchObject({ kind: 'ready' });
await expect(
openDashboardForEdit('exec', 'crm', client, { doors: listed({ crm: { writable: false } }) }),
).resolves.toMatchObject({ kind: 'refused', reason: 'no-version' });
// An item the pair already declares an override of keeps that declaration: no listed version is owed.
const customized = { ...stored, overrides: { package: 'crm', basedOn: '2.0.0' } };
const inPair = {
getDraft: vi.fn(async () => null),
layered: vi.fn(async () => ({ code: null, effective: customized })),
};
await expect(
openDashboardForEdit('exec', 'crm', inPair, {
doors: listed({ crm: { writable: false }, 'crm.custom': { writable: true } }),
}),
).resolves.toMatchObject({ kind: 'ready', doc: customized });
});

it('is refused over a pending draft, without reading the document', async () => {
const client = {
getDraft: vi.fn(async () => ({ item: { name: 'exec' } })),
layered: vi.fn(),
};
await expect(openDashboardForEdit('exec', 'crm', client, { doors: doors(true) })).resolves.toEqual({
kind: 'refused',
reason: 'pending-draft',
});
expect(client.layered).not.toHaveBeenCalled();
});

it('is refused when there is no package to save into, and when the read fails', async () => {
const client = { getDraft: vi.fn(async () => null), layered: vi.fn(async () => ({ code: null, effective: null })) };
await expect(openDashboardForEdit('exec', 'gone', client, { doors: doors(true) })).resolves.toMatchObject({
kind: 'refused',
reason: 'no-package',
});
await expect(openDashboardForEdit('exec', 'crm', client, { doors: doors(true) })).resolves.toMatchObject({
kind: 'refused',
reason: 'load-failed',
});
const failing = { getDraft: vi.fn(async () => { throw new Error('500'); }), layered: vi.fn() };
await expect(openDashboardForEdit('exec', 'crm', failing, { doors: doors(true) })).resolves.toEqual({
kind: 'refused',
reason: 'load-failed',
detail: '500',
});
});
});

describe('the page\'s own buffer edits', () => {
it('adds a card at the end with a fresh id', () => {
const added = withAddedWidget({ ...stored, widgets: [{ id: 'widget_1', type: 'bar' }] }, 'pie');
expect(added.id).toBe('widget_2');
const widgets = added.doc.widgets as Array<{ id: string; type: string }>;
expect(widgets.map((w) => w.id)).toEqual(['widget_1', 'widget_2']);
expect(widgets[1].type).toBe('pie');
});

it('resizes a card, keeping the coordinates the span does not name', () => {
const doc = { widgets: [{ id: 'a', type: 'bar', layout: { x: 6, y: 2, w: 3, h: 4 } }, { id: 'b', type: 'bar' }] };
expect((withResizedWidget(doc, 'a', { w: 4, h: 2 }).widgets as any[])[0].layout).toEqual({ x: 6, y: 2, w: 4, h: 2 });
// A card with no layout gets the grid's auto placement for its index.
expect((withResizedWidget(doc, 'b', { w: 2, h: 1 }).widgets as any[])[1].layout).toEqual({ x: 3, y: 0, w: 2, h: 1 });
// An unknown id changes nothing.
expect(withResizedWidget(doc, 'zz', { w: 2, h: 1 })).toBe(doc);
});

it('finds the panel section that edits the page filters', () => {
expect(filtersSectionLabel('en-US')).toBe('Filters');
});
});
Loading
Loading