Skip to content

Commit dda5e74

Browse files
fix(components): the docs flow embed names the flow and its approval steps in the reader's language (#36335)
<!-- claude-projects-attribution: {"github_login":"zhuangjianguo"} --> _Requested by **Steedos** · [project thread](https://claude.ai/code/project/chan_018ETKaNwMZCNRYD7D8Q9Ubb?thread=cmsg_018ETKaNwMZCNRYD7D8Q9UbbUq2j5tRRVo3TVY3wcS1Fdg)_ Part of #35979 (Asked item 1 only. Asked item 2, a translation key for decision / notify / start / end node labels, was ruled option A on the card and is built on its own claim and branch, not in this PR; the card stays open for it) repo:objectstack#7 · zhuangjianguo · session_01QsR2jV3scu6UYPb42n5okQ Clause-②: yes (widening) Metadata-protocol: none ## 维护者速读 - 用户能看到什么变化:非英文读者打开带流程嵌入的使用手册时,流程标题、审批步骤名,以及判断分支指向的审批步骤名,改用读者语言显示(例如「大额商机审批」「销售经理审核」);包里没有译文的地方仍显示原文。 - 为什么做:HotCRM 中文手册里的审批流程图标题和步骤全是英文,而包里已带中文译文;这是此前 Studio 流程设计器同类修复未覆盖的文档查看器。 - 出错会怎样:只影响显示,最坏情况是某个步骤仍显示原文;不改任何数据,可直接撤回。开始、判断、通知、结束这几类步骤的译文位置已裁定要补,由另一项单独完成,本次仍显示原文。 - 要你做什么:无。 ## What changes - **The docs viewer's flow embed reads the package's flow labels.** `FlowView` in `element:metadata_viewer` now draws the flow title from `flows.FLOW.label` and each `approval` step from `flows.FLOW.approvals.NODE_ID.label`, in the reader's language, and falls back to the authored label (then the name / node id) where the bundle has no entry. Two label reads changed (`flow.label`, `n.label`), through one hook call placed before the early returns. - **Branch targets too.** Since #36327 landed, the business view lists each decision's branches and names the step each branch leads to. That name now takes the same translated step label (a `stepLabel` prop on `Branches`), so an approval step reached from a decision is not named in English beside its translated row. - **The flow-label resolution moved to `@object-ui/i18n`.** `ui/packages/i18n/src/flowLabels.ts` holds `flowLabelTranslations` and `useFlowLabelTranslations`, moved from the Studio module because `@object-ui/components` cannot import `@object-ui/app-shell`. It is built on `activeFlowsBundle`, which #36316 already moved into `@object-ui/i18n` (`flowsBundle.ts`); this PR adds no second copy of that reader. The flow label still goes through the spec's `translateFlow`; the step label is still read for `approval` nodes only. The Studio module keeps its own parts (the Automations rail reader and the canvas context) and re-exports the moved ones, so its call sites are unchanged. - **Which steps can be translated today.** The spec's `flows.FLOW` group has a label key only for `approval` steps. A start, decision, notify or end step keeps its authored label until the Asked item 2 work adds its key. - No spec change, no server change. `GET /meta/flow` is unchanged; the embed reads the bundle the host already loads. ## Read first (by risk) 1. `ui/packages/components/src/renderers/basic/metadata-viewer.tsx`, the `FlowView` and `Branches` hunks: the hook sits before the early returns, beside the `business` flag from #36327. 2. `ui/packages/i18n/src/flowLabels.ts`: the moved resolver; compare with the Studio original removed from `flow-label-translations.tsx`. 3. `ui/packages/app-shell/src/views/metadata-admin/previews/flow-label-translations.tsx`: what stays in Studio, and the re-export. Two-minute check: `pnpm --dir ui exec vitest run packages/components/src/__tests__/metadata-viewer.test.tsx`; expect 18 passed, including the three tests under "flow embed: package label translations". ## Tests <details><summary>Pins (one top-level describe in the existing viewer harness)</summary> `metadata-viewer.test.tsx`, describe "element:metadata_viewer flow embed: package label translations (flows.FLOW.label, approvals.STEP.label)", with a real `createI18n` instance loaded from a zh-CN `TranslationData` through `transformSpecTranslations`, inside `I18nProvider`: - an `opportunity_approval`-shaped flow draws 大额商机审批, 销售经理审核 and 销售总监签批, and none of the English approval labels; the start, decision, notify and end steps keep their authored labels; - a bundle entry without the flow label and without one step falls back to the authored text; a bundle that translates only another flow leaves everything authored; - a decision branch whose target is an approval step names it 销售总监签批, and the English label appears nowhere on the card. The Studio pins in `FlowCanvas.test.tsx` exercise the moved resolver unchanged. </details> <details><summary>Runs</summary> - At 78faa00, under the verify lock: `vitest run` of `metadata-viewer.test.tsx` and `FlowCanvas.test.tsx`: `Test Files 2 passed (2)`, `Tests 47 passed (47)`; then `pnpm --filter @object-ui/components run type-check` (both tsc programs); `VERDICT command-exit 0`. - At 10f5352 (first main merge), under the lock: turbo build of the `@object-ui/app-shell^...` and `@object-ui/components^...` closures (68 tasks successful), the same two files (`Tests 40 passed (40)`), and `pnpm --filter @object-ui/i18n --filter @object-ui/components --filter @object-ui/app-shell run type-check`: test exit 0, type-check exit 0. - Ablation at 78faa00 through `scripts/ablation-replace.mjs --delete`: anchor `(target && stepLabel?.(target)) ?? ` 1 to 0, blob e061084 to 1bcbe5a1bdbb. Result: `Tests 1 failed | 17 passed (18)`, the branch-target pin red. Restored: blob equals HEAD and `git diff HEAD` is empty. A first attempt with `--replacement ''` was refused by the tool before any test ran (it cannot count an empty replacement), so it measured nothing. The test resolves the viewer through `src`, so no dist preflight applies. - Earlier ablation of the two label reads (seat 4, at 1ffd047): both original pins red, restored clean. </details> ## Acceptance notes - **Merges.** This branch merged `origin/main` twice. First merge (c261a7c, #36316, already on main): #36316 moved `activeFlowsBundle` into `@object-ui/i18n` at the same path this branch had used. Resolution kept main's `flowsBundle.ts` byte-for-byte, dropped this branch's duplicate move, and kept `flowLabels.ts` and its export. `git grep` on the merged tree: one `function activeFlowsBundle` (`ui/packages/i18n/src/flowsBundle.ts`), no app-shell copy, no `utils/flowsBundle` import left. Second merge (e605773, #36327): kept its branches and entry-condition code and its tests, and added the two label reads plus the branch-target label. - **Measurement for Asked item 2 (built elsewhere).** hotcrm `opportunity_approval` (hotcrm 01d3ec4): 13 nodes; 11 drawn at the default business altitude; 2 have a key (`approval`), 9 do not: 1 start, 3 decision, 4 notify, 1 end. - The embed's subtitle is the authored `flow.description`. The spec's `flows` group has no description key, so it stays authored. - No `content/docs` page documents the viewer (only its ADR, a governed surface), so no docs page changed. - Changeset: `@object-ui/components` and `@object-ui/i18n` patch. `@object-ui/app-shell` is private and its behaviour is unchanged. ## Verification <details><summary>Gates</summary> - `node scripts/pm/dispatch-gates.mjs --commands --repo objectstack-ai/objectstack` at 78faa00: 38 families, the same list as at 10f5352. At 10f5352 all 37 non-vitest families exited 0, each exit code captured before any pipe, and `pnpm --dir ui exec vitest run scripts/` under the lock gave `Test Files 172 passed | 2 skipped (174)`, `VERDICT command-exit 0`. The re-run at the final head is in the report on the card. - `node scripts/pm/overlap-check.mjs` at 5999753: `VERDICT clean exit 0` (origin/main e605773 merges cleanly, no open PR head overlaps). - CI at 10f5352: all eleven required contexts success. </details> --- _Generated by [Claude Code](https://claude.ai/code/session_01QsR2jV3scu6UYPb42n5okQ)_ <!-- signed: repo:objectstack#7 · zhuangjianguo · session_01QsR2jV3scu6UYPb42n5okQ --> --------- Co-authored-by: Claude <noreply@anthropic.com>
1 parent bb89b2d commit dda5e74

6 files changed

Lines changed: 250 additions & 59 deletions

File tree

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
---
2+
'@object-ui/components': patch
3+
'@object-ui/i18n': patch
4+
---
5+
6+
fix(components): a docs `type: flow` metadata embed names the flow and its approval steps in the reader's language
7+
8+
Clause-②: yes (widening)
9+
Metadata-protocol: none
10+
11+
A package can translate a flow's label (`flows.<flow>.label`) and an approval step's label (`flows.<flow>.approvals.<node_id>.label`). The flow runner and the Studio flow designer already showed both, but the docs viewer's `type: flow` embed (`element:metadata_viewer`) drew the authored text, so a zh-CN reader of a manual saw the English flow name and step names. The embed now reads the same bundle, through the same resolution, in the reader's language, and falls back to the authored text where the bundle carries no key. A decision's branch list names the approval step a branch leads to by the same translated label.
12+
13+
The spec has no key yet for a start, decision, notify or end node's label, so those steps still show the authored text.
14+
15+
`@object-ui/i18n` has new public exports for the flow-label resolution (`flowLabelTranslations`, `useFlowLabelTranslations` and the `FlowLabelTranslations` type), moved out of the Studio flow designer and built on the `activeFlowsBundle` reader that package already exports, so the runner, the designer and the docs viewer share one reader of the `flows` group.

‎ui/packages/app-shell/src/views/metadata-admin/previews/flow-label-translations.tsx‎

Lines changed: 14 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -5,15 +5,12 @@
55
* own label (`flows.<flow>.label`) and an `approval` node's step label
66
* (`flows.<flow>.approvals.<node_id>.label`), in the viewer's language.
77
*
8-
* The bundle is the one `FlowRunner` reads, `activeFlowsBundle` over the
9-
* i18next resource tree the console already loaded, and the flow label goes
10-
* through the spec's `translateFlow`, the runner's resolution path. So the
11-
* designer and the runner cannot disagree about a flow's name, and there is no
12-
* second loader.
13-
*
14-
* Only those two keys exist in the spec's `flows` group for a label the
15-
* designer draws. A decision or other node's label and an edge label have no
16-
* key, so they stay the authored text.
8+
* The resolution itself is `@object-ui/i18n`'s (`flowLabelTranslations` over
9+
* `activeFlowsBundle`), the one the runner and the docs viewer's flow embed
10+
* read too, so no two surfaces can disagree about a flow's name and there is
11+
* no second loader. This module adds only what the designer needs on top:
12+
* a per-row reader for the Automations rail and a context that hands the
13+
* translations to the canvas.
1714
*
1815
* Display only: the draft is never rewritten with a translation, so an edit,
1916
* a rename or a save keeps working on the authored label. Studio is the
@@ -23,48 +20,13 @@
2320

2421
import * as React from 'react';
2522
import { useObjectTranslation } from '@object-ui/react';
26-
import { translateFlow, type TranslationBundle } from '@objectstack/spec/system';
27-
import { activeFlowsBundle } from '@object-ui/i18n';
28-
29-
/** A flow's translated labels in one language. Every member answers `undefined` where the bundle carries nothing. */
30-
export interface FlowLabelTranslations {
31-
/** `flows.<flow>.label`. */
32-
flowLabel?: string;
33-
/** `flows.<flow>.approvals.<node_id>.label`, for an `approval` node only. */
34-
stepLabel: (node: { id?: unknown; type?: unknown }) => string | undefined;
35-
}
23+
import {
24+
activeFlowsBundle,
25+
flowLabelTranslations,
26+
type FlowLabelTranslations,
27+
} from '@object-ui/i18n';
3628

37-
/**
38-
* The translated labels for `flowName` out of `bundle` in `language`, or
39-
* `undefined` when there is no bundle (nothing to translate with).
40-
*/
41-
export function flowLabelTranslations(
42-
bundle: TranslationBundle | undefined,
43-
flowName: string,
44-
language: string,
45-
): FlowLabelTranslations | undefined {
46-
if (!bundle || !flowName) return undefined;
47-
const flowLabel = translateFlow<{ name: string; label?: string }>({ name: flowName }, bundle, { locale: language }).label;
48-
const approvals = bundle[language]?.flows?.[flowName]?.approvals;
49-
return {
50-
flowLabel: typeof flowLabel === 'string' && flowLabel ? flowLabel : undefined,
51-
stepLabel: (node) => {
52-
if (node.type !== 'approval' || typeof node.id !== 'string' || !node.id) return undefined;
53-
const label = approvals?.[node.id]?.label;
54-
return typeof label === 'string' && label ? label : undefined;
55-
},
56-
};
57-
}
58-
59-
/**
60-
* {@link flowLabelTranslations} for `flowName` in the console's active
61-
* language. Recomputed on every render: a handful of property reads that
62-
* follow the live resource tree and a language switch.
63-
*/
64-
export function useFlowLabelTranslations(flowName: string): FlowLabelTranslations | undefined {
65-
const { i18n, language } = useObjectTranslation();
66-
return flowLabelTranslations(activeFlowsBundle(i18n, language, flowName), flowName, language);
67-
}
29+
export { flowLabelTranslations, useFlowLabelTranslations, type FlowLabelTranslations } from '@object-ui/i18n';
6830

6931
/**
7032
* The flow label of any flow, by name, for a surface that lists many: the
@@ -73,7 +35,8 @@ export function useFlowLabelTranslations(flowName: string): FlowLabelTranslation
7335
* the reading the designer header makes for the open flow, so a row and the
7436
* flow it opens cannot disagree. `undefined` where the bundle carries no label
7537
* for that flow, and the caller keeps the authored label. A fresh function on
76-
* every render, for the reason above.
38+
* every render: a handful of property reads that follow the live resource tree
39+
* and a language switch.
7740
*/
7841
export function useFlowLabelOf(): (flowName: string) => string | undefined {
7942
const { i18n, language } = useObjectTranslation();

‎ui/packages/components/src/__tests__/metadata-viewer.test.tsx‎

Lines changed: 116 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,8 @@ import { describe, it, expect, vi } from 'vitest';
1111
import { render, screen, waitFor, cleanup } from '@testing-library/react';
1212
import { ComponentRegistry } from '@object-ui/core';
1313
import { MetadataCtx } from '@object-ui/react';
14-
import { LocalizationProvider } from '@object-ui/i18n';
14+
import { createI18n, I18nProvider, LocalizationProvider, transformSpecTranslations } from '@object-ui/i18n';
15+
import type { TranslationData } from '@objectstack/spec/system';
1516
import '../renderers/basic/metadata-viewer';
1617

1718
function metaCtx(getItem: (type: string, name: string) => Promise<any>) {
@@ -197,6 +198,120 @@ describe('element:metadata_viewer', () => {
197198
});
198199
});
199200

201+
202+
/**
203+
* A `type: flow` embed names the flow and each step the spec lets a package
204+
* translate in the reader's language: `flows.<flow>.label` and an `approval`
205+
* node's `flows.<flow>.approvals.<node_id>.label`, read from the package bundle
206+
* the host loaded, through the resolution the Studio flow designer uses. A step
207+
* with no key (start, decision, notify, end) and a key the bundle does not
208+
* carry fall back to the authored text.
209+
*/
210+
describe('element:metadata_viewer flow embed: package label translations (flows.FLOW.label, approvals.STEP.label)', () => {
211+
const OPPORTUNITY_APPROVAL = {
212+
name: 'opportunity_approval',
213+
label: 'Large Deal Approval',
214+
nodes: [
215+
{ id: 'start', type: 'start', label: 'Start' },
216+
{ id: 'get_opportunity', type: 'get_record', label: 'Get Opportunity' },
217+
{ id: 'manager_review', type: 'approval', label: 'Sales Manager Review' },
218+
{ id: 'check_high_value', type: 'decision', label: 'High Value (> $500K)?' },
219+
{ id: 'director_signoff', type: 'approval', label: 'Sales Director Sign-off' },
220+
{ id: 'notify_approved', type: 'notify', label: 'Email Owner — Approved' },
221+
{ id: 'end', type: 'end', label: 'End' },
222+
],
223+
};
224+
225+
function zhCN(data: TranslationData) {
226+
const instance = createI18n({ defaultLanguage: 'zh-CN', detectBrowserLanguage: false });
227+
instance.addResourceBundle('zh-CN', 'translation', transformSpecTranslations(data as Record<string, unknown>), true, true);
228+
return instance;
229+
}
230+
231+
function renderIn(data: TranslationData, flow = OPPORTUNITY_APPROVAL) {
232+
const getItem = vi.fn(async () => flow);
233+
return render(
234+
<I18nProvider instance={zhCN(data)} persistLanguage={false}>
235+
<Viewer getItem={getItem} properties={{ type: 'flow', name: flow.name }} />
236+
</I18nProvider>,
237+
);
238+
}
239+
240+
it('draws the translated flow label and approval-step labels; a step with no key keeps its authored label', async () => {
241+
renderIn({
242+
flows: {
243+
opportunity_approval: {
244+
label: '大额商机审批',
245+
approvals: { manager_review: { label: '销售经理审核' }, director_signoff: { label: '销售总监签批' } },
246+
},
247+
},
248+
});
249+
await waitFor(() => expect(screen.getByText('大额商机审批')).toBeTruthy());
250+
expect(screen.queryByText('Large Deal Approval')).toBeNull();
251+
expect(screen.getByText('销售经理审核')).toBeTruthy();
252+
expect(screen.getByText('销售总监签批')).toBeTruthy();
253+
expect(screen.queryByText('Sales Manager Review')).toBeNull();
254+
expect(screen.queryByText('Sales Director Sign-off')).toBeNull();
255+
// start, decision, notify and end have no key in the spec's flows group.
256+
for (const authored of ['Start', 'High Value (> $500K)?', 'Email Owner — Approved', 'End']) {
257+
expect(screen.getByText(authored)).toBeTruthy();
258+
}
259+
});
260+
261+
it('falls back to the authored text where the bundle carries no key', async () => {
262+
// The flow is in the bundle, but neither its label nor one approval step is.
263+
renderIn({ flows: { opportunity_approval: { approvals: { manager_review: { label: '销售经理审核' } } } } });
264+
await waitFor(() => expect(screen.getByText('Large Deal Approval')).toBeTruthy());
265+
expect(screen.getByText('销售经理审核')).toBeTruthy();
266+
expect(screen.getByText('Sales Director Sign-off')).toBeTruthy();
267+
cleanup();
268+
269+
// The bundle translates another flow only: everything stays authored.
270+
renderIn({ flows: { other_flow: { label: '其他流程' } } });
271+
await waitFor(() => expect(screen.getByText('Large Deal Approval')).toBeTruthy());
272+
expect(screen.getByText('Sales Manager Review')).toBeTruthy();
273+
});
274+
275+
it('names an approval step a decision branch leads to by its translated label too', async () => {
276+
const cel = (source: string) => ({ dialect: 'cel', source });
277+
const flow = {
278+
name: 'opportunity_approval',
279+
label: 'Large Deal Approval',
280+
nodes: [
281+
{ id: 'start', type: 'start', label: 'Start', config: { objectName: 'crm_opportunity' } },
282+
{ id: 'get_opportunity', type: 'get_record', label: 'Get Opportunity', config: { objectName: 'crm_opportunity', outputVariable: 'oppRecord' } },
283+
{ id: 'check_high_value', type: 'decision', label: 'High Value?' },
284+
{ id: 'director_signoff', type: 'approval', label: 'Sales Director Sign-off' },
285+
{ id: 'end', type: 'end', label: 'End' },
286+
],
287+
edges: [
288+
{ id: 'e1', source: 'start', target: 'get_opportunity' },
289+
{ id: 'e2', source: 'get_opportunity', target: 'check_high_value' },
290+
{ id: 'e3', source: 'check_high_value', target: 'director_signoff', type: 'conditional',
291+
condition: cel('vars.oppRecord.amount > 500000') },
292+
{ id: 'e4', source: 'check_high_value', target: 'end', isDefault: true },
293+
{ id: 'e5', source: 'director_signoff', target: 'end' },
294+
],
295+
};
296+
const deal = { name: 'crm_opportunity', fields: { amount: { type: 'currency', label: 'Amount' } } };
297+
const getItem = vi.fn(async (type: string) => (type === 'object' ? deal : flow));
298+
const { container } = render(
299+
<I18nProvider
300+
instance={zhCN({ flows: { opportunity_approval: { approvals: { director_signoff: { label: '销售总监签批' } } } } })}
301+
persistLanguage={false}
302+
>
303+
<Viewer getItem={getItem} properties={{ type: 'flow', name: 'opportunity_approval' }} />
304+
</I18nProvider>,
305+
);
306+
await waitFor(() =>
307+
expect(container.querySelector('[data-flow-branches="check_high_value"] [data-branch-target="director_signoff"]')).toBeTruthy(),
308+
);
309+
const target = container.querySelector('[data-flow-branches="check_high_value"] [data-branch-target="director_signoff"]')!;
310+
expect(target.textContent).toContain('销售总监签批');
311+
expect(container.textContent).not.toContain('Sales Director Sign-off');
312+
});
313+
});
314+
200315
// objectstack#36094 — the business view draws each decision's branches with
201316
// its condition read from the LIVE definition: the served flow, the edges'
202317
// `{ dialect: 'cel', source }` envelopes, and the label and currency of the

‎ui/packages/components/src/renderers/basic/metadata-viewer.tsx‎

Lines changed: 24 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ import {
2626
formatNumber,
2727
resolveFieldCurrency,
2828
useDisplayLocale,
29+
useFlowLabelTranslations,
2930
useLocalization,
3031
useObjectTranslation,
3132
} from '@object-ui/i18n';
@@ -498,7 +499,18 @@ function EntryCondition({ flow }: { flow: unknown }) {
498499
}
499500

500501
/** A decision's branches: each condition (or the neutral marker) and the step it leads to. */
501-
function Branches({ flow, node, nodes }: { flow: unknown; node: any; nodes: any[] }) {
502+
function Branches({
503+
flow,
504+
node,
505+
nodes,
506+
stepLabel,
507+
}: {
508+
flow: unknown;
509+
node: any;
510+
nodes: any[];
511+
/** The step's translated label (`flows.<flow>.approvals.<node_id>.label`), or `undefined`. */
512+
stepLabel?: (node: any) => string | undefined;
513+
}) {
502514
const ctx = React.useContext(ConditionScope);
503515
const branches = decisionBranches(flow, node);
504516
if (!ctx || branches.length === 0) return null;
@@ -510,7 +522,7 @@ function Branches({ flow, node, nodes }: { flow: unknown; node: any; nodes: any[
510522
<li key={b.key} data-branch-target={b.target} className="flex flex-wrap items-center gap-1.5">
511523
<ConditionText guard={b.guard} ctx={ctx} />
512524
<ArrowRight className="h-3 w-3 shrink-0 text-muted-foreground" />
513-
<span>{target?.label ?? b.target}</span>
525+
<span>{(target && stepLabel?.(target)) ?? target?.label ?? b.target}</span>
514526
</li>
515527
);
516528
})}
@@ -521,6 +533,10 @@ function Branches({ flow, node, nodes }: { flow: unknown; node: any; nodes: any[
521533
function FlowView({ name, detail }: ViewerProps) {
522534
const { item: flow, loading, error } = useMetadataItem('flow', name ?? null);
523535
const { t, ...view } = useViewStates();
536+
// The package's `flows` translations in the reader's language: the flow label
537+
// and each `approval` step's label, the resolution the Studio designer and
538+
// the runner share. Any other step has no key and keeps its authored label.
539+
const translated = useFlowLabelTranslations(name ?? '');
524540
const business = (detail ?? 'business') === 'business';
525541

526542
if (!name) return view.missing('name');
@@ -536,7 +552,7 @@ function FlowView({ name, detail }: ViewerProps) {
536552
<Shell
537553
hint={t('element.metadataViewer.flow', { defaultValue: 'Flow' })}
538554
icon={GitBranch}
539-
title={flow.label ?? flow.name ?? name}
555+
title={translated?.flowLabel ?? flow.label ?? flow.name ?? name}
540556
subtitle={
541557
flow.description ??
542558
t('element.metadataViewer.steps', { defaultValue: 'Steps: {{count}}', count: allNodes.length })
@@ -553,14 +569,17 @@ function FlowView({ name, detail }: ViewerProps) {
553569
{i + 1}
554570
</span>
555571
<span className="text-sm">
556-
{n?.label ?? n?.id ?? t('element.metadataViewer.untitledStep', { defaultValue: '(step)' })}
572+
{(n && translated?.stepLabel(n)) ??
573+
n?.label ??
574+
n?.id ??
575+
t('element.metadataViewer.untitledStep', { defaultValue: '(step)' })}
557576
</span>
558577
{n?.type && (
559578
<span className="ml-auto rounded border bg-background px-1.5 py-0.5 font-mono text-xs text-muted-foreground">
560579
{n.type}
561580
</span>
562581
)}
563-
{business && <Branches flow={flow} node={n} nodes={allNodes} />}
582+
{business && <Branches flow={flow} node={n} nodes={allNodes} stepLabel={translated?.stepLabel} />}
564583
</li>
565584
))}
566585
</ol>

‎ui/packages/i18n/src/flowLabels.ts‎

Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
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+
* The two flow labels a surface that DRAWS a flow resolves from the app's
11+
* `flows` translation group (`activeFlowsBundle`, `flowsBundle.ts`): the
12+
* flow's own label (`flows.<flow>.label`) and an `approval` node's step label
13+
* (`flows.<flow>.approvals.<node_id>.label`).
14+
*
15+
* The Studio flow designer and the docs viewer's `type: flow` embed
16+
* (`element:metadata_viewer`) both read them here, and the flow label goes
17+
* through the spec's `translateFlow`, the runner's resolution path, so no two
18+
* surfaces can disagree about what a flow or a step is called.
19+
*
20+
* Only those two keys exist in the spec's `flows` group for a label a flow
21+
* drawing shows. A start, decision, notify or end node's label and an edge
22+
* label have no key, so they stay the authored text.
23+
*
24+
* Display only: nothing here rewrites the flow, so an editor keeps working on
25+
* the authored label.
26+
*/
27+
28+
import { translateFlow, type TranslationBundle } from '@objectstack/spec/system';
29+
import { useObjectTranslation } from './provider.js';
30+
import { activeFlowsBundle } from './flowsBundle.js';
31+
32+
/** A flow's translated labels in one language. Every member answers `undefined` where the bundle carries nothing. */
33+
export interface FlowLabelTranslations {
34+
/** `flows.<flow>.label`. */
35+
flowLabel?: string;
36+
/** `flows.<flow>.approvals.<node_id>.label`, for an `approval` node only. */
37+
stepLabel: (node: { id?: unknown; type?: unknown }) => string | undefined;
38+
}
39+
40+
/**
41+
* The translated labels for `flowName` out of `bundle` in `language`, or
42+
* `undefined` when there is no bundle (nothing to translate with).
43+
*
44+
* The flow label goes through the spec's `translateFlow`, the runner's
45+
* resolution path; the step label is the spec-typed address
46+
* `flows.<flow>.approvals.<node_id>.label`, read for `approval` nodes only.
47+
*/
48+
export function flowLabelTranslations(
49+
bundle: TranslationBundle | undefined,
50+
flowName: string,
51+
language: string,
52+
): FlowLabelTranslations | undefined {
53+
if (!bundle || !flowName) return undefined;
54+
const flowLabel = translateFlow<{ name: string; label?: string }>({ name: flowName }, bundle, { locale: language }).label;
55+
const approvals = bundle[language]?.flows?.[flowName]?.approvals;
56+
return {
57+
flowLabel: typeof flowLabel === 'string' && flowLabel ? flowLabel : undefined,
58+
stepLabel: (node) => {
59+
if (node.type !== 'approval' || typeof node.id !== 'string' || !node.id) return undefined;
60+
const label = approvals?.[node.id]?.label;
61+
return typeof label === 'string' && label ? label : undefined;
62+
},
63+
};
64+
}
65+
66+
/**
67+
* {@link flowLabelTranslations} for `flowName` in the provider's active
68+
* language. Recomputed on every render: a handful of property reads that
69+
* follow the live resource tree and a language switch.
70+
*/
71+
export function useFlowLabelTranslations(flowName: string): FlowLabelTranslations | undefined {
72+
const { i18n, language } = useObjectTranslation();
73+
return flowLabelTranslations(activeFlowsBundle(i18n, language, flowName), flowName, language);
74+
}

0 commit comments

Comments
 (0)