diff --git a/.changeset/36577-data-page-object-switch.md b/.changeset/36577-data-page-object-switch.md new file mode 100644 index 00000000000..db03fe1b38c --- /dev/null +++ b/.changeset/36577-data-page-object-switch.md @@ -0,0 +1,10 @@ +--- +'@object-ui/app-shell': patch +--- + +fix(console): moving straight from one object's data page to another's opens the new object's list as its address says + +Clause-②: no +Metadata-protocol: none + +On the bare data page (`/apps/APP/OBJECT/data`), going straight to another object's data page without a reload (a link, the browser's back and forward) kept the page and its list mounted. The new object's list then kept the previous object's search, Filter panel, sort, grouping and quick-filter chips, and ignored the ones its own address carried, while the address showed the new ones. The list now opens afresh for each object, from the new address: its `uf__search`, `uf__filter`, `uf__sort`, `uf__group` and `uf_FIELD` chip selections. An address with none of them opens the new object's list with none. 「Reset」 on a list opened from a record's related list is unchanged. diff --git a/ui/packages/app-shell/src/views/ObjectDataPage.openRule-35875.test.tsx b/ui/packages/app-shell/src/views/ObjectDataPage.openRule-35875.test.tsx index a7590e8dd3c..e9aac38f7c3 100644 --- a/ui/packages/app-shell/src/views/ObjectDataPage.openRule-35875.test.tsx +++ b/ui/packages/app-shell/src/views/ObjectDataPage.openRule-35875.test.tsx @@ -32,7 +32,7 @@ import * as React from 'react'; import { describe, it, expect, vi, beforeEach, afterEach } from 'vitest'; import { render, cleanup, screen, fireEvent, waitFor, within, act } from '@testing-library/react'; -import { MemoryRouter, Routes, Route, useLocation } from 'react-router-dom'; +import { MemoryRouter, Routes, Route, useLocation, useNavigate } from 'react-router-dom'; import { createI18n, I18nProvider } from '@object-ui/i18n'; import { MePermissionsProvider, type MePermissionsAuthenticatedResponse } from '@object-ui/permissions'; @@ -549,3 +549,96 @@ describe('objectstack#36408 — the data page keeps its list\'s search, sort, gr expect(screen.queryByTestId('sort-reset-default')).toBeNull(); }); }); + +describe('objectstack#36577 — moving straight from one object\'s data page to another\'s opens the new address\'s list', () => { + const CONTACT = 'demo_contact'; + const CONTACT_PATH = `/apps/sales/${CONTACT}/data`; + const TWO_OBJECTS = [ + OBJECTS[0], + { + name: CONTACT, + label: 'Contact', + pluralLabel: 'Contacts', + fields: { + name: { type: 'text', label: 'Name' }, + city: { type: 'text', label: 'City' }, + stage: { type: 'select', label: 'Stage', options: [{ value: 'won', label: 'Won' }, { value: 'lost', label: 'Lost' }] }, + }, + }, + ]; + /** The list queries with the object each asked, newest last; the count probe (`$top: 0`) is left out. */ + let queries: Array<{ object: string; params: Record }> = []; + const lastQueryOf = (object: string) => [...queries].reverse().find((q) => q.object === object)?.params; + const addressParams = () => new URLSearchParams(search() ?? ''); + + function permissions(): MePermissionsAuthenticatedResponse { + const base = answer(); + return { ...base, objects: { ...base.objects, [CONTACT]: base.objects[OBJECT_NAME] } } as MePermissionsAuthenticatedResponse; + } + + /** In-app navigation, as a link or the history does it: no reload. */ + function GoTo({ to }: { to: string }) { + const navigate = useNavigate(); + return ; + } + + async function mountThenSwitch(fromSearch: string, toSearch: string) { + queries = []; + const dataSource = { + find: vi.fn(async (object: string, params: Record) => { + if (params?.$top !== 0) queries.push({ object, params }); + return { data: ROWS, total: MATCHED }; + }), + findOne: vi.fn(async () => null), + create: vi.fn(async () => ({})), + update: vi.fn(async () => ({})), + delete: vi.fn(async () => ({})), + getObjectSchema: vi.fn(async (name: string) => TWO_OBJECTS.find((o) => o.name === name) ?? null), + } as never; + window.history.replaceState(null, '', DATA_PATH); + render( + + + + + + } /> + + + + + + + , + ); + await screen.findByText('Globex', undefined, { timeout: 5000 }); + await settle(); + fireEvent.click(screen.getByTestId('go-to')); + await waitFor(() => expect(lastQueryOf(CONTACT)).toBeDefined(), { timeout: 5000 }); + await screen.findByText('Globex', undefined, { timeout: 5000 }); + await settle(); + } + + it('a search and a chip on the new address: the new object\'s list searches and filters by them, not by the old one\'s', async () => { + await mountThenSwitch('?uf__search=Acm', '?uf__search=Glo&uf_stage=won'); + expect(pathname()).toBe(CONTACT_PATH); + const q = lastQueryOf(CONTACT)!; + expect(q.$search).toBe('Glo'); + expect(JSON.stringify(q.$filter ?? null)).toContain('stage'); + expect(JSON.stringify(q.$filter ?? null)).toContain('won'); + expect(screen.getByTestId('search-active-keyword').textContent).toBe('Glo'); + // The address and the list say the same thing. + expect(addressParams().get('uf__search')).toBe('Glo'); + expect(addressParams().get('uf_stage')).toBe('won'); + }); + + it('a new address with no list state: the new object\'s list carries none of the old one\'s, and none is written', async () => { + await mountThenSwitch('?uf__search=Acm', ''); + expect(pathname()).toBe(CONTACT_PATH); + for (const { params } of queries.filter((entry) => entry.object === CONTACT)) { + expect(params.$search).toBeUndefined(); + } + expect(screen.queryByTestId('search-active-keyword')).toBeNull(); + expect(addressParams().has('uf__search')).toBe(false); + }); +}); diff --git a/ui/packages/app-shell/src/views/ObjectDataPage.tsx b/ui/packages/app-shell/src/views/ObjectDataPage.tsx index b524303191d..31d1f7d5808 100644 --- a/ui/packages/app-shell/src/views/ObjectDataPage.tsx +++ b/ui/packages/app-shell/src/views/ObjectDataPage.tsx @@ -350,12 +350,10 @@ export function ObjectDataPage({ dataSource, objects }: any) { // empty instead of refusing the whole list query; outside it, the adapter. const listDataSource = usePreviewListDataSource(dataSource, objectDef); - // ADR-0047 filter persistence — same wiring as InterfaceListPage: restore - // `uf_*` once at mount, mirror selection changes back (replace, no history - // spam). - const [initialUfSelections] = React.useState | undefined>( - () => parseUserFilterParams(new URLSearchParams(window.location.search)), - ); + // ADR-0047 filter persistence — same wiring as InterfaceListPage: the + // `uf_*` chips are restored with the list's seed (below), once per object + // and list generation, and selection changes are mirrored back (replace, + // no history spam). const handleUserFilterSelectionsChange = React.useCallback( (selections: Record>) => { setSearchParams((prev) => applyUserFilterParams(prev, selections), { replace: true }); @@ -378,8 +376,11 @@ export function ObjectDataPage({ dataSource, objects }: any) { * view's filter. The list applies both (the conditions AND the panel). * * The SEED — what the list opens with — is read from the address once per - * object and list generation (`ListView` takes its search and panel once, at - * mount). Unlike `ObjectView`, a reload is not a reset here: this page has + * object and list generation, and the list is keyed on the same pair + * (`ListView` takes its search, panel and chips once, at mount): a move + * straight from one object's data page to another's — a link, the history — + * keeps this page mounted, so the list remounts on the new address's seed + * instead of keeping the previous object's (objectstack#36577). Unlike `ObjectView`, a reload is not a reset here: this page has * no view to return to, and its address is the list ("the URL is the view"). * Each piece passes `ObjectView`'s field gate: a field the object does not * declare, or one this user cannot read once permissions are loaded, is @@ -396,6 +397,8 @@ export function ObjectDataPage({ dataSource, objects }: any) { const listSeedRef = React.useRef<{ key: string; state: ListUrlState; + /** The `uf_` chips' selections (ADR-0047), read with the rest. */ + userFilterSelections?: Record; /** Taken by a 「Reset」, which wrote the address itself. */ reset?: boolean; refusedPanelFilter?: boolean; @@ -414,10 +417,16 @@ export function ObjectDataPage({ dataSource, objects }: any) { const carried = addressPanelFilterConditionCount(searchParams); const refusedPanelFilter = carried > 0 && (state.filters?.conditions.length ?? 0) < carried; if (refusedPanelFilter) delete state.filters; - listSeedRef.current = { key: listSeedKey, state, refusedPanelFilter }; + listSeedRef.current = { + key: listSeedKey, + state, + userFilterSelections: parseUserFilterParams(searchParams), + refusedPanelFilter, + }; } } - const listSeed = listSeedRef.current?.key === listSeedKey ? listSeedRef.current?.state : undefined; + const listSeedEntry = listSeedRef.current?.key === listSeedKey ? listSeedRef.current : undefined; + const listSeed = listSeedEntry?.state; React.useEffect(() => { const seed = listSeedRef.current; // A 「Reset」's seed writes nothing: the reset already cleared the @@ -447,9 +456,9 @@ export function ObjectDataPage({ dataSource, objects }: any) { }, [setSearchParams], ); - // The chips' selections the list remounts with: the address's at mount; none + // The chips' selections the list mounts with: the seed's address's; none // after a 「Reset」, which cleared their params. - const listUserFilterSelections = listGeneration === 0 ? initialUfSelections : undefined; + const listUserFilterSelections = listSeedEntry?.userFilterSelections; // URL filter triples, trimmed to readable fields. Predicates on unreadable // fields are dropped here for UX honesty; the SERVER is the actual @@ -1063,7 +1072,9 @@ export function ObjectDataPage({ dataSource, objects }: any) {
{schema && (