Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 24 additions & 3 deletions src/components/AnnotationTimestamps.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not super happy with these variant names. I'm open to suggestions.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The intent is clear from the names, so they will work as a starting point. If you need to add more variants in future it will become clearer how to distinguish them.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ok!


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;
};

/**
Expand Down Expand Up @@ -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 (
<div>
{withEditedTimestamp && (
<span
className="text-color-text-light text-xs italic"
className={classnames('text-color-text-light italic', {
'text-xs': editedTimestampVariant === 'subtle',
'font-bold': editedTimestampVariant === 'prominent',
})}
data-testid="timestamp-edited"
data-variant={editedTimestampVariant}
title={updated.absolute}
>
({editedString}){' '}
Expand Down
17 changes: 17 additions & 0 deletions src/components/test/AnnotationTimestamps-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I decided to just test with a data-variant attribute, rather than testing the actual classes being added for every variant, to reduce coupling the test with implementation details.

});
});

it('does not render edited relative date if equivalent to created relative date', () => {
fakeFormatRelativeDate.returns('equivalent fuzzy strings');

Expand Down