|
| 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 Studio field editor's currency section: a currency amount's |
| 11 | + * `currencyConfig` (ADR-0144 D2), in its three modes — |
| 12 | + * |
| 13 | + * - `dynamic` (the default): no currency of its own; the organization |
| 14 | + * currency displays; |
| 15 | + * - `fixed`: one currency, `defaultCurrency`; |
| 16 | + * - `record`: each record names its own, in the sibling code field |
| 17 | + * `currencyField` points at. |
| 18 | + * |
| 19 | + * The pointer is picked from the object's own record-currency code fields — |
| 20 | + * `text` fields declaring `valueDomain: 'iso_4217_currency'`, the only fields |
| 21 | + * the spec accepts there — and "Create currency field" writes one such field |
| 22 | + * with explicit metadata and points at it, so the author never has to know the |
| 23 | + * convention to get a valid declaration. Nothing here guesses a code or a |
| 24 | + * pointer: an unpicked pointer stays absent and the spec's own refusal says so |
| 25 | + * on save. |
| 26 | + */ |
| 27 | + |
| 28 | +import * as React from 'react'; |
| 29 | +import { Button } from '@object-ui/components'; |
| 30 | +import { Plus } from 'lucide-react'; |
| 31 | +import { InspectorSelectField, InspectorTextField } from './_shared.js'; |
| 32 | +import type { FieldEntry } from '../previews/object-fields-io.js'; |
| 33 | + |
| 34 | +/** The record-currency code field's value domain (ADR-0144 D2). */ |
| 35 | +export const CURRENCY_CODE_VALUE_DOMAIN = 'iso_4217_currency'; |
| 36 | + |
| 37 | +/** The naming convention for a record-currency code field (guidance, never a contract — ADR-0144 D8 rule 2). */ |
| 38 | +export const CURRENCY_CODE_FIELD_NAME = 'currency_code'; |
| 39 | + |
| 40 | +export type CurrencyMode = 'dynamic' | 'fixed' | 'record'; |
| 41 | + |
| 42 | +export interface CurrencyConfigDraft { |
| 43 | + currencyMode?: CurrencyMode; |
| 44 | + defaultCurrency?: string; |
| 45 | + currencyField?: string; |
| 46 | +} |
| 47 | + |
| 48 | +/** Whether a field def is a record-currency code field: `text` + the ISO 4217 domain. */ |
| 49 | +export function isCurrencyCodeFieldDef(def: Record<string, unknown> | undefined): boolean { |
| 50 | + return def?.type === 'text' && def.valueDomain === CURRENCY_CODE_VALUE_DOMAIN; |
| 51 | +} |
| 52 | + |
| 53 | +/** The fields of `entries` (other than `self`) a `record`-mode pointer may name. */ |
| 54 | +export function currencyCodeFieldCandidates(entries: readonly FieldEntry[], self: string): string[] { |
| 55 | + return entries.filter((e) => e.name !== self && isCurrencyCodeFieldDef(e.def)).map((e) => e.name); |
| 56 | +} |
| 57 | + |
| 58 | +/** `currency_code`, or `currency_code_2`, `_3`, … when the name is taken. */ |
| 59 | +export function nextCurrencyCodeFieldName(entries: readonly FieldEntry[]): string { |
| 60 | + const taken = new Set(entries.map((e) => e.name)); |
| 61 | + if (!taken.has(CURRENCY_CODE_FIELD_NAME)) return CURRENCY_CODE_FIELD_NAME; |
| 62 | + let n = 2; |
| 63 | + while (taken.has(`${CURRENCY_CODE_FIELD_NAME}_${n}`)) n += 1; |
| 64 | + return `${CURRENCY_CODE_FIELD_NAME}_${n}`; |
| 65 | +} |
| 66 | + |
| 67 | +/** |
| 68 | + * The config a mode switch writes. Each mode keeps only the keys it reads, so |
| 69 | + * the spec's own pairing rule holds by construction: `currencyField` exists |
| 70 | + * only under `record`, and `defaultCurrency` is carried only under `fixed`. A |
| 71 | + * key the author already wrote for the target mode is kept; none is invented. |
| 72 | + */ |
| 73 | +export function switchCurrencyMode(prev: CurrencyConfigDraft | undefined, mode: CurrencyMode): CurrencyConfigDraft { |
| 74 | + if (mode === 'fixed') { |
| 75 | + return { currencyMode: 'fixed', ...(prev?.defaultCurrency ? { defaultCurrency: prev.defaultCurrency } : {}) }; |
| 76 | + } |
| 77 | + if (mode === 'record') { |
| 78 | + return { currencyMode: 'record', ...(prev?.currencyField ? { currencyField: prev.currencyField } : {}) }; |
| 79 | + } |
| 80 | + return { currencyMode: 'dynamic' }; |
| 81 | +} |
| 82 | + |
| 83 | +/** |
| 84 | + * The explicit metadata "Create currency field" writes: a required `text` |
| 85 | + * field in the ISO 4217 domain. Required because a record-mode amount without |
| 86 | + * a code has no currency, and the server refuses a blank code at write. |
| 87 | + */ |
| 88 | +export function newCurrencyCodeFieldDef(label: string): Record<string, unknown> { |
| 89 | + return { type: 'text', label, valueDomain: CURRENCY_CODE_VALUE_DOMAIN, required: true }; |
| 90 | +} |
| 91 | + |
| 92 | +export function CurrencyConfigField({ |
| 93 | + config, |
| 94 | + candidates, |
| 95 | + onCommit, |
| 96 | + onCreateCodeField, |
| 97 | + disabled, |
| 98 | + tr, |
| 99 | +}: { |
| 100 | + config: CurrencyConfigDraft | undefined; |
| 101 | + /** {@link currencyCodeFieldCandidates} of the object this field belongs to. */ |
| 102 | + candidates: readonly string[]; |
| 103 | + /** Write the whole config. */ |
| 104 | + onCommit: (next: CurrencyConfigDraft) => void; |
| 105 | + /** Append a record-currency code field and point this amount at it, in one write. */ |
| 106 | + onCreateCodeField: () => void; |
| 107 | + disabled?: boolean; |
| 108 | + tr: (key: string) => string; |
| 109 | +}) { |
| 110 | + const mode: CurrencyMode = config?.currencyMode ?? 'dynamic'; |
| 111 | + const pointer = config?.currencyField; |
| 112 | + return ( |
| 113 | + <div className="space-y-2" data-testid="currency-config-field"> |
| 114 | + <InspectorSelectField |
| 115 | + label={tr('designer.field.currency.mode')} |
| 116 | + value={mode} |
| 117 | + options={[ |
| 118 | + { value: 'dynamic', label: tr('designer.field.currency.modeDynamic') }, |
| 119 | + { value: 'fixed', label: tr('designer.field.currency.modeFixed') }, |
| 120 | + { value: 'record', label: tr('designer.field.currency.modeRecord') }, |
| 121 | + ]} |
| 122 | + onCommit={(v) => onCommit(switchCurrencyMode(config, v as CurrencyMode))} |
| 123 | + disabled={disabled} |
| 124 | + /> |
| 125 | + {mode === 'fixed' && ( |
| 126 | + <InspectorTextField |
| 127 | + label={tr('designer.field.currency.defaultCurrency')} |
| 128 | + value={config?.defaultCurrency ?? ''} |
| 129 | + // A three-letter code, written upper-case as ISO 4217 spells it. |
| 130 | + onCommit={(v) => { |
| 131 | + const code = v.trim().toUpperCase(); |
| 132 | + onCommit(code ? { currencyMode: 'fixed', defaultCurrency: code } : { currencyMode: 'fixed' }); |
| 133 | + }} |
| 134 | + placeholder="USD" |
| 135 | + disabled={disabled} |
| 136 | + /> |
| 137 | + )} |
| 138 | + {mode === 'record' && ( |
| 139 | + <> |
| 140 | + <InspectorSelectField |
| 141 | + label={tr('designer.field.currency.currencyField')} |
| 142 | + value={pointer ?? ''} |
| 143 | + options={[ |
| 144 | + { value: '', label: tr('designer.field.currency.currencyFieldNone') }, |
| 145 | + ...candidates.map((name) => ({ value: name, label: name })), |
| 146 | + ]} |
| 147 | + // Unpicking DELETES the pointer: the spec refuses a `record` config |
| 148 | + // without one on save, which is the honest answer, rather than a |
| 149 | + // pointer at a field the author never chose. |
| 150 | + onCommit={(v) => onCommit(v ? { currencyMode: 'record', currencyField: v } : { currencyMode: 'record' })} |
| 151 | + required |
| 152 | + disabled={disabled} |
| 153 | + /> |
| 154 | + <Button |
| 155 | + type="button" |
| 156 | + variant="outline" |
| 157 | + size="sm" |
| 158 | + onClick={onCreateCodeField} |
| 159 | + disabled={disabled} |
| 160 | + data-testid="currency-config-create-code-field" |
| 161 | + > |
| 162 | + <Plus className="mr-1 h-3.5 w-3.5" /> |
| 163 | + {tr('designer.field.currency.createCodeField')} |
| 164 | + </Button> |
| 165 | + <p className="text-xs text-muted-foreground">{tr('designer.field.currency.recordHint')}</p> |
| 166 | + </> |
| 167 | + )} |
| 168 | + </div> |
| 169 | + ); |
| 170 | +} |
0 commit comments