Skip to content

Commit 2e11452

Browse files
committed
refactor(app-shell): move ObjectView's realtime refresh and record count into a sibling hook
Slice 4 of the move-only ObjectView split. The realtime subscription, the reconnection conflict resolution and the footer record-count fetch move verbatim from ObjectViewInner into useObjectViewLiveData in views/objectViewLiveData.ts. The component calls the hook at the same position (hook order unchanged) and passes its inputs explicitly; the recordCount state stays in the component. No test edits. Claude-Session: https://claude.ai/code/session_01QciHBvFJXnhZqXmdqpDtpz Co-authored-by: Claude <noreply@anthropic.com>
1 parent 05038cd commit 2e11452

3 files changed

Lines changed: 87 additions & 52 deletions

File tree

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
---
2+
'@object-ui/app-shell': patch
3+
---
4+
5+
refactor(app-shell): the Console `ObjectView`'s realtime refresh and record-count fetch move into a sibling hook
6+
7+
Clause-②: no
8+
Metadata-protocol: none
9+
10+
The realtime subscription that refreshes the list when the server reports a change, the reconnection conflict resolution, and the footer's `$top: 0` record-count fetch move verbatim out of `ObjectViewInner` in `views/ObjectView.tsx` into `useObjectViewLiveData` in `objectViewLiveData.ts` beside it. The component calls the hook at the same place, so the hook order is unchanged; the record-count state stays in the component and is passed in with the other inputs. No logic, name or rendered output changes, and the package entry is unchanged.
11+
12+
What to do: nothing.

‎ui/packages/app-shell/src/views/ObjectView.tsx‎

Lines changed: 4 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,6 @@ import { useObjectActions } from '../hooks/useObjectActions.js';
8989
import { useObjectTranslation, useObjectLabel, useDisplayLocale, pickLocalized } from '@object-ui/i18n';
9090
import { usePermissions } from '@object-ui/permissions';
9191
import { useAuth, useWorkspaceAdminStatus } from '@object-ui/auth';
92-
import { useRealtimeSubscription, useConflictResolution } from '@object-ui/collaboration';
9392
import { ActionProvider, useNavigationOverlay, SchemaRenderer, useActionTextLocalizer, useRowPredicate, RelatedRecordActionsProvider, notifyDataChanged, useCapabilityGate, usePredicateScope, useMetadata } from '@object-ui/react';
9493
import { toast } from 'sonner';
9594
import { useConsoleActionRuntime } from '../hooks/useConsoleActionRuntime.js';
@@ -126,6 +125,7 @@ import {
126125
dispatchViewPatches,
127126
} from './objectViewOptions.js';
128127
import { ObjectNotFoundState, useViewTabsFit, keepInlineEditModeForTheSession } from './objectViewChrome.js';
128+
import { useObjectViewLiveData } from './objectViewLiveData.js';
129129

130130
// objectstack#25992 slice 1: these module-level helpers moved to the sibling
131131
// `objectView*.ts` modules above. Every name this file exported stays
@@ -1449,57 +1449,9 @@ function ObjectViewInner({ dataSource, objects, onEdit, externalRefreshKey }: Co
14491449
onToast: toastHandler as (message: string, options?: { type?: string }) => void,
14501450
});
14511451

1452-
// Real-time: auto-refresh when server reports data changes
1453-
const { lastMessage: realtimeMessage } = useRealtimeSubscription({
1454-
channel: `object:${objectDef.name}`,
1455-
});
1456-
1457-
// Conflict resolution: detect and queue conflicts on reconnection
1458-
const conflictUserId = objectDef.name ? `user-${objectDef.name}` : 'current-user';
1459-
const { hasConflicts, resolveAllConflicts } = useConflictResolution(conflictUserId);
1460-
1461-
useEffect(() => {
1462-
if (realtimeMessage) {
1463-
// On reconnection data change, auto-resolve with server-wins strategy
1464-
if (hasConflicts) {
1465-
resolveAllConflicts('remote');
1466-
}
1467-
refreshData();
1468-
}
1469-
}, [realtimeMessage, hasConflicts, resolveAllConflicts, refreshData]);
1470-
1471-
// Fetch record count for footer display.
1472-
//
1473-
// `$top: 0` IS the request: give me the total, send no rows. It was written
1474-
// `limit: 0`, which is not a `QueryParams` key — `convertQueryParams` copies
1475-
// exactly the `$`-prefixed keys the type declares — so the cap reached no
1476-
// branch and was dropped, and the platform's GET list route has no default
1477-
// page size. The effect therefore did the opposite of what it says: it
1478-
// downloaded EVERY row of the object, on every mount and every refresh of
1479-
// every list view, to read one integer off the envelope. The dead spelling
1480-
// type-checked because `QueryParams` then carried `[key: string]: any` for
1481-
// adapter-specific params; `object-ui/no-unprefixed-query-params` rejects it
1482-
// at write time now (objectui#5458), and the index signature itself is gone
1483-
// (objectui#7497), so `tsc` refuses it as well.
1484-
//
1485-
// `total` is the only field that can still answer the question, so the
1486-
// row-counting fallbacks are gone rather than repointed. Once we ask for
1487-
// zero rows an empty `data` means "you asked for none", not "the object is
1488-
// empty" — counting the response would report a confident `0` for any
1489-
// adapter that does not send a total. Leaving `recordCount` `undefined`
1490-
// omits the footer line instead of asserting a wrong number; the render is
1491-
// already guarded by `typeof recordCount === 'number'`.
1492-
useEffect(() => {
1493-
if (dataSource?.find && objectDef.name) {
1494-
dataSource.find(objectDef.name, { $top: 0 }).then((result: any) => {
1495-
if (typeof result?.total === 'number') {
1496-
setRecordCount(result.total);
1497-
}
1498-
}).catch(() => {
1499-
// Silently ignore — record count is non-critical
1500-
});
1501-
}
1502-
}, [dataSource, objectDef.name, refreshKey]);
1452+
// objectstack#25992 slice 4: the realtime refresh, conflict resolution and
1453+
// footer record count moved to `./objectViewLiveData.ts`; the count state stays here.
1454+
useObjectViewLiveData({ dataSource, objectDef, refreshData, refreshKey, setRecordCount });
15031455

15041456
// Navigation overlay for record detail (supports drawer/modal/split/popover via config)
15051457
// Priority: activeView.navigation > objectDef.navigation > default drawer.
Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
/**
2+
* Live data of the Console `ObjectView` (`./ObjectView.tsx`): the realtime
3+
* subscription that refreshes the list when the server reports a change, the
4+
* reconnection conflict resolution, and the footer's record-count fetch, moved
5+
* out of `ObjectViewInner` verbatim (objectstack#25992, slice 4). The record
6+
* count state stays in the component; it and every other input are passed in.
7+
*/
8+
9+
import { useEffect } from 'react';
10+
import type { DataSource } from '@object-ui/types';
11+
import { useRealtimeSubscription, useConflictResolution } from '@object-ui/collaboration';
12+
13+
export function useObjectViewLiveData({ dataSource, objectDef, refreshData, refreshKey, setRecordCount }: {
14+
dataSource: DataSource;
15+
objectDef: { name: string };
16+
refreshData: () => void;
17+
refreshKey: number;
18+
setRecordCount: (count: number) => void;
19+
}) {
20+
// Real-time: auto-refresh when server reports data changes
21+
const { lastMessage: realtimeMessage } = useRealtimeSubscription({
22+
channel: `object:${objectDef.name}`,
23+
});
24+
25+
// Conflict resolution: detect and queue conflicts on reconnection
26+
const conflictUserId = objectDef.name ? `user-${objectDef.name}` : 'current-user';
27+
const { hasConflicts, resolveAllConflicts } = useConflictResolution(conflictUserId);
28+
29+
useEffect(() => {
30+
if (realtimeMessage) {
31+
// On reconnection data change, auto-resolve with server-wins strategy
32+
if (hasConflicts) {
33+
resolveAllConflicts('remote');
34+
}
35+
refreshData();
36+
}
37+
}, [realtimeMessage, hasConflicts, resolveAllConflicts, refreshData]);
38+
39+
// Fetch record count for footer display.
40+
//
41+
// `$top: 0` IS the request: give me the total, send no rows. It was written
42+
// `limit: 0`, which is not a `QueryParams` key — `convertQueryParams` copies
43+
// exactly the `$`-prefixed keys the type declares — so the cap reached no
44+
// branch and was dropped, and the platform's GET list route has no default
45+
// page size. The effect therefore did the opposite of what it says: it
46+
// downloaded EVERY row of the object, on every mount and every refresh of
47+
// every list view, to read one integer off the envelope. The dead spelling
48+
// type-checked because `QueryParams` then carried `[key: string]: any` for
49+
// adapter-specific params; `object-ui/no-unprefixed-query-params` rejects it
50+
// at write time now (objectui#5458), and the index signature itself is gone
51+
// (objectui#7497), so `tsc` refuses it as well.
52+
//
53+
// `total` is the only field that can still answer the question, so the
54+
// row-counting fallbacks are gone rather than repointed. Once we ask for
55+
// zero rows an empty `data` means "you asked for none", not "the object is
56+
// empty" — counting the response would report a confident `0` for any
57+
// adapter that does not send a total. Leaving `recordCount` `undefined`
58+
// omits the footer line instead of asserting a wrong number; the render is
59+
// already guarded by `typeof recordCount === 'number'`.
60+
useEffect(() => {
61+
if (dataSource?.find && objectDef.name) {
62+
dataSource.find(objectDef.name, { $top: 0 }).then((result: any) => {
63+
if (typeof result?.total === 'number') {
64+
setRecordCount(result.total);
65+
}
66+
}).catch(() => {
67+
// Silently ignore — record count is non-critical
68+
});
69+
}
70+
}, [dataSource, objectDef.name, refreshKey]);
71+
}

0 commit comments

Comments
 (0)