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
12 changes: 12 additions & 0 deletions .changeset/37031-dashboard-binding-recheck.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
---
'@object-ui/app-shell': patch
---

fix(console): re-ticking a dashboard card's "Apply" for a page filter restores the field the app binds it to, so an opt-out can be undone

Clause-②: no
Metadata-protocol: none

In the dashboard's own edit state (and in Studio's dashboard editor), unticking "Apply" under a card's "Follows the filters at the top of the page" writes the card's opt-out (`filterBindings: { NAME: false }`). Ticking it again used to drop the entry, which makes the card follow the filter's own field rather than the field the app binds it to. A dashboard save stores the whole document, so the re-target the app ships (for example `dateRange: 'signed_on'` on a card whose dataset has no `issued_on`) was lost: the save then bound the card to a field its dataset lacks, the publish was refused (`dashboard-filter-field-unknown`), and the opt-out stayed in force with no way to undo it from the editor.

Re-ticking now restores the binding the dashboard as shipped by its package gives that card (the layered read's `code` layer) when it re-targets the filter to a field; where the shipped card opts out too, or binds nothing, the entry is dropped as before. The edit state reads the shipped layer in the same layered read it opens the document from, and Studio's editor hands it to the card panel from the read it already makes. Nothing else a save writes changes, and no new strings.
35 changes: 34 additions & 1 deletion ui/packages/app-shell/src/views/DashboardInPageEditor.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ describe('openDashboardForEdit', () => {
kind: 'ready',
target: { kind: 'package', packageId: 'crm' },
doc: stored,
shipped: { name: 'exec' },
});
expect(client.getDraft).toHaveBeenCalledWith('dashboard', 'exec', { packageId: 'crm' });
});
Expand All @@ -54,7 +55,12 @@ describe('openDashboardForEdit', () => {
};
// 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(opened).toEqual({
kind: 'ready',
target: { kind: 'customization', base: 'crm', basedOn: '3.0.0', pair: null },
doc: stored,
shipped: stored,
});
expect(client.getDraft).not.toHaveBeenCalled();
expect(client.layered).toHaveBeenCalledTimes(1);
expect(client.layered).toHaveBeenCalledWith('dashboard', 'exec', { packageId: 'crm' });
Expand All @@ -73,6 +79,33 @@ describe('openDashboardForEdit', () => {
expect(client.getDraft).toHaveBeenCalledWith('dashboard', 'exec', { packageId: 'crm.custom' });
});

it('a pair that holds a customization opens it, with the base as shipped beside it', async () => {
// objectstack#37031: the panels restore an undone opt-out from `shipped`.
const shippedDoc = { ...stored, widgets: [{ id: 'a', type: 'bar', filterBindings: { dateRange: 'signed_on' } }] };
const customized = {
...stored,
overrides: { package: 'crm', basedOn: '3.0.0' },
widgets: [{ id: 'a', type: 'bar', filterBindings: { dateRange: false } }],
};
const client = {
getDraft: vi.fn(async () => null),
layered: vi.fn(async () => ({ code: shippedDoc, effective: customized })),
};
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', doc: customized, shipped: shippedDoc });
expect(client.layered).toHaveBeenCalledTimes(1);
expect(client.layered).toHaveBeenCalledWith('dashboard', 'exec', { packageId: 'crm.custom' });
});

it('an item no package ships opens with nothing shipped', async () => {
const client = { getDraft: vi.fn(async () => null), layered: vi.fn(async () => ({ code: null, effective: stored })) };
await expect(openDashboardForEdit('exec', 'crm', client, { doors: doors(true) })).resolves.toMatchObject({
kind: 'ready',
shipped: null,
});
});

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({
Expand Down
35 changes: 28 additions & 7 deletions ui/packages/app-shell/src/views/DashboardInPageEditor.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,13 @@ export type DashboardEditRefusal = 'no-package' | 'walled' | 'no-version' | 'pen

/** What opening a dashboard for editing found. */
export type DashboardEditOpening =
| { kind: 'ready'; target: WritableDashboardEditTarget; doc: Record<string, unknown> }
| {
kind: 'ready';
target: WritableDashboardEditTarget;
doc: Record<string, unknown>;
/** The dashboard as its package ships it (the layered read's `code`), `null` when none ships. */
shipped: Record<string, unknown> | null;
}
| { kind: 'refused'; reason: DashboardEditRefusal; detail?: string };

/** The metadata client calls {@link openDashboardForEdit} makes. */
Expand All @@ -80,9 +86,12 @@ export interface DashboardEditReader {
): Promise<{ code: unknown; effective: unknown }>;
}

function recordOf(value: unknown): Record<string, unknown> | null {
return value && typeof value === 'object' && !Array.isArray(value) ? (value as Record<string, unknown>) : null;
}

function authoredDoc(layers: { code: unknown; effective: unknown } | null): Record<string, unknown> | null {
const doc = layers ? (layers.effective ?? layers.code) : null;
return doc && typeof doc === 'object' && !Array.isArray(doc) ? (doc as Record<string, unknown>) : null;
return layers ? recordOf(layers.effective ?? layers.code) : null;
}

/** What {@link openDashboardForEdit} is told besides the item. */
Expand Down Expand Up @@ -128,16 +137,20 @@ export async function openDashboardForEdit(
if (at && unwrapDraftBody(await client.getDraft('dashboard', name, at))) {
return { kind: 'refused', reason: 'pending-draft' };
}
let doc = at ? authoredDoc(await client.layered('dashboard', name, at)) : null;
let layers = at ? await client.layered('dashboard', name, at) : null;
let doc = authoredDoc(layers);
if (!doc && target.kind === 'customization') {
doc = authoredDoc(await client.layered('dashboard', name, { packageId: target.base }));
layers = await client.layered('dashboard', name, { packageId: target.base });
doc = authoredDoc(layers);
}
if (!doc) return { kind: 'refused', reason: 'load-failed' };
// What the package ships, for the panels to restore an undone change from.
const shipped = recordOf(layers?.code);
// Studio's `noVersion`: no override declared yet, and no version to declare it at.
if (target.kind === 'customization' && dashboardSaveBody(doc, target) === null) {
return { kind: 'refused', reason: 'no-version', detail: target.base };
}
return { kind: 'ready', target, doc };
return { kind: 'ready', target, doc, shipped };
} catch (err) {
return { kind: 'refused', reason: 'load-failed', detail: err instanceof Error ? err.message : String(err) };
}
Expand Down Expand Up @@ -288,13 +301,16 @@ export function DashboardEditPanel({
onFiltersRevealed,
served,
published,
shipped = null,
}: {
name: string;
draft: Record<string, unknown>;
/** The dashboard as the page serves it, in the session's language. */
served?: Record<string, unknown> | null;
/** The stored document the edit state opened, before any change. */
published?: Record<string, unknown> | null;
/** The dashboard as its package ships it: an undone opt-out restores its filter binding. */
shipped?: Record<string, unknown> | null;
selection: MetadataSelection | null;
onPatch: (patch: Record<string, unknown>) => void;
onSelectionChange: (next: MetadataSelection | null) => void;
Expand Down Expand Up @@ -334,6 +350,7 @@ export function DashboardEditPanel({
type="dashboard"
name={name}
draft={draft}
shipped={shipped}
selection={selection}
onPatch={onPatch}
onClearSelection={() => onSelectionChange(null)}
Expand Down Expand Up @@ -370,6 +387,8 @@ export interface DashboardInPageEdit {
draft: Record<string, unknown> | null;
/** The stored document as the edit state opened it, before any change; `null` outside it. */
published: Record<string, unknown> | null;
/** The dashboard as its package ships it, `null` when none ships or outside the edit state. */
shipped: Record<string, unknown> | null;
opening: boolean;
saving: boolean;
dirty: boolean;
Expand Down Expand Up @@ -410,6 +429,7 @@ export function useDashboardInPageEdit({
target: WritableDashboardEditTarget;
draft: Record<string, unknown>;
published: Record<string, unknown>;
shipped: Record<string, unknown> | null;
dirty: boolean;
} | null>(null);
const [opening, setOpening] = React.useState(false);
Expand All @@ -434,7 +454,7 @@ export function useDashboardInPageEdit({
try {
const opened = await openDashboardForEdit(name, ownerPackageId, client, { doors, walled });
if (opened.kind === 'refused') return opened;
setSession({ target: opened.target, draft: opened.doc, published: opened.doc, dirty: false });
setSession({ target: opened.target, draft: opened.doc, published: opened.doc, shipped: opened.shipped, dirty: false });
setSelection(null);
return null;
} finally {
Expand Down Expand Up @@ -483,6 +503,7 @@ export function useDashboardInPageEdit({
return {
draft: session?.draft ?? null,
published: session?.published ?? null,
shipped: session?.shipped ?? null,
opening,
saving,
dirty: session?.dirty ?? false,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,7 @@ vi.mock('./metadata-admin/inspectors/DashboardWidgetInspector', async () => {
<div data-testid="stub-widget-inspector">
{p.selection.id}
<span data-testid="stub-widget-served-title">{pair ? `${pair.served} ← ${pair.authored}` : 'none'}</span>
<span data-testid="stub-widget-shipped">{JSON.stringify(p.shipped ?? null)}</span>
</div>
);
},
Expand Down Expand Up @@ -261,6 +262,25 @@ describe('DashboardView in-page edit (objectstack#36372)', () => {
expect(screen.getByTestId('stub-widget-served-title')).toHaveTextContent('Total (served) ← Total (stored)');
});

it('the card panel is handed the dashboard as its package ships it (objectstack#37031)', async () => {
packages.listed = new Map([
['crm', { id: 'crm', writable: false, manifest: { id: 'crm', version: '1.4.0' } }],
['crm.custom', { id: 'crm.custom', writable: true, manifest: { id: 'crm.custom', version: '0.0.0' } }],
]);
gate.entry = { path: '/studio/crm', posture: 'view' };
const shipped = { ...stored, widgets: [{ ...stored.widgets[0], filterBindings: { dateRange: 'signed_on' } }] };
const customized = {
...stored,
overrides: { package: 'crm', basedOn: '1.4.0' },
widgets: [{ ...stored.widgets[0], filterBindings: { dateRange: false } }],
};
client.value = makeClient({ layered: vi.fn(async () => ({ code: shipped, overlay: customized, effective: customized })) });
mount();
await openEdit();
fireEvent.click(screen.getByTestId('dashboard-preview-widget-widget_total'));
expect(JSON.parse(screen.getByTestId('stub-widget-shipped').textContent ?? 'null')).toEqual(shipped);
});

it('Add card appends a card and selects it', async () => {
mount();
await openEdit();
Expand Down
1 change: 1 addition & 0 deletions ui/packages/app-shell/src/views/DashboardView.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -549,6 +549,7 @@ export function DashboardView({ dataSource }: { dataSource?: any }) {
onFiltersRevealed={edit.filtersRevealed}
served={dashboard as Record<string, unknown> | undefined}
published={edit.published}
shipped={edit.shipped}
/>
)}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -127,9 +127,9 @@ function stubCanvas(selection: MetadataSelection) {
}

/** A host's inspector for a type that ships none: it draws what it is handed. */
function HostPositionInspector({ readOnly, readOnlyReason }: MetadataInspectorProps) {
function HostPositionInspector({ readOnly, readOnlyReason, shipped }: MetadataInspectorProps) {
return (
<div data-testid="host-position-inspector" data-readonly={String(readOnly)}>
<div data-testid="host-position-inspector" data-readonly={String(readOnly)} data-shipped={JSON.stringify(shipped ?? null)}>
{readOnlyReason ?? ''}
</div>
);
Expand Down Expand Up @@ -352,3 +352,25 @@ describe('the panel shown with nothing selected names the read-only cause too (o
expect(screen.queryByText(LOCK_REASON())).toBeNull();
});
});

/**
* objectstack#37031 — the page hands a scoped inspector the item as its
* package ships it (the layered read's `code`), so an inspector undoing a
* change (the dashboard card's re-ticked Apply) restores the shipped value.
*/
describe('the metadata editor page hands the scoped inspector the shipped layer (objectstack#37031)', () => {
it('a shipped item: the inspector is handed its code layer', async () => {
const shipped = { ...POSITION, label: 'Sales rep (shipped)' };
layered = { effective: POSITION, code: shipped, overlay: POSITION, editable: true, deletable: true };
await openAndSelect('position', 'sales_rep');
const inspector = await screen.findByTestId('host-position-inspector');
expect(JSON.parse(inspector.getAttribute('data-shipped') ?? 'undefined')).toEqual(shipped);
});

it('control: an item no package ships hands nothing', async () => {
layered = { effective: POSITION, code: null, overlay: POSITION, editable: true, deletable: true };
await openAndSelect('position', 'sales_rep');
const inspector = await screen.findByTestId('host-position-inspector');
expect(inspector.getAttribute('data-shipped')).toBe('null');
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -419,6 +419,15 @@ function readActivePackageBinding(): string | undefined {
}
}

/**
* The item as its package ships it (the layered read's `code`), handed to a
* scoped inspector so an undone change restores it (objectstack#37031).
*/
function shippedLayerOf(layered: MetadataLayered<unknown> | null): Record<string, unknown> | null {
const code = layered?.code;
return code && typeof code === 'object' && !Array.isArray(code) ? (code as Record<string, unknown>) : null;
}

/**
* Whether a layered answer this page already read holds no layer at all: no
* packaged baseline, no customisation row, no effective value. That is what
Expand Down Expand Up @@ -3854,6 +3863,7 @@ function MetadataResourceEditPageImpl({
type={type}
name={name}
draft={draft}
shipped={shippedLayerOf(layered)}
selection={selection}
onPatch={(patch) =>
handleDraftChange((d) => ({
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,13 @@ export interface MetadataInspectorProps {
name: string;
/** Current draft from the editor. Treat as immutable. */
draft: Record<string, unknown>;
/**
* The item as its package ships it (the layered read's `code`), when the
* host read one. An inspector that undoes an author's change restores the
* shipped value from it rather than a default the package never wrote.
* Omitted or `null`: there is nothing shipped to restore.
*/
shipped?: Record<string, unknown> | null;
/** The selection emitted by the matching preview. Never null here. */
selection: MetadataSelection;
/** Apply a shallow patch to the draft. */
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -286,6 +286,70 @@ describe('DashboardWidgetInspector — dashboard filter bindings (framework#2501
});
});

/**
* objectstack#37031 — re-ticking Apply undoes an opt-out. A dashboard save
* stores the whole document, so a binding the box drops is the filter's own
* field, not the field the package re-targeted it to: re-ticking restores the
* binding the shipped dashboard (`shipped`, the layered read's `code`) gives
* this widget, and only when it ships none does the entry go.
*/
describe('DashboardWidgetInspector — re-ticking Apply restores the shipped binding (objectstack#37031)', () => {
const filters = {
dateRange: { field: 'issued_on', defaultRange: 'last_90_days' },
globalFilters: [{ name: 'region', field: 'region', label: 'Region', type: 'select', options: [{ value: 'EMEA', label: 'EMEA' }] }],
};
const shippedWith = (filterBindings: Record<string, string | false> | undefined, id = 'w1') => ({
...filters,
widgets: [{ id, type: 'metric', title: 'Accounts Signed', ...(filterBindings ? { filterBindings } : {}) }],
});

function renderBound(filterBindings: Record<string, string | false>, shipped: Record<string, unknown> | null) {
const onPatch = vi.fn();
render(
<DashboardWidgetInspector
{...baseProps}
draft={{ ...filters, widgets: [widget({ filterBindings })] }}
shipped={shipped}
selection={{ kind: 'widget', id: 'w1' }}
onPatch={onPatch}
/>,
);
const box = within(screen.getByTestId('widget-filter-binding-dateRange')).getByRole('checkbox');
return { onPatch, box };
}

const shipped = shippedWith({ dateRange: 'signed_on', region: 'sales_region' });

it('unticking writes the opt-out over the shipped re-target', () => {
const { onPatch, box } = renderBound({ dateRange: 'signed_on', region: 'sales_region' }, shipped);
expect(box).toBeChecked();
fireEvent.click(box);
expect(onPatch).toHaveBeenCalledWith({
widgets: [expect.objectContaining({ id: 'w1', filterBindings: { dateRange: false, region: 'sales_region' } })],
});
});

it('re-ticking an opt-out restores the field the shipped dashboard binds', () => {
const { onPatch, box } = renderBound({ dateRange: false, region: 'sales_region' }, shipped);
expect(box).not.toBeChecked();
fireEvent.click(box);
expect(onPatch).toHaveBeenCalledWith({
widgets: [expect.objectContaining({ id: 'w1', filterBindings: { dateRange: 'signed_on', region: 'sales_region' } })],
});
});

it('control: where the shipped widget opts out too, or ships no binding, re-ticking removes the entry', () => {
for (const ships of [shippedWith({ dateRange: false }), shippedWith(undefined), shippedWith({ dateRange: 'signed_on' }, 'other')]) {
const { onPatch, box } = renderBound({ dateRange: false, region: 'sales_region' }, ships);
fireEvent.click(box);
expect(onPatch).toHaveBeenCalledWith({
widgets: [expect.objectContaining({ id: 'w1', filterBindings: { region: 'sales_region' } })],
});
cleanup();
}
});
});

/**
* The Title field's `I18nLabel` write-back (objectui#5428, under objectui#5301's
* maintainer ruling of 2026-08-20).
Expand Down
Loading
Loading