Skip to content

Commit 3bfdb60

Browse files
committed
wip(app-shell): draw a served object or list view label as served in the sidebar and recent list
Claude-Session: https://claude.ai/code/session_01Utp6CZywtebM5fE7KZuMEM Co-authored-by: Claude <noreply@anthropic.com>
1 parent ff3f83b commit 3bfdb60

2 files changed

Lines changed: 78 additions & 22 deletions

File tree

‎ui/packages/app-shell/src/hooks/useNavTargetLabel.ts‎

Lines changed: 59 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -37,12 +37,16 @@
3737
* Nothing is written back and nothing is memoised across metadata versions, so
3838
* a renamed target shows its new name on the next render (the ruling accepts a
3939
* reload). A metadata label that is an inline locale map resolves through the
40-
* spec's own `resolveI18nLabel` in the active language. An object's or view's
41-
* result passes through the same convention-based bundle lookup
42-
* (`useObjectLabel`) the rest of the console applies to their names; a served
43-
* dashboard's label is already translated by the server and is drawn as given,
44-
* as its page header draws it (objectstack#30244). Either way the nav shows the
45-
* name the target's own page shows.
40+
* spec's own `resolveI18nLabel` in the active language. A served label is
41+
* already translated by the server, by its label order for an overridden item,
42+
* and is drawn as given: an object's (every object in the cache is a served
43+
* document), a served view's (`isServedView`, the question the object page's
44+
* view tabs ask) and a dashboard's (objectstack#30244, objectstack#30311). The
45+
* convention-based bundle lookup (`useObjectLabel`) is asked only for text no
46+
* server translated: a view the client derived, a pending draft's label, or a
47+
* target with no served label. Running the bundle over a served label was a
48+
* second translation pass in which the base package's string won a rename
49+
* back.
4650
*
4751
* A target with no label answers `undefined` — the renderer then falls through
4852
* the spec's ladder and, at the bottom, to the target's machine name.
@@ -74,6 +78,7 @@ import type { NavLabelTarget, NavTargetLabelResolver } from '@object-ui/layout';
7478
import { useMetadata } from '../providers/MetadataProvider.js';
7579
import { bundledFirst, useBundledReads } from '../providers/bundledReads.js';
7680
import { readObjectsByName, useReadItemsByName } from './useObjectPresence.js';
81+
import { isServedView } from './useServedViewItems.js';
7782

7883
/**
7984
* A metadata label (plain string or inline locale map) as display text, or
@@ -141,6 +146,41 @@ function listViewsOf(def: LabelledRecord | undefined): Record<string, LabelledRe
141146
return views && typeof views === 'object' ? (views as Record<string, LabelledRecord | undefined>) : {};
142147
}
143148

149+
/** One identity for an absent read. */
150+
const NO_ITEMS: readonly unknown[] = Object.freeze([]);
151+
152+
/**
153+
* What a `view` target reads (objectstack#30311): its object, with the views
154+
* merged in, and the two served reads `isServedView` asks of it — the
155+
* `/meta/view` documents and the object documents as served, before the merge.
156+
* By name, all three come from the one names-only scope read
157+
* `readObjectsByName` makes; otherwise from the whole lists, as
158+
* `useServedViewItems` reads them outside an app scope.
159+
*/
160+
function readViewTarget(
161+
metadata: ReturnType<typeof useMetadata>,
162+
byName: boolean,
163+
objectName: string,
164+
): { objects: readonly unknown[]; served: { views: readonly unknown[]; objects: readonly unknown[] } } {
165+
const { readObjectScope, getTypeStatus } = metadata;
166+
if (byName && readObjectScope && getTypeStatus?.('object') === 'idle' && objectName) {
167+
// `views` rides beside the published shape for this package's own readers.
168+
const scope = readObjectScope({ names: [objectName] }) as { objects: unknown[]; served?: unknown; views?: unknown };
169+
return {
170+
objects: scope.objects,
171+
served: {
172+
views: Array.isArray(scope.views) ? scope.views : NO_ITEMS,
173+
objects: Array.isArray(scope.served) ? scope.served : NO_ITEMS,
174+
},
175+
};
176+
}
177+
const objects = byName ? readObjectsByName(metadata, [objectName], { views: true }) : metadata.objects;
178+
return {
179+
objects: objects ?? NO_ITEMS,
180+
served: { views: metadata.getItemsByType?.('view') ?? NO_ITEMS, objects: metadata.getItemsByType?.('object') ?? NO_ITEMS },
181+
};
182+
}
183+
144184
/**
145185
* How the resolver reads an object (objectstack#29121). By default from the
146186
* whole `objects` list, as the Studio's and the search page's callers do. With
@@ -171,15 +211,24 @@ export function useNavTargetLabel(options?: NavTargetLabelOptions): NavTargetLab
171211
case 'object': {
172212
const objects = byName ? readObjectsByName(metadata, [target.objectName]) : metadata.objects;
173213
const def = findByName(objects, target.objectName);
174-
const text =
175-
metadataText(def?.label, language) ?? draftText(drafts, 'object', target.objectName, language);
176-
return nonEmpty(objectLabel({ name: target.objectName, label: text ?? '' }));
214+
// A served object's label is already translated (objectstack#30311):
215+
// drawn as given. A pending draft's label, which no server
216+
// translated, still goes through the bundle.
217+
return metadataText(def?.label, language)
218+
?? nonEmpty(objectLabel({
219+
name: target.objectName,
220+
label: draftText(drafts, 'object', target.objectName, language) ?? '',
221+
}));
177222
}
178223
case 'view': {
179-
const objects = byName ? readObjectsByName(metadata, [target.objectName], { views: true }) : metadata.objects;
224+
const { objects, served } = readViewTarget(metadata, byName, target.objectName);
180225
const views = listViewsOf(findByName(objects, target.objectName));
181226
const id = resolveViewId(target.viewName, Object.keys(views), target.objectName);
182227
const text = metadataText(id ? views[id]?.label : undefined, language);
228+
// A served view's label is already translated (objectstack#30311),
229+
// as its tab on the object page draws it; a view the client derived
230+
// still goes through the bundle.
231+
if (text !== undefined && isServedView(served, target.objectName, id)) return text;
183232
return nonEmpty(viewLabel(target.objectName, target.viewName, text ?? ''));
184233
}
185234
case 'dashboard': {

‎ui/packages/app-shell/src/hooks/useRecentItemLabel.ts‎

Lines changed: 19 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -19,19 +19,22 @@
1919
* already asks for that kind:
2020
*
2121
* - an object: what the page the entry opens — the object's LIST — is titled,
22-
* `useObjectLabel`'s `objectPluralLabel` over the object's cached metadata
23-
* (the translated plural, else the declared `pluralLabel`, else the label
24-
* the singular rung reads; objectui#11733). ⛔ Not {@link useNavTargetLabel}:
25-
* its object rung is the spec's step 3 for an UNLABELLED NAVIGATION ENTRY
26-
* ("the object's label", the singular), a ruling this entry does not reopen;
22+
* the served `pluralLabel`, else the served `label` (objectui#11733). The
23+
* server translated both, by its label order for an overridden item, so
24+
* they are drawn as given (objectstack#30311); `useObjectLabel`'s
25+
* `objectPluralLabel` under the object's name is asked only when the cache
26+
* holds neither. ⛔ Not {@link useNavTargetLabel}: its object rung is the
27+
* spec's step 3 for an UNLABELLED NAVIGATION ENTRY ("the object's label",
28+
* the singular), a ruling this entry does not reopen;
2729
* - a dashboard: {@link useNavTargetLabel} — the text the sidebar shows for an
2830
* unlabelled navigation entry that opens the same target (the served
2931
* label, an inline locale map read in the active language, drawn as given;
3032
* the `useObjectLabel` bundle lookup only when the read carries none);
3133
* - a list view (objectui#12604): {@link useNavTargetLabel} again — the text
3234
* the sidebar shows for an unlabelled navigation entry that opens the same
33-
* view (the view's label from its object's `listViews`, through the
34-
* `viewLabel` bundle lookup). A view the cache does not hold — one a user
35+
* view (a served view's label from its object's `listViews` as given, a
36+
* view the client derived through the `viewLabel` bundle lookup). A view
37+
* the cache does not hold — one a user
3538
* saved for themself — is drawn as the list it opens: the object's, as an
3639
* object entry is;
3740
* - a page or a report: the same rule for that kind — the page's / report's
@@ -98,11 +101,15 @@ export function useRecentItemLabel(
98101
/** What an object's list is titled — an `object` entry's label, and a view's fallback. */
99102
const listTitle = (objectName: string): string => {
100103
const def = findByName(readObjectsByName(metadata, [objectName]), objectName) as (LabelledRecord & { pluralLabel?: unknown }) | undefined;
101-
// The singular half is read exactly as `useNavTargetLabel` reads it;
102-
// `pluralLabel` is a plain string in the spec, so only a string is one.
103-
const text = metadataText(def?.label, language);
104-
const plural = typeof def?.pluralLabel === 'string' ? def.pluralLabel : undefined;
105-
return nonEmpty(objectPluralLabel({ name: objectName, label: text ?? '', pluralLabel: plural })) ?? objectName;
104+
// A served object's labels are already translated (objectstack#30311):
105+
// drawn as given. The singular half is read exactly as
106+
// `useNavTargetLabel` reads it; `pluralLabel` is a plain string in the
107+
// spec, so only a string is one.
108+
const plural = typeof def?.pluralLabel === 'string' ? nonEmpty(def.pluralLabel) : undefined;
109+
return plural
110+
?? metadataText(def?.label, language)
111+
?? nonEmpty(objectPluralLabel({ name: objectName, label: '' }))
112+
?? objectName;
106113
};
107114
switch (item.type) {
108115
case 'object':

0 commit comments

Comments
 (0)