|
| 1 | +/** |
| 2 | + * ObjectUI |
| 3 | + * Copyright (c) 2024-present ObjectStack Inc. |
| 4 | + * |
| 5 | + * This source code is licensed under the MIT license found in the |
| 6 | + * LICENSE file in the root directory of this source tree. |
| 7 | + */ |
| 8 | + |
| 9 | +/** |
| 10 | + * useServedAppLabel — what an app is called, and how it describes itself, on |
| 11 | + * every console surface that names an app (objectstack#30311). |
| 12 | + * |
| 13 | + * An app the `/meta/app` read served is already translated: `translateApp` in |
| 14 | + * `@objectstack/spec` writes its `label` and `description` for the request's |
| 15 | + * locale, and for an overridden app (a `.custom` pair) in the ruled label |
| 16 | + * order, the override's own text kept over the base package's translation |
| 17 | + * (objectstack#30353). So a served label is drawn as given. Running the |
| 18 | + * client bundle's `apps.<name>.label` lookup over it was a second translation |
| 19 | + * pass in which the base package's string won a rename back. |
| 20 | + * |
| 21 | + * The bundle (`useObjectLabel().appLabel` / `appDescription`) is asked only |
| 22 | + * for text no server translated: a pending draft under `?preview=draft` |
| 23 | + * (the item carries `_draft: true`), a keyed label (`{ key, defaultValue }`, |
| 24 | + * resolved through `t` as before), or an app with no label or description at |
| 25 | + * all. An unedited app reads the same text as before, because the server |
| 26 | + * serves the bundle's translation for it. |
| 27 | + */ |
| 28 | + |
| 29 | +import { useCallback, useMemo } from 'react'; |
| 30 | +import { useObjectLabel, useObjectTranslation } from '@object-ui/i18n'; |
| 31 | +import { resolveKeyedI18nLabel } from '../utils/index.js'; |
| 32 | +import { metadataText } from './useNavTargetLabel.js'; |
| 33 | + |
| 34 | +/** The members of an app record this hook reads. */ |
| 35 | +export interface ServedAppSource { |
| 36 | + name: string; |
| 37 | + label?: unknown; |
| 38 | + description?: unknown; |
| 39 | + _draft?: unknown; |
| 40 | +} |
| 41 | + |
| 42 | +type KeyedLabel = { key: string; defaultValue?: string; params?: Record<string, any> }; |
| 43 | + |
| 44 | +/** A console-keyed label (`{ key, defaultValue }`): a key, not served text. */ |
| 45 | +function isKeyedLabel(value: unknown): value is KeyedLabel { |
| 46 | + return !!value && typeof value === 'object' && typeof (value as { key?: unknown }).key === 'string'; |
| 47 | +} |
| 48 | + |
| 49 | +/** `label` / `description` as the server translated it, or `undefined` when it served none. */ |
| 50 | +function servedText(app: ServedAppSource, value: unknown, language: string | undefined): string | undefined { |
| 51 | + if (app._draft === true || isKeyedLabel(value)) return undefined; |
| 52 | + return metadataText(value, language); |
| 53 | +} |
| 54 | + |
| 55 | +/** The bundle's input for a value that is not served text: a keyed label through `t`, else a plain string. */ |
| 56 | +function bundleFallback(value: unknown, t: (key: string, options?: any) => string): string | undefined { |
| 57 | + if (isKeyedLabel(value)) return resolveKeyedI18nLabel(value, t); |
| 58 | + return typeof value === 'string' ? value : undefined; |
| 59 | +} |
| 60 | + |
| 61 | +export interface ServedAppLabels { |
| 62 | + /** The app's display name: the served label, else the bundle's. */ |
| 63 | + appLabel: (app: ServedAppSource) => string; |
| 64 | + /** The app's description: the served one, else the bundle's; `undefined` when neither names one. */ |
| 65 | + appDescription: (app: ServedAppSource) => string | undefined; |
| 66 | +} |
| 67 | + |
| 68 | +export function useServedAppLabel(): ServedAppLabels { |
| 69 | + const { language, t } = useObjectTranslation(); |
| 70 | + const { appLabel: bundleAppLabel, appDescription: bundleAppDescription } = useObjectLabel(); |
| 71 | + |
| 72 | + const appLabel = useCallback( |
| 73 | + (app: ServedAppSource) => |
| 74 | + servedText(app, app.label, language) |
| 75 | + ?? bundleAppLabel({ name: app.name, label: bundleFallback(app.label, t) }), |
| 76 | + [language, t, bundleAppLabel], |
| 77 | + ); |
| 78 | + const appDescription = useCallback( |
| 79 | + (app: ServedAppSource) => |
| 80 | + servedText(app, app.description, language) |
| 81 | + ?? bundleAppDescription({ name: app.name, description: bundleFallback(app.description, t) }), |
| 82 | + [language, t, bundleAppDescription], |
| 83 | + ); |
| 84 | + return useMemo(() => ({ appLabel, appDescription }), [appLabel, appDescription]); |
| 85 | +} |
0 commit comments