Skip to content

Commit

Permalink
Render "Data updated" as a relative date
Browse files Browse the repository at this point in the history
To us humans, "Data updated 3 years ago" is more interpretable than
"Data updated 2020-12-11". Time flies!
  • Loading branch information
victorlin committed Dec 12, 2023
1 parent c6381cb commit 8b2ca9f
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 5 deletions.
17 changes: 17 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,7 @@
"react-hot-loader": "^4.8.4",
"react-i18next": "^11.15.6",
"react-icons": "^3.9.0",
"react-moment": "^1.1.3",
"react-redux": "^7.2.6",
"react-select": "^5.2.2",
"react-tooltip": "^4.2.10",
Expand Down
12 changes: 8 additions & 4 deletions src/components/info/byline.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import React from "react";
import { connect } from "react-redux";
import Moment from 'react-moment';
import { withTranslation } from 'react-i18next';
import styled from 'styled-components';
import moment from "moment";
Expand All @@ -12,7 +13,8 @@ import { headerFont } from "../../globalStyles";
*/
@connect((state) => {
return {
metadata: state.metadata
metadata: state.metadata,
language: state.general.language
};
})
class Byline extends React.Component {
Expand All @@ -23,7 +25,7 @@ class Byline extends React.Component {
{renderAvatar(t, this.props.metadata)}
{renderBuildInfo(t, this.props.metadata)}
{renderMaintainers(t, this.props.metadata)}
{renderDataUpdated(t, this.props.metadata)}
{renderDataUpdated(t, this.props.metadata, this.props.language)}
{renderDataProvenance(t, this.props.metadata)}
</>
);
Expand Down Expand Up @@ -107,11 +109,13 @@ function renderMaintainers(t, metadata) {
* Returns a React component detailing the date the data was last updated.
* Renders a <span> containing "Data updated X", where X derives from `metadata.updated`
*/
function renderDataUpdated(t, metadata) {
function renderDataUpdated(t, metadata, language) {
if (moment(metadata.updated).isValid()) {
return (
<span>
{`${t("Data updated")} ${metadata.updated}. `}
{`${t("Data updated")} `}
<Moment locale={language} date={metadata.updated} fromNow />
{`. `}
</span>
);
}
Expand Down
2 changes: 1 addition & 1 deletion webpack.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,7 @@ const generateConfig = ({extensionPath, devMode=false, customOutputPath, analyze
"whatwg-fetch",
"style-loader",
"@hot-loader/react-dom",
"react(-(redux|select|helmet|i18next))?",
"react(-(redux|select|helmet|i18next|moment))?",
"moment",
"leaflet",
"redux",
Expand Down

0 comments on commit 8b2ca9f

Please sign in to comment.