@ossy/resources 3.9.0 → 3.10.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/package.json
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@ossy/resources",
|
|
3
3
|
"description": "Resource domain — aggregate and events for the Ossy resource model",
|
|
4
|
-
"version": "3.
|
|
4
|
+
"version": "3.10.0",
|
|
5
5
|
"private": false,
|
|
6
6
|
"type": "module",
|
|
7
7
|
"main": "./src/index.js",
|
|
@@ -49,5 +49,5 @@
|
|
|
49
49
|
"/src",
|
|
50
50
|
"README.md"
|
|
51
51
|
],
|
|
52
|
-
"gitHead": "
|
|
52
|
+
"gitHead": "198084eb98871876396ae625d043dd0f03aa0325"
|
|
53
53
|
}
|
package/src/SchemaDetailView.jsx
CHANGED
|
@@ -1,6 +1,10 @@
|
|
|
1
1
|
import React from 'react'
|
|
2
2
|
import { Text, View, MarkdownViewer, useLocale, hasTranslation } from '@ossy/design-system'
|
|
3
|
-
import {
|
|
3
|
+
import {
|
|
4
|
+
formatTemporalFieldValue,
|
|
5
|
+
isTemporalField,
|
|
6
|
+
shouldRenderFieldAsMarkdown,
|
|
7
|
+
} from './schema-field-display.js'
|
|
4
8
|
|
|
5
9
|
const labelStyle = { fontWeight: 600, opacity: 0.65 }
|
|
6
10
|
const valueStyle = { whiteSpace: 'pre-wrap' }
|
|
@@ -12,6 +16,13 @@ function renderPlainFieldValue (value) {
|
|
|
12
16
|
return String(value)
|
|
13
17
|
}
|
|
14
18
|
|
|
19
|
+
function renderFieldValue (field, value, locale) {
|
|
20
|
+
if (isTemporalField(field)) {
|
|
21
|
+
return formatTemporalFieldValue(value, field, locale) ?? '—'
|
|
22
|
+
}
|
|
23
|
+
return renderPlainFieldValue(value)
|
|
24
|
+
}
|
|
25
|
+
|
|
15
26
|
/**
|
|
16
27
|
* Generic read-only detail projection for schema-backed resources.
|
|
17
28
|
*
|
|
@@ -30,7 +41,8 @@ export function SchemaDetailView ({
|
|
|
30
41
|
title,
|
|
31
42
|
emptyMessage = 'No data',
|
|
32
43
|
}) {
|
|
33
|
-
const { messages } = useLocale()
|
|
44
|
+
const { messages, language } = useLocale()
|
|
45
|
+
const locale = language === 'sv' ? 'sv-SE' : 'en-GB'
|
|
34
46
|
const fields = (fieldsProp ?? []).filter((field) => !field.derived)
|
|
35
47
|
const displayTitle = title ?? data.name ?? 'Preview'
|
|
36
48
|
|
|
@@ -86,7 +98,7 @@ export function SchemaDetailView ({
|
|
|
86
98
|
? <MarkdownViewer>{value}</MarkdownViewer>
|
|
87
99
|
: (
|
|
88
100
|
<Text variant="small" style={valueStyle}>
|
|
89
|
-
{
|
|
101
|
+
{renderFieldValue(field, value, locale)}
|
|
90
102
|
</Text>
|
|
91
103
|
)}
|
|
92
104
|
</View>
|
|
@@ -1,3 +1,5 @@
|
|
|
1
|
+
const TEMPORAL_FIELD_TYPES = new Set(['date', 'month', 'timestamp'])
|
|
2
|
+
|
|
1
3
|
/**
|
|
2
4
|
* Whether a schema field value should render as Markdown in detail views.
|
|
3
5
|
*
|
|
@@ -10,3 +12,32 @@ export function shouldRenderFieldAsMarkdown (field, value) {
|
|
|
10
12
|
&& typeof value === 'string'
|
|
11
13
|
&& value.trim().length > 0
|
|
12
14
|
}
|
|
15
|
+
|
|
16
|
+
/**
|
|
17
|
+
* @param {{ type?: string } | null | undefined} field
|
|
18
|
+
*/
|
|
19
|
+
export function isTemporalField (field) {
|
|
20
|
+
return TEMPORAL_FIELD_TYPES.has(field?.type)
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
/**
|
|
24
|
+
* Localized label for date / month / timestamp schema values (Unix ms).
|
|
25
|
+
*
|
|
26
|
+
* @param {unknown} value
|
|
27
|
+
* @param {{ type?: string } | null | undefined} field
|
|
28
|
+
* @param {string} [locale]
|
|
29
|
+
* @returns {string | null}
|
|
30
|
+
*/
|
|
31
|
+
export function formatTemporalFieldValue (value, field, locale = 'en-GB') {
|
|
32
|
+
if (value == null || value === '') return null
|
|
33
|
+
const ms = typeof value === 'number' ? value : Number(value)
|
|
34
|
+
if (!Number.isFinite(ms)) return null
|
|
35
|
+
|
|
36
|
+
const options = field?.type === 'month'
|
|
37
|
+
? { year: 'numeric', month: 'long' }
|
|
38
|
+
: field?.type === 'timestamp'
|
|
39
|
+
? { dateStyle: 'medium', timeStyle: 'short' }
|
|
40
|
+
: { dateStyle: 'medium' }
|
|
41
|
+
|
|
42
|
+
return new Intl.DateTimeFormat(locale, options).format(new Date(ms))
|
|
43
|
+
}
|
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
import { describe, expect, it } from '@jest/globals'
|
|
2
|
+
import {
|
|
3
|
+
formatTemporalFieldValue,
|
|
4
|
+
isTemporalField,
|
|
5
|
+
} from './schema-field-display.js'
|
|
6
|
+
|
|
7
|
+
describe('isTemporalField', () => {
|
|
8
|
+
it('matches date, month, and timestamp', () => {
|
|
9
|
+
expect(isTemporalField({ type: 'date' })).toBe(true)
|
|
10
|
+
expect(isTemporalField({ type: 'month' })).toBe(true)
|
|
11
|
+
expect(isTemporalField({ type: 'timestamp' })).toBe(true)
|
|
12
|
+
expect(isTemporalField({ type: 'number' })).toBe(false)
|
|
13
|
+
expect(isTemporalField({})).toBe(false)
|
|
14
|
+
})
|
|
15
|
+
})
|
|
16
|
+
|
|
17
|
+
describe('formatTemporalFieldValue', () => {
|
|
18
|
+
const ms = Date.UTC(2020, 7, 8)
|
|
19
|
+
|
|
20
|
+
it('formats date fields as a calendar day', () => {
|
|
21
|
+
const label = formatTemporalFieldValue(ms, { type: 'date' }, 'en-GB')
|
|
22
|
+
expect(label).toContain('2020')
|
|
23
|
+
expect(label).not.toMatch(/1596844800000/)
|
|
24
|
+
})
|
|
25
|
+
|
|
26
|
+
it('formats month fields as month and year', () => {
|
|
27
|
+
const label = formatTemporalFieldValue(ms, { type: 'month' }, 'en-GB')
|
|
28
|
+
expect(label.toLowerCase()).toContain('august')
|
|
29
|
+
expect(label).toContain('2020')
|
|
30
|
+
})
|
|
31
|
+
|
|
32
|
+
it('returns em-dash fallback as null for empty values', () => {
|
|
33
|
+
expect(formatTemporalFieldValue(null, { type: 'date' })).toBe(null)
|
|
34
|
+
expect(formatTemporalFieldValue('', { type: 'date' })).toBe(null)
|
|
35
|
+
expect(formatTemporalFieldValue('not-a-date', { type: 'date' })).toBe(null)
|
|
36
|
+
})
|
|
37
|
+
})
|