Skip to content

Conversation

mortigen
Copy link
Contributor

I noticed that Timeago component had several issues, so I decided to update it because we use it in several places among different projects.

Changes:

  • fixed handling datetime prop - it nows correctly handle Date object and string representing a date
  • fix datetime attribute for <time /> tag to use toLocaleString()
  • use title attribute for <time />
  • allow for custom renders, for example when translations are needed:
    • renderNow - takes no arguments
    • renderMinutes - takes minutes param as number
    • renderHours - takes hours param as number
    • renderDays - takes days param as number
    • renderDate - takes date param as string (localDateString)

@mortigen mortigen requested a review from vforge as a code owner August 20, 2020 07:31

if (diffInSeconds > TIMEAGO_NOW_THRESHOLD) {
const minutes = Math.round(diffInSeconds / MINUTE_SECONDS);
return withCustomRender(renderMinutes, minutes, `${minutes}m`);
Copy link
Collaborator

Choose a reason for hiding this comment

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

Should this string (the default ones) be translated maybe? Just a guess.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I think react-common doesn't handle translations. That's why I thought we need those custom renders to provide translations in specific apps. With this, we can create a wrapper for Timeago for example in Feeds which will handle translations there.

Copy link
Collaborator

Choose a reason for hiding this comment

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

Some components of design-system handles i18n, so I think it wouldn't be an issue to handle this in react-common as well. I see that we don't have any components in this repo that translates anything but maybe we should think about that.

}

// less than a minute ago
return typeof (renderNow) === 'function' ? renderNow() : 'now';
Copy link
Collaborator

Choose a reason for hiding this comment

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

Shouldn't this be translated?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Copy link
Collaborator

@vforge vforge left a comment

Choose a reason for hiding this comment

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

I like that idea. Good customization

display: this.props.datetime,
};
export default function Timeago({
datetime, renderNow, renderDate, renderDays, renderHours, renderMinutes,
Copy link
Collaborator

Choose a reason for hiding this comment

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

Can render* function return JSX/JS string? Ideally Id like to use renderNow={() => <strong>NOW!</string>}

Copy link
Contributor Author

Choose a reason for hiding this comment

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

You can return anything because it will just call your render* function and return its result. Check this fragment out: return typeof (renderNow) === 'function' ? renderNow() : 'now'; So sure, you can use it like you did it above :)

@mortigen mortigen merged commit f096827 into master Aug 25, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants