mirror of
https://github.com/Zakkaus/doona.git
synced 2026-12-05 16:02:03 +00:00
fix(i18n): show enum values a newer backend adds instead of a blank
Labels for wire enums were looked up as t(labels[value]). A value the contract gained after this build has no entry, so translate received undefined: the label came out blank (a log level, a connection state), "undefined" inside a sentence, or, for an event kind, eventSummary fell off its switch and the events page threw on the missing summary. enumLabel(labels, value, t) returns the value as sent when there is no label, and replaces the lookups on the events, logs, connections, flows, rules, nodes, config, overview, policies and search views, plus the hand-written `labels[v] ? t(labels[v]) : v` checks. eventSummary gets a default branch that names the event's resource.
This commit is contained in:
1 parent
933f046d94
commit
fd01bb6502
19 files changed
+87
-28
No files matched your search
@@ -167,6 +167,12 @@ export function eventSummary(event: ApiEvent, t?: (key: Key) => string): Message
|
||||
n: droppedCount(event.data.dropped_records)
|
||||
}
|
||||
};
|
||||
// An event kind from a newer backend: the resource it names, when it names one.
|
||||
default: {
|
||||
const data = (event as {data?: {resource_id?: unknown; href?: unknown}}).data;
|
||||
const resource = [data?.resource_id, data?.href].find(value => typeof value === 'string');
|
||||
return {key: 'event.resource', params: {resource: resource ?? '—'}};
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -1,4 +1,5 @@
|
||||
import type {ApiEvent, ConnectionList, Group, Node, Runtime, RuntimeMemory, RuntimeOutbounds} from '../../api/model';
|
||||
import {enumLabel} from '../../i18n/enum';
|
||||
import {isBuiltinOutbound} from '../../dae/vocab';
|
||||
import {
|
||||
eventKindLabels,
|
||||
@@ -67,7 +68,7 @@ export function noticeRows(events: ApiEvent[], t: LabelFn) {
|
||||
id: event.id,
|
||||
tone: event.event === 'flow.gap' ? ('warn' as const) : ('info' as const),
|
||||
kindText: t(event.event === 'flow.gap' ? 'ui.warning' : 'ui.notice'),
|
||||
summaryText: t('ui.valuePair', {label: t(eventKindLabels[event.event]), value: t(summary.key, params)}),
|
||||
summaryText: t('ui.valuePair', {label: enumLabel(eventKindLabels, event.event, t), value: t(summary.key, params)}),
|
||||
key,
|
||||
count: 1
|
||||
});
|
||||
@@ -126,7 +127,7 @@ export function activityView(runtime: Runtime | undefined, memory: RuntimeMemory
|
||||
return {
|
||||
status: {
|
||||
tone: lifecycleTone(runtime?.lifecycle.state) as 'ok' | 'err' | 'warn',
|
||||
text: runtime ? t(lifecycleStates[runtime.lifecycle.state]) : t(runtimeAvailable === false ? 'act.modeUnavailable' : 'ui.loading')
|
||||
text: runtime ? enumLabel(lifecycleStates, runtime.lifecycle.state, t) : t(runtimeAvailable === false ? 'act.modeUnavailable' : 'ui.loading')
|
||||
},
|
||||
download: formatRate(runtime?.traffic.rates?.download_bytes_per_second ?? null, locale),
|
||||
upload: formatRate(runtime?.traffic.rates?.upload_bytes_per_second ?? null, locale),
|
||||
|
||||
@@ -1,4 +1,5 @@
|
||||
import type {ConfigDiagnostic, ConfigSource} from '../../api/model';
|
||||
import {enumLabel} from '../../i18n/enum';
|
||||
import {localTime, formatBytes} from '../../i18n/format';
|
||||
import {formatList, formatNumber, type Lang, type Translator} from '../../i18n';
|
||||
import {backendMessage} from '../../i18n/backend';
|
||||
@@ -178,7 +179,7 @@ export function sectionSummaries(sources: ConfigSource[], lang: Lang, t: Transla
|
||||
}
|
||||
|
||||
export function sourceView(source: ConfigSource, locale: string, t: Translator): SourceView {
|
||||
const kind = t(sourceKinds[source.kind]);
|
||||
const kind = enumLabel(sourceKinds, source.kind, t);
|
||||
return {
|
||||
id: source.id,
|
||||
label: redacted(source) ? `${kind} ${source.id.slice(0, 8)}` : source.path,
|
||||
@@ -217,7 +218,7 @@ export function diagnosticRows(diagnostics: ConfigDiagnostic[], sources: ConfigS
|
||||
id: String(index),
|
||||
level: item.level,
|
||||
tone: tones[item.level],
|
||||
levelText: t(levels[item.level]),
|
||||
levelText: enumLabel(levels, item.level, t),
|
||||
sourceId: item.source_id,
|
||||
line: item.line,
|
||||
where: item.line === null ? path : `${path}:${item.line}`,
|
||||
|
||||
@@ -1,4 +1,5 @@
|
||||
import type {BulkCloseQuery, Connection, ConnectionList} from '../../api/model';
|
||||
import {enumLabel} from '../../i18n/enum';
|
||||
import {addU64, parseU64} from '../../api/u64';
|
||||
import {
|
||||
chainLabel,
|
||||
@@ -101,7 +102,7 @@ export function tableRows(rows: Connection[], view: ConnectionView, locale: stri
|
||||
case 'src':
|
||||
return row.src;
|
||||
case 'state':
|
||||
return t(connectionStates[row.state]);
|
||||
return enumLabel(connectionStates, row.state, t);
|
||||
case 'down':
|
||||
return parseU64(row.download_bytes);
|
||||
case 'age':
|
||||
@@ -172,7 +173,7 @@ export function connectionTableView(
|
||||
path: chainPath(c, t, names),
|
||||
rule: {expression: c.rule_expression, href: ruleHref(c.rule_id, rulesListed)},
|
||||
recomputed: c.rule_source === 'recomputed' ? t('conn.recomputed') : null,
|
||||
state: t(connectionStates[c.state]),
|
||||
state: enumLabel(connectionStates, c.state, t),
|
||||
download: formatBytes(c.download_bytes, locale),
|
||||
startedAt: c.started_at
|
||||
};
|
||||
@@ -244,7 +245,7 @@ export function connectionDetail(
|
||||
id: current.id,
|
||||
title: current.domain || current.dst || current.id,
|
||||
tone: current.state === 'blocked' || current.state === 'failed' ? ('err' as const) : current.state === 'active' ? ('ok' as const) : ('info' as const),
|
||||
status: t('ui.aside', {text: t(connectionStates[current.state]), note: current.network.toUpperCase()}),
|
||||
status: t('ui.aside', {text: enumLabel(connectionStates, current.state, t), note: current.network.toUpperCase()}),
|
||||
chain: chainLabel(current, t, names),
|
||||
outbound: outboundLabel(current.outbound, t),
|
||||
rule: {expression: current.rule_expression, href: ruleHref(current.rule_id, rulesListed)},
|
||||
|
||||
@@ -1,4 +1,5 @@
|
||||
import type {Capabilities, DnsCacheList, DnsLogList, DnsLogRecord, DnsQueryResponse} from '../../api/model';
|
||||
import {enumLabel} from '../../i18n/enum';
|
||||
import {localTime, formatLatency} from '../../i18n/format';
|
||||
import {formatNumber, type Translator as LabelFn} from '../../i18n';
|
||||
import {csvLine} from '../../ui/ui';
|
||||
@@ -12,7 +13,7 @@ export function dnsAnswerView(result: Result, t: LabelFn) {
|
||||
fields: [
|
||||
[t('ui.state'), result.status],
|
||||
[t('ui.upstream'), result.upstream ?? '—'],
|
||||
[t('dns.routeSource'), routeSources[result.route.source] ? t(routeSources[result.route.source]) : result.route.source],
|
||||
[t('dns.routeSource'), enumLabel(routeSources, result.route.source, t)],
|
||||
[t('dns.routeRule'), result.route.rule ?? '—'],
|
||||
[t('ui.elapsed'), formatLatency(result.elapsed_ms, t)]
|
||||
] as Array<[string, string]>,
|
||||
|
||||
@@ -44,3 +44,10 @@ it('says on the ready row after lost history that events are missing', () => {
|
||||
const view = eventsView([lost, resumed], 'all', true, true, 200, 'en-US', t, ['stream.ready'], false, event => event === lost);
|
||||
expect(view.rows.map(row => row.summary)).toEqual([t('event.lostHistory'), 'instance']);
|
||||
});
|
||||
|
||||
it('shows an event kind from a newer backend as sent, with the resource it names', () => {
|
||||
const event = {id: 'x', event: 'policy.updated', data: {observed_at: '2026-01-01T00:00:00Z', resource_id: 'proxy'}} as unknown as ApiEvent;
|
||||
const view = eventsView([event], 'all', true, true, 200, 'en-US', t, ['policy.updated' as ApiEvent['event']]);
|
||||
expect(view.rows.map(row => [row.kindText, row.summary])).toEqual([['policy.updated', 'proxy']]);
|
||||
expect(view.kinds.at(-1)).toEqual({id: 'policy.updated', label: 'policy.updated'});
|
||||
});
|
||||
@@ -1,4 +1,5 @@
|
||||
import type {ApiEvent, EventKind} from '../../api/model';
|
||||
import {enumLabel} from '../../i18n/enum';
|
||||
import {eventKindLabels, eventSummary} from '../../api/selectors';
|
||||
import {localTime} from '../../i18n/format';
|
||||
import {formatNumber, type Translator as LabelFn} from '../../i18n';
|
||||
@@ -15,7 +16,7 @@ function eventRow(event: ApiEvent, locale: string, t: LabelFn, lost: boolean): E
|
||||
timestamp: localTime(event.data.observed_at, locale),
|
||||
iso: event.data.observed_at,
|
||||
kind: event.event,
|
||||
kindText: t(eventKindLabels[event.event]),
|
||||
kindText: enumLabel(eventKindLabels, event.event, t),
|
||||
summary: t(summary.key, summary.params)
|
||||
};
|
||||
rows.set(event, {locale, row});
|
||||
@@ -44,7 +45,7 @@ export function eventsView(
|
||||
kinds: [
|
||||
{id: 'without-runtime', label: t('event.withoutRuntime')},
|
||||
{id: 'all', label: t('event.allKinds')},
|
||||
...advertised.map(id => ({id, label: t(eventKindLabels[id])}))
|
||||
...advertised.map(id => ({id, label: enumLabel(eventKindLabels, id, t)}))
|
||||
],
|
||||
// Before capabilities answer nothing has failed yet; a warning only fits a stream that was expected. A failed
|
||||
// stream waits for a retry, so it is not reconnecting.
|
||||
|
||||
@@ -60,3 +60,12 @@ it('marks the levels the engine does not record and states the level it records'
|
||||
expect(unknown.levels.map(level => level.label)).toEqual([t('log.level.debug'), t('log.level.info')]);
|
||||
expect(unknown.recordedText).toBeNull();
|
||||
});
|
||||
|
||||
it('shows a log level from a newer backend as sent', () => {
|
||||
const level = 'fatal' as 'error';
|
||||
const record = {id: 'r', ts: '2026-01-01T00:00:00Z', level, target: 'dns', message: 'm', fields: null};
|
||||
const view = logView([record], [level, 'error'], true, undefined, 'en-US', t, false, new Set(), level);
|
||||
expect(view.rows[0].levelText).toBe('fatal');
|
||||
expect(view.levels[0].label).toBe('fatal');
|
||||
expect(view.recordedText).toBe(t('log.recorded', {level: 'fatal'}));
|
||||
});
|
||||
@@ -1,4 +1,5 @@
|
||||
import {logLevelLabels} from '../../api/selectors';
|
||||
import {enumLabel} from '../../i18n/enum';
|
||||
import type {LogLevel, LogRecord} from '../../api/model';
|
||||
import {localTime} from '../../i18n/format';
|
||||
import type {Translator as LabelFn} from '../../i18n';
|
||||
@@ -25,7 +26,7 @@ function logRow(record: LogRecord & {id: string}, locale: string, t: LabelFn): L
|
||||
id: record.id,
|
||||
timestamp: localTime(record.ts, locale),
|
||||
iso: record.ts,
|
||||
levelText: t(logLevelLabels[record.level]),
|
||||
levelText: enumLabel(logLevelLabels, record.level, t),
|
||||
tone: tones[record.level],
|
||||
target: record.target,
|
||||
gap: false,
|
||||
@@ -60,8 +61,11 @@ export function logView(
|
||||
if (!gaps.has(record)) return [row];
|
||||
return [{...row, id: `gap ${record.id}`, timestamp: '', iso: '', levelText: '', target: '', gap: true, message: t('log.gap')}, row];
|
||||
}),
|
||||
levels: levels.map(id => ({id, label: below(id) ? t('log.levelNotRecorded', {level: t(logLevelLabels[id])}) : t(logLevelLabels[id])})),
|
||||
recordedText: recorded ? t('log.recorded', {level: t(logLevelLabels[recorded])}) : null,
|
||||
levels: levels.map(id => ({
|
||||
id,
|
||||
label: below(id) ? t('log.levelNotRecorded', {level: enumLabel(logLevelLabels, id, t)}) : enumLabel(logLevelLabels, id, t)
|
||||
})),
|
||||
recordedText: recorded ? t('log.recorded', {level: enumLabel(logLevelLabels, recorded, t)}) : null,
|
||||
// A failed stream is not retried until asked, so it is not "connecting".
|
||||
status: failed
|
||||
? {tone: 'err' as const, text: t('log.disconnected')}
|
||||
|
||||
@@ -1,4 +1,5 @@
|
||||
import type {Capabilities, Node, Provider, ProviderCreate} from '../../api/model';
|
||||
import {enumLabel} from '../../i18n/enum';
|
||||
import {compareLatency, healthMillis, preferredHealth, pseudoOwner, pseudoOwnerId, type PseudoOwner} from '../../api/selectors';
|
||||
import type {TableSort} from '../../ui/ui';
|
||||
import {urlHost, type SubscriptionEntry} from './subscriptions';
|
||||
@@ -172,7 +173,7 @@ export function providerRowView(item: ProviderRow, seconds: number | null | unde
|
||||
id: item.id,
|
||||
name,
|
||||
url: item.url_redacted ?? undefined,
|
||||
kind: t(kinds[item.kind]),
|
||||
kind: enumLabel(kinds, item.kind, t),
|
||||
count: formatNumber(item.node_count, locale),
|
||||
usage:
|
||||
used === null
|
||||
@@ -187,7 +188,7 @@ export function providerRowView(item: ProviderRow, seconds: number | null | unde
|
||||
hasInterval: interval !== undefined,
|
||||
intervalLabel: t('nodes.intervalOf', {name}),
|
||||
intervals: interval === undefined ? [] : intervalItems(interval, locale, t),
|
||||
status: pseudo ? null : t(statuses[item.status]),
|
||||
status: pseudo ? null : enumLabel(statuses, item.status, t),
|
||||
tone: tones[item.status],
|
||||
error: item.last_error ? backendMessage(item.last_error.code, item.last_error.message, t) : undefined,
|
||||
refreshLabel: t('nodes.refresh', {name}),
|
||||
|
||||
@@ -1,4 +1,5 @@
|
||||
import type {Capabilities, Datapath, Runtime, RuntimeMemory, Version} from '../../api/model';
|
||||
import {enumLabel} from '../../i18n/enum';
|
||||
import type {Key} from '../../i18n';
|
||||
import {formatDuration, localTime, formatBytes} from '../../i18n/format';
|
||||
import {lifecycleStates, lifecycleTone, shortId} from '../../api/selectors';
|
||||
@@ -114,7 +115,7 @@ export function overviewView(
|
||||
return {
|
||||
status: {
|
||||
tone: lifecycleTone(state) as 'ok' | 'err' | 'warn',
|
||||
text: state ? t(lifecycleStates[state]) : t(loading.capabilities || loading.runtime ? 'ov.loading' : 'ov.unknown')
|
||||
text: state ? enumLabel(lifecycleStates, state, t) : t(loading.capabilities || loading.runtime ? 'ov.loading' : 'ov.unknown')
|
||||
},
|
||||
strip: [
|
||||
[t('ov.config'), shortId(revision), revision],
|
||||
|
||||
@@ -1,4 +1,5 @@
|
||||
import {formatLatency} from '../../i18n/format';
|
||||
import {enumLabel} from '../../i18n/enum';
|
||||
import type {Group, HealthObservation, ProbeResult} from '../../api/model';
|
||||
import type {Key} from '../../i18n';
|
||||
import {compareLatency, healthMillis, type MessageRef} from '../../api/selectors';
|
||||
@@ -86,7 +87,10 @@ export function memberViews(members: Array<Group['members'][number] & {health?:
|
||||
// Only an observation other than the usual TCP on the data path says how it was made.
|
||||
description:
|
||||
member.health && (member.health.transport !== 'tcp' || member.health.purpose !== 'data')
|
||||
? t('policy.observedVia', {transport: t(member.health.transport === 'udp' ? 'ui.udp' : 'ui.tcp'), purpose: t(purposes[member.health.purpose])})
|
||||
? t('policy.observedVia', {
|
||||
transport: t(member.health.transport === 'udp' ? 'ui.udp' : 'ui.tcp'),
|
||||
purpose: enumLabel(purposes, member.health.purpose, t)
|
||||
})
|
||||
: ' ',
|
||||
region: regionOf(member.name) ?? '?'
|
||||
}));
|
||||
|
||||
@@ -1,4 +1,5 @@
|
||||
import type {FlowDetail, FlowList, FlowStep, FlowSummary} from '../../../api/model';
|
||||
import {enumLabel} from '../../../i18n/enum';
|
||||
import type {Key} from '../../../i18n';
|
||||
import {formatList, formatNumber, LOCALE, type Lang, type Translator} from '../../../i18n';
|
||||
import {chainPath, connectionStates, nodeLabel, outboundLabel, sourceIp, type MessageRef, type OutboundNames} from '../../../api/selectors';
|
||||
@@ -183,7 +184,7 @@ export function coverageView(data: Pick<FlowList, 'coverage' | 'dropped_records'
|
||||
summary: partial.length ? t('flow.coverageSummary', {n: partial.length}) : null,
|
||||
detail: formatList(
|
||||
lang,
|
||||
partial.map(([scope, value]) => t('ui.valuePair', {label: scopes[scope] ? t(scopes[scope]) : scope, value: t(visibility[value])}))
|
||||
partial.map(([scope, value]) => t('ui.valuePair', {label: enumLabel(scopes, scope, t), value: enumLabel(visibility, value, t)}))
|
||||
),
|
||||
dropped
|
||||
};
|
||||
@@ -243,7 +244,7 @@ export function flowRecordsView(flows: FlowSummary[], list: FlowList | undefined
|
||||
ruleId: flow.rule_id,
|
||||
recomputed: flow.rule_source === 'recomputed',
|
||||
network: flow.network.toUpperCase(),
|
||||
state: t(connectionStates[flow.state]),
|
||||
state: enumLabel(connectionStates, flow.state, t),
|
||||
startedAt: flow.started_at
|
||||
})),
|
||||
coverage: list ? coverageView(list, t, lang) : null,
|
||||
@@ -258,11 +259,11 @@ export function flowDetailView(detail: FlowDetail | undefined, canAdd: boolean,
|
||||
const seed = detail.input.domain ? {kind: 'domainSuffix' as const, value: detail.input.domain} : ip ? {kind: 'dip' as const, value: ip} : null;
|
||||
return {
|
||||
title: detail.input.domain || detail.input.dst || detail.id,
|
||||
status: t(traceStates[detail.trace.status]),
|
||||
status: enumLabel(traceStates, detail.trace.status, t),
|
||||
tone: detail.trace.status === 'complete' ? undefined : 'warn',
|
||||
revision: t('flow.revision', {n: detail.revision}),
|
||||
fields: [
|
||||
[t('ui.state'), t(connectionStates[detail.state])],
|
||||
[t('ui.state'), enumLabel(connectionStates, detail.state, t)],
|
||||
[t('ui.outbound'), outboundLabel(detail.outbound, t)],
|
||||
...(detail.trace.missing.length
|
||||
? [
|
||||
@@ -270,7 +271,7 @@ export function flowDetailView(detail: FlowDetail | undefined, canAdd: boolean,
|
||||
t('flow.missing'),
|
||||
formatList(
|
||||
lang,
|
||||
detail.trace.missing.map(gap => (traceGaps[gap] ? t(traceGaps[gap]) : gap))
|
||||
detail.trace.missing.map(gap => enumLabel(traceGaps, gap, t))
|
||||
)
|
||||
] as [string, string]
|
||||
]
|
||||
@@ -284,7 +285,7 @@ export function flowDetailView(detail: FlowDetail | undefined, canAdd: boolean,
|
||||
const fields = flowStepFields(step);
|
||||
return {
|
||||
id: step.seq,
|
||||
stage: stages[step.stage] ? t(stages[step.stage]) : step.stage,
|
||||
stage: enumLabel(stages, step.stage, t),
|
||||
observed: localTime(step.observed_at, locale),
|
||||
elapsed: step.elapsed_us == null ? '—' : t('ui.microseconds', {n: step.elapsed_us}),
|
||||
fields:
|
||||
|
||||
@@ -1,4 +1,5 @@
|
||||
import type {Capabilities, ConfigSource, FlowList, GroupSummary, Node, RoutingEvaluation, RoutingRule, RoutingTraceResponse, RuleSource} from '../../api/model';
|
||||
import {enumLabel} from '../../i18n/enum';
|
||||
import {isBuiltinOutbound} from '../../dae/vocab';
|
||||
import {formatList, formatNumber, LOCALE, type Lang, type Translator} from '../../i18n';
|
||||
import type {Key} from '../../i18n';
|
||||
@@ -146,7 +147,7 @@ export function distributionView(list: FlowList | undefined, source: string, t:
|
||||
ruleId: row.id ?? '—',
|
||||
expression: row.expression ?? t('rule.unknownRule'),
|
||||
expressionClass: row.expression ? 'rp-code' : undefined,
|
||||
source: t(sources[row.source]),
|
||||
source: enumLabel(sources, row.source, t),
|
||||
hits: formatNumber(row.count, locale),
|
||||
share: t('ui.percent', {n: formatNumber(row.share * 100, locale, 1)})
|
||||
})),
|
||||
@@ -258,7 +259,7 @@ export function evaluationView(
|
||||
rows: evaluation.rules.map(rule => ({
|
||||
id: rule.rule_id,
|
||||
expression: rule.expression ?? rule.rule_id,
|
||||
outcome: outcomes[rule.result] ? t(outcomes[rule.result]) : rule.result,
|
||||
outcome: enumLabel(outcomes, rule.result, t),
|
||||
tone: rule.result === 'matched' ? 'ok' : rule.result === 'indeterminate' ? 'warn' : rule.result === 'skipped' ? 'muted' : 'neutral',
|
||||
missing: formatList(lang, rule.missing_inputs) || '—'
|
||||
}))
|
||||
|
||||
@@ -1,4 +1,5 @@
|
||||
import {useState} from 'react';
|
||||
import {enumLabel} from '../../i18n/enum';
|
||||
import {useCapabilities, useRuntimeSettings} from '../../store';
|
||||
import type {RuntimeSettingField, RuntimeSettings, RuntimeSettingsPatch} from '../../api/model';
|
||||
import {useT, useLang, LOCALE} from '../../i18n';
|
||||
@@ -90,7 +91,7 @@ export function useRuntimeSettingsForm() {
|
||||
setLevel: (value: string) => {
|
||||
if (!settings.busy) setDraft({...edits, level: value});
|
||||
},
|
||||
levels: (capabilities?.logs.levels ?? (['trace', 'debug', 'info', 'warn', 'error'] as const)).map(id => ({id, label: t(logLevelLabels[id])})),
|
||||
levels: (capabilities?.logs.levels ?? (['trace', 'debug', 'info', 'warn', 'error'] as const)).map(id => ({id, label: enumLabel(logLevelLabels, id, t)})),
|
||||
hasLevel: fields.has('log.level'),
|
||||
hasBaseline: !!baseline,
|
||||
source: baseline ? t(baseline.source === 'runtime' ? 'settings.sourceRuntime' : 'settings.sourceConfig') : null,
|
||||
|
||||
@@ -1,4 +1,5 @@
|
||||
import {groupNameProblem, type GroupEntry} from '../../dae/groups';
|
||||
import {enumLabel} from '../../i18n/enum';
|
||||
import {isFragment} from '../../dae/text';
|
||||
import type {Translator} from '../../i18n';
|
||||
import type {Key} from '../../i18n';
|
||||
@@ -25,7 +26,7 @@ export function policyLabel(value: string | null, t: Translator): string {
|
||||
// spelling, set only when the label differs from it.
|
||||
export function groupPolicyText(policy: Pick<Group['policy'], 'kind' | 'native'>, t: Translator): {label: string; id?: string} {
|
||||
const offered = newGroupPolicies.some(item => item.id === (policy.native ?? IMPLIED));
|
||||
const label = policy.native && offered ? policyLabel(policy.native, t) : t(policyKindLabels[policy.kind]);
|
||||
const label = policy.native && offered ? policyLabel(policy.native, t) : enumLabel(policyKindLabels, policy.kind, t);
|
||||
const id = policy.native || policy.kind;
|
||||
return label === id ? {label} : {label, id};
|
||||
}
|
||||
|
||||
@@ -0,0 +1,12 @@
|
||||
import {expect, it} from 'vitest';
|
||||
import {enumLabel} from './enum';
|
||||
import {translate, type Key, type Translator} from './index';
|
||||
const t: Translator = (key, params) => translate('en', key, params);
|
||||
|
||||
it('names a known value and shows an unknown one as sent', () => {
|
||||
const labels: Record<string, Key> = {ok: 'ui.unknown'};
|
||||
expect(enumLabel(labels, 'ok', t)).toBe(t('ui.unknown'));
|
||||
expect(enumLabel(labels, 'added_later', t)).toBe('added_later');
|
||||
// Inherited names are values too, not labels.
|
||||
expect(enumLabel(labels, 'constructor', t)).toBe('constructor');
|
||||
});
|
||||
@@ -0,0 +1,5 @@
|
||||
import type {Key, Translator} from './index';
|
||||
|
||||
// A wire enum value in words. A value from a newer backend has no label yet, so it reads as sent instead of blank.
|
||||
export const enumLabel = (labels: Readonly<Record<string, Key>>, value: string, t: Translator): string =>
|
||||
Object.hasOwn(labels, value) ? t(labels[value]) : value;
|
||||
@@ -1,4 +1,5 @@
|
||||
import type {Capabilities, ConnectionList, Node, GroupSummary, ProviderList, EffectiveConfig, RuleList} from '../../api/model';
|
||||
import {enumLabel} from '../../i18n/enum';
|
||||
import {formatList, formatNumber, LOCALE, type Key, type Lang, type Translator} from '../../i18n';
|
||||
import {chainLabel, connectionRows, nodeOwner} from '../../api/selectors';
|
||||
import {features, navAvailable} from '../registry';
|
||||
@@ -82,7 +83,7 @@ export function providerEntries(providers: ProviderList | undefined, t: Translat
|
||||
}
|
||||
export function sourceEntries(config: EffectiveConfig | undefined, t: Translator): SearchEntry[] {
|
||||
return (config?.sources ?? []).map(source =>
|
||||
entry([source.path], `source:${source.id}`, source.path, t(sourceKinds[source.kind]), 'config', within('', {tab: 'source', source: source.id}))
|
||||
entry([source.path], `source:${source.id}`, source.path, enumLabel(sourceKinds, source.kind, t), 'config', within('', {tab: 'source', source: source.id}))
|
||||
);
|
||||
}
|
||||
export function ruleEntries(rules: RuleList | undefined, lang: Lang): SearchEntry[] {
|
||||
|
||||
Reference in new issue
Block a user