Skip to content
Merged
18 changes: 18 additions & 0 deletions .changeset/36286-palette-preview-create.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
---
'@object-ui/app-shell': patch
'@object-ui/i18n': patch
---

feat(console): the ⌘K palette previews the record it is on, creates a record from what you typed, finds "New OBJECT", and says what Enter does

Clause-②: yes (widening) — eight new published language-pack keys, in all eleven packs: console.commandPalette.create, createAs, previewEmpty, enterOpenRecord, enterCreate, enterSwitchApp, enterApplyTheme, enterOpen
Metadata-protocol: none

The command palette inside an app gains four things; which records it searches, and how it ranks them, are unchanged.

- **Record preview.** While the query finds records, the palette widens and shows the selected hit on the right, read-only: the object's label, the record's title and the object's declared key fields (`highlightFields`), drawn the way the record pointer card draws a record (field-level security applied; an object that declares no key fields shows its title alone). Arrow keys move the preview with the selection; the record is read once the selection rests for 150 ms. A palette narrower than 45rem (720px) shows the results alone.
- **Create "words" as OBJECT.** Once something is typed, the end of the list offers to create a record of up to three of the app's objects, the object of the page the palette was opened on first, with the typed words in the object's title field: Enter opens that object's create page (`/apps/APP/OBJECT/new`) with the field filled. An object is offered only where its list offers New (the same verdict: CRUD bucket, `userActions.create`, effective API operations, the create grant, and the create action's `visibleWhen` / `disabledWhen`), and only when its title field is a text field the member may write on create.
- **New OBJECT.** Typing "new", "create" or an object's name finds a "New OBJECT" entry for each such object, which opens its empty create page.
- **Enter hint.** A footer names what Enter does to the selected entry: Open record, Create, Switch app, Apply theme or Open.

New locale keys, in every pack: `console.commandPalette.create`, `createAs`, `previewEmpty`, `enterOpenRecord`, `enterCreate`, `enterSwitchApp`, `enterApplyTheme`, `enterOpen`. No props or package exports change.
10 changes: 10 additions & 0 deletions ui/packages/app-shell/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -1426,6 +1426,16 @@ bar's middle zone, centered between the left group and the utility cluster. In
the `home`, `orgs` and `studio` frames, and below `lg` (as an icon button), it
sits at the utility cluster's left edge.

Inside an app, while the query finds records, the palette shows the selected
hit on the right, read-only: its title and the object's declared
`highlightFields`, as the record pointer card draws them (a palette narrower
than 45rem shows the results alone). Once something is typed, the end of the
list offers "New OBJECT" for the app's objects the query names and "Create
"words" as OBJECT" for up to three of them, the page's own object first; each
opens the object's create page (`/apps/APP/OBJECT/new`), the second with the
words in its title field. An object is offered exactly where its list offers
New. A footer names what Enter does to the selected entry.

```tsx
import { useCommandPalette } from '@object-ui/app-shell';

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,333 @@
/**
* 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.
*/

/**
* The ⌘K palette previews the record hit it is on, offers to create a record
* from what was typed, finds "New OBJECT" entries, and says what Enter does
* (objectstack#36286).
*
* Pinned against the real `CommandPaletteProvider` (opened through its
* `?palette=1` deep link) over the per-object `find` fanout, with the record
* preview reading the record through the data source's `findOne`. Where Enter
* went is read off the router.
*/

import * as React from 'react';
import { describe, it, expect, vi, afterEach } from 'vitest';
import { render, fireEvent, waitFor, cleanup, screen, act } from '@testing-library/react';
import { MemoryRouter, useLocation } from 'react-router-dom';
import { MetadataCtx } from '@object-ui/react';

vi.mock('@object-ui/auth', async (importOriginal) => ({
...(await importOriginal<Record<string, unknown>>()),
useAuth: () => ({ user: { id: 'u1' }, activeOrganization: null }),
}));

/** The member may read everything and create everything but an invoice. */
const PERMS = {
isLoaded: true,
can: (object: string, action: string) => !(object === 'invoice_36286' && action === 'create'),
cannot: (object: string, action: string) => object === 'invoice_36286' && action === 'create',
check: () => ({ allowed: true }),
checkField: () => true,
getFieldPermissions: () => [],
getRowFilter: () => undefined,
getObjectApiOperations: () => undefined,
roles: [],
userId: 'u1',
systemPermissions: undefined,
hasCapabilities: () => true,
hasAnyQueue: undefined,
objectsUsingQueues: undefined,
};
vi.mock('@object-ui/permissions', async (importOriginal) => ({
...(await importOriginal<Record<string, unknown>>()),
usePermissions: () => PERMS,
}));

// The preview's key fields draw through `lazy()`: load the detail entry at
// module scope, so its cost is paid at import (ui/AGENTS.md, lazy boundaries).
import '@object-ui/plugin-detail';

import { CommandPalette } from './CommandPalette';
import { CommandPaletteProvider } from '../context/CommandPaletteProvider';
import { paletteEnterAction } from './PaletteEnterHint';
import { paletteCreateTargets, palettePrefillField, routeObjectName } from './paletteCreate';

const CUSTOMER = 'customer_36286';
const CONTACT = 'contact_36286';
const INVOICE = 'invoice_36286';

const OBJECTS = [
{
name: CUSTOMER,
label: 'Customer',
nameField: 'name',
highlightFields: ['industry', 'phone'],
fields: {
name: { type: 'text', label: 'Customer Name' },
industry: { type: 'text', label: 'Industry' },
phone: { type: 'text', label: 'Phone' },
},
},
{ name: CONTACT, label: 'Contact', nameField: 'full_name', fields: { full_name: { type: 'text', label: 'Full Name' } } },
{ name: INVOICE, label: 'Invoice', nameField: 'subject', fields: { subject: { type: 'text', label: 'Subject' } } },
];

const ROWS: Record<string, Array<Record<string, unknown>>> = {
[CUSTOMER]: [
{ id: 'C1', name: 'Qingdao Haichen Foods', industry: 'Food processing', phone: '0532-1000' },
{ id: 'C2', name: 'Qingdao Harbour Logistics', industry: 'Logistics', phone: '0532-2000' },
],
[CONTACT]: [],
[INVOICE]: [],
};

const CRM = {
name: 'crm',
label: 'CRM',
navigation: [
{ id: 'n_contact', type: 'object', objectName: CONTACT, label: 'Contacts' },
{ id: 'n_invoice', type: 'object', objectName: INVOICE, label: 'Invoices' },
{ id: 'n_customer', type: 'object', objectName: CUSTOMER, label: 'Customers' },
],
};
const SALES = { name: 'sales', label: 'Sales', navigation: [] };

function metadataValue() {
return {
apps: [],
objects: [],
dashboards: [],
reports: [],
pages: [],
loading: false,
error: null,
refresh: async () => {},
invalidate: () => {},
ensureType: async () => [],
getItem: async (type: string, name: string) => (type === 'object' ? OBJECTS.find((o) => o.name === name) ?? null : null),
getItemsByType: () => [],
getTypeStatus: () => 'ready' as const,
};
}

function Probe() {
const location = useLocation();
return <span data-testid="location" data-path={location.pathname} data-search={location.search} />;
}

function mount(path = `/apps/crm/${CUSTOMER}`) {
const ds = {
find: vi.fn(async (name: string, params: { $search?: string }) => ({
data: (ROWS[name] ?? []).filter((row) =>
String(row.name ?? '').toLowerCase().includes(String(params?.$search ?? '').toLowerCase()),
),
})),
findOne: vi.fn(async (name: string, id: string) => (ROWS[name] ?? []).find((row) => row.id === id) ?? null),
};
const metadata = metadataValue();
render(
<MetadataCtx.Provider value={metadata as never}>
<MemoryRouter initialEntries={[`${path}?palette=1`]}>
<CommandPaletteProvider>
<Probe />
<CommandPalette apps={[CRM, SALES]} activeApp={CRM} objects={OBJECTS} onAppChange={() => {}} dataSource={ds} />
</CommandPaletteProvider>
</MemoryRouter>
</MetadataCtx.Provider>,
);
const input = document.querySelector('[cmdk-input]') as HTMLInputElement | null;
if (!input) throw new Error('the palette did not open');
return { ds, input };
}

const type = (input: HTMLInputElement, value: string) => fireEvent.change(input, { target: { value } });
const press = (input: HTMLInputElement, key: string) => fireEvent.keyDown(input, { key });
const pane = () => screen.queryByTestId('palette-preview');
const hint = () => screen.getByTestId('palette-enter-hint').getAttribute('data-enter-action');
const selected = () => document.querySelector('[cmdk-item][data-selected="true"]')?.getAttribute('data-value');
const values = (prefix: string) =>
Array.from(document.querySelectorAll(`[cmdk-item][data-value^="${prefix}"]`)).map((el) => el.getAttribute('data-value'));
const location = () => {
const probe = screen.getByTestId('location');
return `${probe.getAttribute('data-path')}${probe.getAttribute('data-search')}`;
};

async function recordHitsShown(count: number) {
await waitFor(() => expect(values('record ')).toHaveLength(count), { timeout: 4000 });
}

afterEach(() => cleanup());

describe('objectstack#36286 — a record hit is previewed beside the results', () => {
it('draws the selected hit read-only: its title and the object\'s declared key fields; Enter opens the record', async () => {
const { ds, input } = mount();
type(input, 'haichen');
await recordHitsShown(1);

// The palette widens for two halves, and the pane follows the selection.
expect(document.querySelector('[role="dialog"]')?.className).toContain('sm:max-w-3xl');
await waitFor(() => expect(pane()?.querySelector('[data-record-preview="readable"]')).not.toBeNull(), { timeout: 4000 });
expect(pane()).toHaveTextContent('Qingdao Haichen Foods');
expect(await screen.findByText('Food processing')).toBeInTheDocument();
expect(pane()).toHaveTextContent('Industry');
expect(ds.findOne).toHaveBeenCalledWith(CUSTOMER, 'C1');
expect(hint()).toBe('openRecord');

press(input, 'Enter');
expect(location()).toBe(`/apps/crm/${CUSTOMER}/record/C1`);
});

it('ArrowDown moves the preview to the second hit, reading only the record it settles on', async () => {
const { ds, input } = mount();
type(input, 'qingdao');
await recordHitsShown(2);
await waitFor(() => expect(pane()?.getAttribute('data-preview-record')).toBe('C1'), { timeout: 4000 });

press(input, 'ArrowDown');
await waitFor(() => expect(pane()?.getAttribute('data-preview-record')).toBe('C2'), { timeout: 4000 });
await waitFor(() => expect(pane()).toHaveTextContent('Qingdao Harbour Logistics'), { timeout: 4000 });
expect(pane()).toHaveTextContent('Logistics');
expect(ds.findOne.mock.calls.map((call) => call[1])).toEqual(['C1', 'C2']);
});

it('the pane shows only from a 45rem palette: a container query on the palette frame', async () => {
const { input } = mount();
type(input, 'haichen');
await recordHitsShown(1);
expect(document.querySelector('[cmdk-root]')?.className).toContain('@container/palette');
expect(pane()?.className).toMatch(/(^| )hidden( |$)/);
expect(pane()?.className).toContain('@min-[45rem]/palette:block');
});

it('CONTROL: no record hit, no pane and no wide palette', async () => {
const { input } = mount();
type(input, 'zhang san');
await waitFor(() => expect(values('create as ')).not.toHaveLength(0));
await act(async () => {
await new Promise((resolve) => setTimeout(resolve, 400));
});
expect(pane()).toBeNull();
expect(document.querySelector('[role="dialog"]')?.className).not.toContain('sm:max-w-3xl');
});
});

describe('objectstack#36286 — "Create \'words\' as OBJECT" below the results', () => {
it('lists the creatable objects, the page\'s own object first; Enter opens its create form seeded with the words', async () => {
const { input } = mount();
type(input, 'Zhang San');
await waitFor(() => expect(values('create as ')).toEqual([`create as ${CUSTOMER}`, `create as ${CONTACT}`]));
expect(screen.getByText('Create "Zhang San" as Customer')).toBeInTheDocument();
// No create grant on invoices: no row offers to create one.
expect(values('create ').some((value) => value?.includes(INVOICE))).toBe(false);

expect(selected()).toBe(`create as ${CUSTOMER}`);
expect(hint()).toBe('create');
press(input, 'Enter');
expect(location()).toBe(`/apps/crm/${CUSTOMER}/new?name=Zhang+San`);
});

it('on a page of no object, the app\'s navigation order stands', async () => {
const { input } = mount('/apps/crm');
type(input, 'Zhang San');
await waitFor(() => expect(values('create as ')).toEqual([`create as ${CONTACT}`, `create as ${CUSTOMER}`]));
});

it('CONTROL: the empty palette offers no create row', async () => {
mount();
await act(async () => {});
expect(values('create ')).toEqual([]);
});

it('clearing the query takes the create rows away again', async () => {
const { input } = mount();
type(input, 'Zhang San');
await waitFor(() => expect(values('create as ')).not.toHaveLength(0));
type(input, '');
await waitFor(() => expect(values('create ')).toEqual([]));
});
});

describe('objectstack#36286 — "New OBJECT" entries', () => {
it('"new" finds one per object the member may create, never one it may not', async () => {
const { input } = mount();
type(input, 'new');
await waitFor(() => expect(values('create new ')).toEqual([`create new ${CUSTOMER}`, `create new ${CONTACT}`]));
expect(screen.getByText('New Customer')).toBeInTheDocument();
expect(screen.getByText('New Contact')).toBeInTheDocument();
expect(screen.queryByText('New Invoice')).toBeNull();
// The query names the action, not a record called "new".
expect(values('create as ')).toEqual([]);
});

it('an object\'s own name finds its entry; Enter opens its empty create form', async () => {
const { input } = mount('/apps/crm');
type(input, 'contact');
await waitFor(() => expect(values('create new ')).toEqual([`create new ${CONTACT}`]));
// The other creatable object still takes the words as a record name.
expect(values('create as ')).toEqual([`create as ${CUSTOMER}`]);
act(() => {
fireEvent.click(document.querySelector(`[cmdk-item][data-value="create new ${CONTACT}"]`)!);
});
expect(location()).toBe(`/apps/crm/${CONTACT}/new`);
});
});

describe('objectstack#36286 — the footer says what Enter does', () => {
it('reads the action off the selected entry\'s kind', () => {
expect(paletteEnterAction(`record Qingdao Customer ${CUSTOMER} C1`)).toBe('openRecord');
expect(paletteEnterAction('recent Qingdao C1')).toBe('openRecord');
expect(paletteEnterAction(`create as ${CUSTOMER}`)).toBe('create');
expect(paletteEnterAction(`create new ${CUSTOMER}`)).toBe('create');
expect(paletteEnterAction('app Sales sales')).toBe('switchApp');
expect(paletteEnterAction('theme dark')).toBe('applyTheme');
expect(paletteEnterAction(`object Customers ${CUSTOMER}`)).toBe('open');
expect(paletteEnterAction('search all results full page')).toBe('open');
expect(paletteEnterAction('')).toBeNull();
});

it('an app entry says Switch app', async () => {
const { input } = mount();
type(input, 'sales');
await waitFor(() => expect(selected()).toBe('app Sales sales'));
expect(hint()).toBe('switchApp');
expect(screen.getByTestId('palette-enter-hint')).toHaveTextContent('Switch app');
});
});

describe('objectstack#36286 — which objects a create row offers', () => {
const byName = new Map(OBJECTS.map((o) => [o.name, o]));
const names = OBJECTS.map((o) => o.name);

it('the create action\'s toolbar visibleWhen (fails closed) and disabledWhen hide an object', () => {
const hidden = { ...OBJECTS[1], userActions: { create: { visibleWhen: 'false' } } };
const greyed = { ...OBJECTS[0], userActions: { create: { disabledWhen: 'true' } } };
const map = new Map<string, any>([[CUSTOMER, greyed], [CONTACT, hidden], [INVOICE, OBJECTS[2]]]);
expect(paletteCreateTargets(names, map, PERMS as never, {}).map((t) => t.objectName)).toEqual([]);
expect(paletteCreateTargets(names, byName as never, PERMS as never, {}).map((t) => t.objectName)).toEqual([
CUSTOMER,
CONTACT,
]);
});

it('only a writable text title field takes the typed words', () => {
const base = OBJECTS[1];
expect(palettePrefillField(base as never, PERMS as never)).toBe('full_name');
expect(palettePrefillField({ ...base, fields: { full_name: { type: 'lookup' } } } as never, PERMS as never)).toBeUndefined();
expect(palettePrefillField({ ...base, fields: { full_name: { type: 'text', readonly: true } } } as never, PERMS as never)).toBeUndefined();
const noCreateField = { ...PERMS, checkField: () => false };
expect(palettePrefillField(base as never, noCreateField as never)).toBeUndefined();
});

it('the route\'s object is read off /apps/APP/OBJECT, only when the app lists it', () => {
expect(routeObjectName(`/apps/crm/${CUSTOMER}/record/C1`, names)).toBe(CUSTOMER);
expect(routeObjectName('/apps/crm/dashboard/sales', names)).toBeUndefined();
expect(routeObjectName('/apps/crm', names)).toBeUndefined();
});
});
Loading
Loading