diff --git a/.changeset/35979-flow-embed-labels.md b/.changeset/35979-flow-embed-labels.md new file mode 100644 index 00000000000..7a968366dbb --- /dev/null +++ b/.changeset/35979-flow-embed-labels.md @@ -0,0 +1,15 @@ +--- +'@object-ui/components': patch +'@object-ui/i18n': patch +--- + +fix(components): a docs `type: flow` metadata embed names the flow and its approval steps in the reader's language + +Clause-②: yes (widening) +Metadata-protocol: none + +A package can translate a flow's label (`flows..label`) and an approval step's label (`flows..approvals..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. + +The spec has no key yet for a start, decision, notify or end node's label, so those steps still show the authored text. + +`@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. diff --git a/ui/packages/app-shell/src/views/metadata-admin/previews/flow-label-translations.tsx b/ui/packages/app-shell/src/views/metadata-admin/previews/flow-label-translations.tsx index 7ff49a1498e..9840ce7ea6b 100644 --- a/ui/packages/app-shell/src/views/metadata-admin/previews/flow-label-translations.tsx +++ b/ui/packages/app-shell/src/views/metadata-admin/previews/flow-label-translations.tsx @@ -5,15 +5,12 @@ * own label (`flows..label`) and an `approval` node's step label * (`flows..approvals..label`), in the viewer's language. * - * The bundle is the one `FlowRunner` reads, `activeFlowsBundle` over the - * i18next resource tree the console already loaded, and the flow label goes - * through the spec's `translateFlow`, the runner's resolution path. So the - * designer and the runner cannot disagree about a flow's name, and there is no - * second loader. - * - * Only those two keys exist in the spec's `flows` group for a label the - * designer draws. A decision or other node's label and an edge label have no - * key, so they stay the authored text. + * The resolution itself is `@object-ui/i18n`'s (`flowLabelTranslations` over + * `activeFlowsBundle`), the one the runner and the docs viewer's flow embed + * read too, so no two surfaces can disagree about a flow's name and there is + * no second loader. This module adds only what the designer needs on top: + * a per-row reader for the Automations rail and a context that hands the + * translations to the canvas. * * Display only: the draft is never rewritten with a translation, so an edit, * a rename or a save keeps working on the authored label. Studio is the @@ -23,48 +20,13 @@ import * as React from 'react'; import { useObjectTranslation } from '@object-ui/react'; -import { translateFlow, type TranslationBundle } from '@objectstack/spec/system'; -import { activeFlowsBundle } from '@object-ui/i18n'; - -/** A flow's translated labels in one language. Every member answers `undefined` where the bundle carries nothing. */ -export interface FlowLabelTranslations { - /** `flows..label`. */ - flowLabel?: string; - /** `flows..approvals..label`, for an `approval` node only. */ - stepLabel: (node: { id?: unknown; type?: unknown }) => string | undefined; -} +import { + activeFlowsBundle, + flowLabelTranslations, + type FlowLabelTranslations, +} from '@object-ui/i18n'; -/** - * The translated labels for `flowName` out of `bundle` in `language`, or - * `undefined` when there is no bundle (nothing to translate with). - */ -export function flowLabelTranslations( - bundle: TranslationBundle | undefined, - flowName: string, - language: string, -): FlowLabelTranslations | undefined { - if (!bundle || !flowName) return undefined; - const flowLabel = translateFlow<{ name: string; label?: string }>({ name: flowName }, bundle, { locale: language }).label; - const approvals = bundle[language]?.flows?.[flowName]?.approvals; - return { - flowLabel: typeof flowLabel === 'string' && flowLabel ? flowLabel : undefined, - stepLabel: (node) => { - if (node.type !== 'approval' || typeof node.id !== 'string' || !node.id) return undefined; - const label = approvals?.[node.id]?.label; - return typeof label === 'string' && label ? label : undefined; - }, - }; -} - -/** - * {@link flowLabelTranslations} for `flowName` in the console's active - * language. Recomputed on every render: a handful of property reads that - * follow the live resource tree and a language switch. - */ -export function useFlowLabelTranslations(flowName: string): FlowLabelTranslations | undefined { - const { i18n, language } = useObjectTranslation(); - return flowLabelTranslations(activeFlowsBundle(i18n, language, flowName), flowName, language); -} +export { flowLabelTranslations, useFlowLabelTranslations, type FlowLabelTranslations } from '@object-ui/i18n'; /** * 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 * the reading the designer header makes for the open flow, so a row and the * flow it opens cannot disagree. `undefined` where the bundle carries no label * for that flow, and the caller keeps the authored label. A fresh function on - * every render, for the reason above. + * every render: a handful of property reads that follow the live resource tree + * and a language switch. */ export function useFlowLabelOf(): (flowName: string) => string | undefined { const { i18n, language } = useObjectTranslation(); diff --git a/ui/packages/components/src/__tests__/metadata-viewer.test.tsx b/ui/packages/components/src/__tests__/metadata-viewer.test.tsx index d3846f4c90d..f372841a62a 100644 --- a/ui/packages/components/src/__tests__/metadata-viewer.test.tsx +++ b/ui/packages/components/src/__tests__/metadata-viewer.test.tsx @@ -11,7 +11,8 @@ import { describe, it, expect, vi } from 'vitest'; import { render, screen, waitFor, cleanup } from '@testing-library/react'; import { ComponentRegistry } from '@object-ui/core'; import { MetadataCtx } from '@object-ui/react'; -import { LocalizationProvider } from '@object-ui/i18n'; +import { createI18n, I18nProvider, LocalizationProvider, transformSpecTranslations } from '@object-ui/i18n'; +import type { TranslationData } from '@objectstack/spec/system'; import '../renderers/basic/metadata-viewer'; function metaCtx(getItem: (type: string, name: string) => Promise) { @@ -197,6 +198,120 @@ describe('element:metadata_viewer', () => { }); }); + +/** + * A `type: flow` embed names the flow and each step the spec lets a package + * translate in the reader's language: `flows..label` and an `approval` + * node's `flows..approvals..label`, read from the package bundle + * the host loaded, through the resolution the Studio flow designer uses. A step + * with no key (start, decision, notify, end) and a key the bundle does not + * carry fall back to the authored text. + */ +describe('element:metadata_viewer flow embed: package label translations (flows.FLOW.label, approvals.STEP.label)', () => { + const OPPORTUNITY_APPROVAL = { + name: 'opportunity_approval', + label: 'Large Deal Approval', + nodes: [ + { id: 'start', type: 'start', label: 'Start' }, + { id: 'get_opportunity', type: 'get_record', label: 'Get Opportunity' }, + { id: 'manager_review', type: 'approval', label: 'Sales Manager Review' }, + { id: 'check_high_value', type: 'decision', label: 'High Value (> $500K)?' }, + { id: 'director_signoff', type: 'approval', label: 'Sales Director Sign-off' }, + { id: 'notify_approved', type: 'notify', label: 'Email Owner — Approved' }, + { id: 'end', type: 'end', label: 'End' }, + ], + }; + + function zhCN(data: TranslationData) { + const instance = createI18n({ defaultLanguage: 'zh-CN', detectBrowserLanguage: false }); + instance.addResourceBundle('zh-CN', 'translation', transformSpecTranslations(data as Record), true, true); + return instance; + } + + function renderIn(data: TranslationData, flow = OPPORTUNITY_APPROVAL) { + const getItem = vi.fn(async () => flow); + return render( + + + , + ); + } + + it('draws the translated flow label and approval-step labels; a step with no key keeps its authored label', async () => { + renderIn({ + flows: { + opportunity_approval: { + label: '大额商机审批', + approvals: { manager_review: { label: '销售经理审核' }, director_signoff: { label: '销售总监签批' } }, + }, + }, + }); + await waitFor(() => expect(screen.getByText('大额商机审批')).toBeTruthy()); + expect(screen.queryByText('Large Deal Approval')).toBeNull(); + expect(screen.getByText('销售经理审核')).toBeTruthy(); + expect(screen.getByText('销售总监签批')).toBeTruthy(); + expect(screen.queryByText('Sales Manager Review')).toBeNull(); + expect(screen.queryByText('Sales Director Sign-off')).toBeNull(); + // start, decision, notify and end have no key in the spec's flows group. + for (const authored of ['Start', 'High Value (> $500K)?', 'Email Owner — Approved', 'End']) { + expect(screen.getByText(authored)).toBeTruthy(); + } + }); + + it('falls back to the authored text where the bundle carries no key', async () => { + // The flow is in the bundle, but neither its label nor one approval step is. + renderIn({ flows: { opportunity_approval: { approvals: { manager_review: { label: '销售经理审核' } } } } }); + await waitFor(() => expect(screen.getByText('Large Deal Approval')).toBeTruthy()); + expect(screen.getByText('销售经理审核')).toBeTruthy(); + expect(screen.getByText('Sales Director Sign-off')).toBeTruthy(); + cleanup(); + + // The bundle translates another flow only: everything stays authored. + renderIn({ flows: { other_flow: { label: '其他流程' } } }); + await waitFor(() => expect(screen.getByText('Large Deal Approval')).toBeTruthy()); + expect(screen.getByText('Sales Manager Review')).toBeTruthy(); + }); + + it('names an approval step a decision branch leads to by its translated label too', async () => { + const cel = (source: string) => ({ dialect: 'cel', source }); + const flow = { + name: 'opportunity_approval', + label: 'Large Deal Approval', + nodes: [ + { id: 'start', type: 'start', label: 'Start', config: { objectName: 'crm_opportunity' } }, + { id: 'get_opportunity', type: 'get_record', label: 'Get Opportunity', config: { objectName: 'crm_opportunity', outputVariable: 'oppRecord' } }, + { id: 'check_high_value', type: 'decision', label: 'High Value?' }, + { id: 'director_signoff', type: 'approval', label: 'Sales Director Sign-off' }, + { id: 'end', type: 'end', label: 'End' }, + ], + edges: [ + { id: 'e1', source: 'start', target: 'get_opportunity' }, + { id: 'e2', source: 'get_opportunity', target: 'check_high_value' }, + { id: 'e3', source: 'check_high_value', target: 'director_signoff', type: 'conditional', + condition: cel('vars.oppRecord.amount > 500000') }, + { id: 'e4', source: 'check_high_value', target: 'end', isDefault: true }, + { id: 'e5', source: 'director_signoff', target: 'end' }, + ], + }; + const deal = { name: 'crm_opportunity', fields: { amount: { type: 'currency', label: 'Amount' } } }; + const getItem = vi.fn(async (type: string) => (type === 'object' ? deal : flow)); + const { container } = render( + + + , + ); + await waitFor(() => + expect(container.querySelector('[data-flow-branches="check_high_value"] [data-branch-target="director_signoff"]')).toBeTruthy(), + ); + const target = container.querySelector('[data-flow-branches="check_high_value"] [data-branch-target="director_signoff"]')!; + expect(target.textContent).toContain('销售总监签批'); + expect(container.textContent).not.toContain('Sales Director Sign-off'); + }); +}); + // objectstack#36094 — the business view draws each decision's branches with // its condition read from the LIVE definition: the served flow, the edges' // `{ dialect: 'cel', source }` envelopes, and the label and currency of the diff --git a/ui/packages/components/src/renderers/basic/metadata-viewer.tsx b/ui/packages/components/src/renderers/basic/metadata-viewer.tsx index b4e787426f1..e0610844af5 100644 --- a/ui/packages/components/src/renderers/basic/metadata-viewer.tsx +++ b/ui/packages/components/src/renderers/basic/metadata-viewer.tsx @@ -26,6 +26,7 @@ import { formatNumber, resolveFieldCurrency, useDisplayLocale, + useFlowLabelTranslations, useLocalization, useObjectTranslation, } from '@object-ui/i18n'; @@ -498,7 +499,18 @@ function EntryCondition({ flow }: { flow: unknown }) { } /** A decision's branches: each condition (or the neutral marker) and the step it leads to. */ -function Branches({ flow, node, nodes }: { flow: unknown; node: any; nodes: any[] }) { +function Branches({ + flow, + node, + nodes, + stepLabel, +}: { + flow: unknown; + node: any; + nodes: any[]; + /** The step's translated label (`flows..approvals..label`), or `undefined`. */ + stepLabel?: (node: any) => string | undefined; +}) { const ctx = React.useContext(ConditionScope); const branches = decisionBranches(flow, node); if (!ctx || branches.length === 0) return null; @@ -510,7 +522,7 @@ function Branches({ flow, node, nodes }: { flow: unknown; node: any; nodes: any[
  • - {target?.label ?? b.target} + {(target && stepLabel?.(target)) ?? target?.label ?? b.target}
  • ); })} @@ -521,6 +533,10 @@ function Branches({ flow, node, nodes }: { flow: unknown; node: any; nodes: any[ function FlowView({ name, detail }: ViewerProps) { const { item: flow, loading, error } = useMetadataItem('flow', name ?? null); const { t, ...view } = useViewStates(); + // The package's `flows` translations in the reader's language: the flow label + // and each `approval` step's label, the resolution the Studio designer and + // the runner share. Any other step has no key and keeps its authored label. + const translated = useFlowLabelTranslations(name ?? ''); const business = (detail ?? 'business') === 'business'; if (!name) return view.missing('name'); @@ -536,7 +552,7 @@ function FlowView({ name, detail }: ViewerProps) { - {n?.label ?? n?.id ?? t('element.metadataViewer.untitledStep', { defaultValue: '(step)' })} + {(n && translated?.stepLabel(n)) ?? + n?.label ?? + n?.id ?? + t('element.metadataViewer.untitledStep', { defaultValue: '(step)' })} {n?.type && ( {n.type} )} - {business && } + {business && } ))} diff --git a/ui/packages/i18n/src/flowLabels.ts b/ui/packages/i18n/src/flowLabels.ts new file mode 100644 index 00000000000..5f1bac2999e --- /dev/null +++ b/ui/packages/i18n/src/flowLabels.ts @@ -0,0 +1,74 @@ +/** + * ObjectUI + * Copyright (c) 2024-present ObjectStack Inc. + * + * This source code is licensed under the MIT license found in the + * LICENSE file in the root directory of this source tree. + */ + +/** + * The two flow labels a surface that DRAWS a flow resolves from the app's + * `flows` translation group (`activeFlowsBundle`, `flowsBundle.ts`): the + * flow's own label (`flows..label`) and an `approval` node's step label + * (`flows..approvals..label`). + * + * The Studio flow designer and the docs viewer's `type: flow` embed + * (`element:metadata_viewer`) both read them here, and the flow label goes + * through the spec's `translateFlow`, the runner's resolution path, so no two + * surfaces can disagree about what a flow or a step is called. + * + * Only those two keys exist in the spec's `flows` group for a label a flow + * drawing shows. A start, decision, notify or end node's label and an edge + * label have no key, so they stay the authored text. + * + * Display only: nothing here rewrites the flow, so an editor keeps working on + * the authored label. + */ + +import { translateFlow, type TranslationBundle } from '@objectstack/spec/system'; +import { useObjectTranslation } from './provider.js'; +import { activeFlowsBundle } from './flowsBundle.js'; + +/** A flow's translated labels in one language. Every member answers `undefined` where the bundle carries nothing. */ +export interface FlowLabelTranslations { + /** `flows..label`. */ + flowLabel?: string; + /** `flows..approvals..label`, for an `approval` node only. */ + stepLabel: (node: { id?: unknown; type?: unknown }) => string | undefined; +} + +/** + * The translated labels for `flowName` out of `bundle` in `language`, or + * `undefined` when there is no bundle (nothing to translate with). + * + * The flow label goes through the spec's `translateFlow`, the runner's + * resolution path; the step label is the spec-typed address + * `flows..approvals..label`, read for `approval` nodes only. + */ +export function flowLabelTranslations( + bundle: TranslationBundle | undefined, + flowName: string, + language: string, +): FlowLabelTranslations | undefined { + if (!bundle || !flowName) return undefined; + const flowLabel = translateFlow<{ name: string; label?: string }>({ name: flowName }, bundle, { locale: language }).label; + const approvals = bundle[language]?.flows?.[flowName]?.approvals; + return { + flowLabel: typeof flowLabel === 'string' && flowLabel ? flowLabel : undefined, + stepLabel: (node) => { + if (node.type !== 'approval' || typeof node.id !== 'string' || !node.id) return undefined; + const label = approvals?.[node.id]?.label; + return typeof label === 'string' && label ? label : undefined; + }, + }; +} + +/** + * {@link flowLabelTranslations} for `flowName` in the provider's active + * language. Recomputed on every render: a handful of property reads that + * follow the live resource tree and a language switch. + */ +export function useFlowLabelTranslations(flowName: string): FlowLabelTranslations | undefined { + const { i18n, language } = useObjectTranslation(); + return flowLabelTranslations(activeFlowsBundle(i18n, language, flowName), flowName, language); +} diff --git a/ui/packages/i18n/src/index.ts b/ui/packages/i18n/src/index.ts index d84cd8c8ed8..95656d9fac4 100644 --- a/ui/packages/i18n/src/index.ts +++ b/ui/packages/i18n/src/index.ts @@ -83,6 +83,10 @@ export type { TranslateFn } from './translateFn.js'; // Convention-based object/field label i18n export { useObjectLabel, useSafeFieldLabel } from './useObjectLabel.js'; +// The flow labels a surface that draws a flow reads from the app's `flows` +// group: the Studio flow designer and the docs viewer's `type: flow` embed. +export { flowLabelTranslations, useFlowLabelTranslations, type FlowLabelTranslations } from './flowLabels.js'; + // The record-named save message every record save surface toasts. export { recordSaveMessage, type RecordSaveMessageInput } from './recordSaveMessage.js'; @@ -194,8 +198,9 @@ export { firstDayOfWeek, type WeekdayIndex } from './utils/index.js'; // The app's `flows` translation group, read back out of the loaded resource // tree: the ONE reader every surface that names a flow goes through — the -// console's flow runner and designer, and the stage-change confirms that name -// the flows a move would start (objectstack#35928). +// console's flow runner and designer, the stage-change confirms that name +// the flows a move would start (objectstack#35928), and the docs viewer's +// flow embed. export { activeFlowsBundle, type FlowsTranslation,