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
17 changes: 17 additions & 0 deletions .changeset/36011-restore-factory-view.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
---
'@object-ui/app-shell': patch
'@object-ui/plugin-view': patch
'@object-ui/data-objectstack': patch
'@object-ui/i18n': patch
---

feat(console): Manage views restores the factory version of a customized package view

Clause-②: yes (widening)
Metadata-protocol: none

A pin, a rename or a set-default on a view a read-only package ships writes the whole view into the package's customization (`<package>.custom`), and every user of the view then sees that copy. Manage views now marks such a view "customized" and offers "Restore factory version" in its `…` menu. After one confirm, which says every user of the view sees the factory version again, the override is removed from the customization and the list shows the package's own view. A view without a customization has no mark and offers no restore; a refused restore is said with the server's own message.

- `@object-ui/data-objectstack`: `ObjectStackAdapter.restoreFactoryView(objectName, viewName)` deletes the view's draft, then its row, at the customization the ensure door names for the shipping package (`DELETE /meta/view/NAME?package=PAIR`, the door Studio's "Restore package original" uses). It refuses, sending nothing, a view that is no override of the package it is served under, and throws when the customization held no row of the view. The customization package itself stays. `listViews()` now carries a record's `overrides` declaration onto the flattened row.
- `@object-ui/plugin-view`: `ViewTabItem.customized` and `ManageViewsDialogProps.onRestoreFactory`.
- `@object-ui/i18n`: the mark, the menu entry, the confirm, its result and the failure title, in every locale pack.
Original file line number Diff line number Diff line change
@@ -0,0 +1,295 @@
// Copyright (c) 2026 ObjectStack. Licensed under the Apache-2.0 license.

/**
* objectstack#36011 — Manage views restores the factory version of a view a
* read-only package ships, after a pin, a rename or a set-default wrote the
* view into the package's customization pair (ruling record 7058654416 keeps
* those three on such a view).
*
* A row the list serves with the override declaration naming the package it
* is served under (`overrides`, ADR-0146 D2) is marked "customized" and
* offers "Restore factory version". One confirm says every user of the view
* sees the factory version again; on confirm the host calls the adapter's
* `restoreFactoryView`, reads the views again and shows the package's own
* view. A view without a customization offers no restore. A refused restore
* says why, with the door's message.
*
* What runs: the real `ObjectView` and the real `ManageViewsDialog` (held
* open), under the real English `I18nProvider`, on a stub data source whose
* list answers what the server stores: the pin writes the override, the
* restore removes it. `ListView` and the view panel are stand-ins.
*/

import * as React from 'react';
import { describe, it, expect, vi, beforeEach, afterEach } from 'vitest';
import { render, cleanup, act, waitFor, screen, fireEvent, within } from '@testing-library/react';
import { toast } from 'sonner';
import { I18nProvider } from '@object-ui/i18n';
import { MemoryRouter, Routes, Route } from 'react-router-dom';
// `ObjectView` reaches a dynamic `import('@objectstack/lint')`: pay its load at
// collection, never inside a case's clocked window (`check:test-source-alias`).
import '@objectstack/lint';

vi.mock('@object-ui/permissions', async (importOriginal) => {
const actual = await importOriginal<typeof import('@object-ui/permissions')>();
// Stable identities: `ListView` names `perms` in its fetch dependencies.
const perms = {
check: () => ({ allowed: true }),
checkField: () => true,
getFieldPermissions: () => [],
getRowFilter: () => undefined,
getObjectApiOperations: () => undefined,
roles: [],
isLoaded: false,
hasCapabilities: () => true,
can: () => true,
cannot: () => false,
};
const fieldPerms = { canRead: () => true, canWrite: () => true, permissions: [] };
return { ...actual, usePermissions: () => perms, useFieldPermissions: () => fieldPerms };
});

vi.mock('@object-ui/auth', async (importOriginal) => ({
...(await importOriginal<Record<string, unknown>>()),
useAuth: () => ({ user: { id: 'u1', name: 'Ada' }, activeOrganization: { id: 'org1', name: 'Acme' } }),
// Manage views and the view panel are an administrator's.
useWorkspaceAdminStatus: () => ({ isAdmin: true, isResolved: true }),
createAuthenticatedFetch: () => vi.fn(),
}));

vi.mock('@object-ui/collaboration', async (importOriginal) => ({
...(await importOriginal<Record<string, unknown>>()),
useRealtimeSubscription: () => ({ lastMessage: null }),
useConflictResolution: () => ({ hasConflicts: false, resolveAllConflicts: () => {} }),
}));

vi.mock('sonner', () => ({
toast: Object.assign(vi.fn(), {
success: vi.fn(), error: vi.fn(), info: vi.fn(),
warning: vi.fn(), loading: vi.fn(), dismiss: vi.fn(),
}),
}));

vi.mock('./MetadataInspector', () => ({
MetadataPanel: () => null,
useMetadataInspector: () => ({ showDebug: false, toggle: () => {} }),
}));
vi.mock('./RecordDetailView', () => ({ RecordDetailView: () => null }));

vi.mock('@object-ui/plugin-list', async (importOriginal) => ({
...(await importOriginal<typeof import('@object-ui/plugin-list')>()),
ListView: () => null,
}));

// The real dialog, held open: the host opens it from the title switcher.
vi.mock('@object-ui/plugin-view', async (importOriginal) => {
const actual = await importOriginal<typeof import('@object-ui/plugin-view')>();
const Dialog = actual.ManageViewsDialog;
return {
...actual,
ManageViewsDialog: (props: React.ComponentProps<typeof Dialog>) => (
<Dialog {...props} open onOpenChange={() => {}} />
),
};
});

vi.mock('./ViewConfigPanel', () => ({ ViewConfigPanel: () => null }));

/** The session-held package list, as `GET /api/v1/packages` answers it (parsed). */
const packageList = vi.hoisted(() => ({ answer: (): Promise<any[]> => Promise.resolve([]) }));
vi.mock('./studio-design/packages-io', async (importOriginal) => ({
...(await importOriginal<Record<string, unknown>>()),
fetchSessionPackages: vi.fn(() => packageList.answer()),
}));

import { ObjectView } from './ObjectView';
import { ExpressionProvider } from '../providers/ExpressionProvider';

const OBJECT = 'showcase_task';
const BASE = 'com.example.showcase';
const PAIR = 'com.example.showcase.custom';
const VIEW = `${OBJECT}.in_progress`;
const OTHER = `${OBJECT}.done`;

/** The package's own view, as `listViews()` serves it. */
const factory = (name: string, label: string) => ({
name, object: OBJECT, label, type: 'grid', columns: ['name', 'amount'], _packageId: BASE,
});

const PACKAGES = [
{ id: BASE, name: 'Showcase', writable: false, namespace: 'showcase' },
{ id: PAIR, name: 'Showcase (custom)', writable: true, namespace: 'showcase' },
];

/**
* The stored state behind the stub: the pair's override of VIEW, or none.
* `updateView` writes the override (served under the base, declaring it);
* `restoreFactoryView` removes it, or throws `refusal` when one is set.
*/
function makeDataSource(opts: { refusal?: Error } = {}) {
let override: Record<string, unknown> | null = null;
const dataSource = {
find: vi.fn(async () => ({ data: [], total: 0 })),
findOne: vi.fn(async () => null),
create: vi.fn(async () => ({})),
update: vi.fn(async () => ({})),
delete: vi.fn(async () => ({})),
getObjectSchema: vi.fn(async () => ({ name: OBJECT, fields: {} })),
listViews: vi.fn(async () => [override ?? factory(VIEW, 'In progress'), factory(OTHER, 'Done')]),
updateView: vi.fn(async (_object: string, name: string, patch: Record<string, unknown>) => {
if (name !== VIEW) throw new Error('only VIEW is customized in this test');
override = { ...factory(VIEW, 'In progress'), ...(override ?? {}), ...patch, overrides: { package: BASE, basedOn: '0.1.0' } };
return override;
}),
restoreFactoryView: vi.fn(async () => {
if (opts.refusal) throw opts.refusal;
override = null;
return { deleted: true };
}),
};
return dataSource;
}

function mount(dataSource: ReturnType<typeof makeDataSource>) {
render(
<I18nProvider config={{ defaultLanguage: 'en', detectBrowserLanguage: false }}>
<ExpressionProvider user={{ id: 'u1', name: 'Ada', profile: 'admin' }}>
<MemoryRouter initialEntries={[`/apps/demo/${OBJECT}/view/${encodeURIComponent(OTHER)}`]}>
<Routes>
<Route
path="/apps/:appName/:objectName/view/:viewId"
element={
<ObjectView
dataSource={dataSource as never}
objects={[{
name: OBJECT,
label: 'Task',
fields: { name: { type: 'text', label: 'Name' }, amount: { type: 'number', label: 'Amount' } },
}]}
onEdit={() => {}}
/>
}
/>
</Routes>
</MemoryRouter>
</ExpressionProvider>
</I18nProvider>,
);
}

const settle = () => act(() => new Promise<void>((resolve) => setTimeout(resolve, 300)));

async function openMenu(id: string) {
const trigger = await waitFor(() => screen.getByTestId(`manage-views-actions-${id}`));
fireEvent.pointerDown(trigger, { button: 0, ctrlKey: false });
fireEvent.click(trigger);
await waitFor(() => expect(screen.getByRole('menu')).toBeTruthy());
}
async function closeMenu() {
fireEvent.keyDown(screen.getByRole('menu'), { key: 'Escape' });
await waitFor(() => expect(screen.queryByRole('menu')).toBeNull());
}
/** Whether the row offers a restore, read by opening its menu. */
async function offersRestore(id: string): Promise<boolean> {
await openMenu(id);
const offered = screen.queryByTestId(`manage-views-action-restore-${id}`) !== null;
await closeMenu();
return offered;
}
const marked = (id: string) => screen.queryByTestId(`manage-views-customized-${id}`) !== null;

/** Pin VIEW from its row: the write that creates the override. */
async function pin() {
await waitFor(async () => expect((await rowReady(VIEW))).toBe(true));
fireEvent.click(screen.getByTestId(`manage-views-pin-${VIEW}`));
}
async function rowReady(id: string) {
// The row is a read-only package's once its configuration reads "View config".
await openMenu(id);
const label = screen.queryByTestId(`manage-views-action-config-${id}`)?.textContent?.trim();
await closeMenu();
return label === 'View config';
}

/** Click the row's restore, and answer the confirm (`true` confirms). */
async function restore(confirm: boolean) {
await openMenu(VIEW);
fireEvent.click(screen.getByTestId(`manage-views-action-restore-${VIEW}`));
const dialog = await screen.findByRole('alertdialog');
const text = dialog.textContent ?? '';
const button = within(dialog).getByRole('button', { name: confirm ? 'Restore' : 'Cancel' });
await act(async () => { fireEvent.click(button); });
return text;
}

beforeEach(() => {
packageList.answer = () => Promise.resolve(PACKAGES);
localStorage.clear();
vi.mocked(toast.error).mockClear();
vi.mocked(toast.success).mockClear();
});

afterEach(() => {
cleanup();
});

describe('objectstack#36011 — restore the factory version of a customized package view', () => {
it('pin a factory view, then restore it: the override is removed and the list shows the package\'s own view', async () => {
const dataSource = makeDataSource();
mount(dataSource);
await settle();
// Before: the package's own view, unmarked, no restore.
await waitFor(async () => expect(await rowReady(VIEW)).toBe(true));
expect(marked(VIEW)).toBe(false);
expect(await offersRestore(VIEW)).toBe(false);

await pin();
expect(dataSource.updateView).toHaveBeenCalledWith(OBJECT, VIEW, { isPinned: true });
await waitFor(() => expect(marked(VIEW)).toBe(true));
expect(await offersRestore(VIEW)).toBe(true);
// The uncustomized view beside it offers none.
expect(marked(OTHER)).toBe(false);
expect(await offersRestore(OTHER)).toBe(false);

const said = await restore(true);
expect(said).toContain('Everyone who uses this view will see the factory version again');
expect(said).toContain('In progress');
await waitFor(() => expect(dataSource.restoreFactoryView).toHaveBeenCalledWith(OBJECT, VIEW));
// The views are read again: the factory view, unmarked and unpinned.
await waitFor(() => expect(marked(VIEW)).toBe(false));
expect(screen.getByTestId(`manage-views-pin-${VIEW}`).getAttribute('aria-label')).toBe('Pin View');
expect(await offersRestore(VIEW)).toBe(false);
expect(toast.success).toHaveBeenCalledTimes(1);
expect(toast.error).not.toHaveBeenCalled();
});

it('a cancelled confirm removes nothing', async () => {
const dataSource = makeDataSource();
mount(dataSource);
await settle();
await pin();
await waitFor(() => expect(marked(VIEW)).toBe(true));

await restore(false);
await settle();
expect(dataSource.restoreFactoryView).not.toHaveBeenCalled();
expect(marked(VIEW)).toBe(true);
});

it('a refused restore says why, with the door\'s message, and the view stays customized', async () => {
const reason = 'Deleting metadata requires the Manage Metadata permission';
const dataSource = makeDataSource({ refusal: Object.assign(new Error(reason), { status: 403, code: 'PERMISSION_DENIED' }) });
mount(dataSource);
await settle();
await pin();
await waitFor(() => expect(marked(VIEW)).toBe(true));

await restore(true);
await waitFor(() => expect(toast.error).toHaveBeenCalledTimes(1));
const [title, options] = vi.mocked(toast.error).mock.calls[0] as [string, { description?: string }];
expect(title).toBe('Failed to restore the factory version');
expect(options.description).toContain(reason);
expect(toast.success).not.toHaveBeenCalled();
expect(marked(VIEW)).toBe(true);
});
});
2 changes: 2 additions & 0 deletions ui/packages/app-shell/src/views/ObjectView.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -826,6 +826,7 @@ function ObjectViewInner({ dataSource, objects, onEdit, externalRefreshKey }: Co
handleViewAction,
handleRenameView,
handleDeleteView,
handleRestoreFactoryView,
handlePinView,
handleSetDefaultView,
handleReorderViews,
Expand Down Expand Up @@ -1552,6 +1553,7 @@ function ObjectViewInner({ dataSource, objects, onEdit, externalRefreshKey }: Co
onReorder={handleReorderViews}
onAddView={handleAddView}
onConfigView={handleConfigViewFromRow}
onRestoreFactory={(id) => { void handleRestoreFactoryView(id); }}
/>
)}

Expand Down
10 changes: 8 additions & 2 deletions ui/packages/app-shell/src/views/objectViewTitleSwitcher.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -432,8 +432,14 @@ export function useObjectViewTitleSwitcher({
.filter((item) => !personalViewIds.has(item.id))
.map((item) => {
if (item.readonly) return item;
const packageId = studioPackageId(savedViews.find((sv: any) => viewRowId(sv) === item.id));
return packageId !== null && refusingPackages.has(packageId) ? { ...item, definitionReadonly: true } : item;
const saved = savedViews.find((sv: any) => viewRowId(sv) === item.id);
const packageId = studioPackageId(saved);
if (packageId === null || !refusingPackages.has(packageId)) return item;
// objectstack#36011 — customized: the row is served with the
// override declaration naming that package (ADR-0146 D2), the one
// `updateView` writes into the package's customization.
const customized = saved?.overrides?.package === packageId;
return { ...item, definitionReadonly: true, ...(customized ? { customized: true } : {}) };
});
const switcherViews: TitleSwitcherView[] = viewItems.map((item) => {
const personal = personalViewIds.has(item.id);
Expand Down
36 changes: 36 additions & 0 deletions ui/packages/app-shell/src/views/objectViewViewCrud.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -414,6 +414,41 @@ export function useObjectViewViewCrud({
}
}, [dataSource, isSavedView, activeViewId, views, viewId, navigate, t, confirmHandler]);

/**
* objectstack#36011 — "Restore factory version" on a customized view of a
* read-only package (`ViewTabItem.customized`): after one confirm saying
* every user of the view sees the factory version again, the override
* the package's customization holds is removed
* (`restoreFactoryView`), and the views are read again. A refusal is
* said with the door's own message.
*/
const handleRestoreFactoryView = useCallback(async (vid: string) => {
if (!dataSource || !objectName) return;
if (typeof (dataSource as any)?.restoreFactoryView !== 'function') return;
const targetView = views.find((v: any) => v.id === vid);
const viewLabel = targetView?.label || vid;
const confirmed = await confirmHandler(
t('console.objectView.restoreFactoryConfirm', { name: viewLabel }),
{
title: t('console.objectView.restoreFactoryTitle'),
confirmText: t('console.objectView.restoreFactory'),
cancelText: t('console.objectView.cancel'),
},
);
if (!confirmed) return;
try {
await (dataSource as any).restoreFactoryView(objectName, vid);
setRefreshKey(k => k + 1);
toast.success(t('console.objectView.restoreFactoryDone', { name: viewLabel }));
} catch (err) {
console.error('[ManageViews] Failed to restore the factory view:', err);
toast.error(t('objectViewActions.restoreFactoryFailed'), {
description: formatMetadataError(err),
classNames: { description: 'whitespace-pre-line' },
});
}
}, [dataSource, objectName, views, t, confirmHandler, setRefreshKey]);

const handlePinView = useCallback(async (vid: string, pinned: boolean) => {
if (!dataSource) return;
if (!isSavedView(vid)) {
Expand Down Expand Up @@ -509,6 +544,7 @@ export function useObjectViewViewCrud({
handleViewAction,
handleRenameView,
handleDeleteView,
handleRestoreFactoryView,
handlePinView,
handleSetDefaultView,
handleReorderViews,
Expand Down
Loading
Loading