From 441c5ac4b994bcbb281347794d502e40fc475ed1 Mon Sep 17 00:00:00 2001 From: Alejandro Celaya Date: Wed, 20 Aug 2025 09:46:30 +0200 Subject: [PATCH] Allow some customization in AnnotationTimestamps edited text --- src/components/AnnotationTimestamps.tsx | 27 ++++++++++++++++--- .../test/AnnotationTimestamps-test.js | 17 ++++++++++++ 2 files changed, 41 insertions(+), 3 deletions(-) diff --git a/src/components/AnnotationTimestamps.tsx b/src/components/AnnotationTimestamps.tsx index bc1eb7a..79ce3a5 100644 --- a/src/components/AnnotationTimestamps.tsx +++ b/src/components/AnnotationTimestamps.tsx @@ -4,14 +4,29 @@ import { formatRelativeDate, formatDateTime, } from '@hypothesis/frontend-shared'; +import classnames from 'classnames'; import { useEffect, useMemo, useState } from 'preact/hooks'; +/** + * `subtle`: Small text with inherited font weight. + * `prominent`: Bold text with inherited font size. + */ +export type EditedTimestampVariant = 'subtle' | 'prominent'; + export type AnnotationTimestampsProps = { annotationCreated: string; annotationUpdated: string; annotationURL?: string; - /** Display a relative last-updated timestamp */ - withEditedTimestamp?: boolean; + + /** + * Whether a relative last-updated timestamp should be displayed or not. + * - `false`: do not display edited timestamp + * - `true`: display using default variant, `subtle` + * - {variant_name}: display using specified variant + * + * Defaults to `false`. + */ + withEditedTimestamp?: boolean | EditedTimestampVariant; }; /** @@ -72,13 +87,19 @@ export default function AnnotationTimestamps({ updated && updated.relative !== created.relative ? `edited ${updated.relative}` : 'edited'; + const editedTimestampVariant: EditedTimestampVariant = + typeof withEditedTimestamp === 'string' ? withEditedTimestamp : 'subtle'; return (
{withEditedTimestamp && ( ({editedString}){' '} diff --git a/src/components/test/AnnotationTimestamps-test.js b/src/components/test/AnnotationTimestamps-test.js index 94a3ae3..5f3a9a3 100644 --- a/src/components/test/AnnotationTimestamps-test.js +++ b/src/components/test/AnnotationTimestamps-test.js @@ -70,6 +70,23 @@ describe('AnnotationTimestamps', () => { assert.include(editedTimestamp.text(), '(edited another fuzzy string)'); }); + [ + { withEditedTimestamp: true, expectedVariant: 'subtle' }, + { withEditedTimestamp: 'subtle', expectedVariant: 'subtle' }, + { withEditedTimestamp: 'prominent', expectedVariant: 'prominent' }, + ].forEach(({ withEditedTimestamp, expectedVariant }) => { + it('renders edited timestamp with expected variant', () => { + fakeFormatRelativeDate.onCall(1).returns('another fuzzy string'); + + const wrapper = createComponent({ withEditedTimestamp }); + + const editedTimestamp = wrapper + .find('[data-testid="timestamp-edited"]') + .getDOMNode(); + assert.equal(editedTimestamp.dataset.variant, expectedVariant); + }); + }); + it('does not render edited relative date if equivalent to created relative date', () => { fakeFormatRelativeDate.returns('equivalent fuzzy strings');