fix: Document history event headings (#2433)

* fix: Document history events from last year but within 12 months shown as 'this year'
fix: Events older than a year have repeated headings

* lint
This commit is contained in:
Tom Moor
2021-08-12 18:24:13 -04:00
committed by GitHub
parent 4cbae1cf7d
commit 77db0c2e95
5 changed files with 75 additions and 55 deletions

View File

@ -22,7 +22,6 @@ import UiStore from "stores/UiStore";
import ErrorSuspended from "scenes/ErrorSuspended"; import ErrorSuspended from "scenes/ErrorSuspended";
import KeyboardShortcuts from "scenes/KeyboardShortcuts"; import KeyboardShortcuts from "scenes/KeyboardShortcuts";
import Button from "components/Button"; import Button from "components/Button";
import DocumentHistory from "components/DocumentHistory";
import Flex from "components/Flex"; import Flex from "components/Flex";
import Guide from "components/Guide"; import Guide from "components/Guide";
import { LoadingIndicatorBar } from "components/LoadingIndicator"; import { LoadingIndicatorBar } from "components/LoadingIndicator";
@ -38,6 +37,12 @@ import {
newDocumentUrl, newDocumentUrl,
} from "utils/routeHelpers"; } from "utils/routeHelpers";
const DocumentHistory = React.lazy(() =>
import(
/* webpackChunkName: "document-history" */ "components/DocumentHistory"
)
);
type Props = { type Props = {
documents: DocumentsStore, documents: DocumentsStore,
children?: ?React.Node, children?: ?React.Node,
@ -154,12 +159,14 @@ class Layout extends React.Component<Props> {
{this.props.children} {this.props.children}
</Content> </Content>
<Switch> <React.Suspense>
<Route <Switch>
path={`/doc/${slug}/history/:revisionId?`} <Route
component={DocumentHistory} path={`/doc/${slug}/history/:revisionId?`}
/> component={DocumentHistory}
</Switch> />
</Switch>
</React.Suspense>
</Container> </Container>
<Guide <Guide
isOpen={this.keyboardShortcutsOpen} isOpen={this.keyboardShortcutsOpen}

View File

@ -1,39 +1,9 @@
// @flow // @flow
import { format as formatDate, formatDistanceToNow } from "date-fns"; import { format as formatDate, formatDistanceToNow } from "date-fns";
import {
enUS,
de,
faIR,
fr,
es,
it,
ja,
ko,
ptBR,
pt,
zhCN,
zhTW,
ru,
} from "date-fns/locale";
import * as React from "react"; import * as React from "react";
import Tooltip from "components/Tooltip"; import Tooltip from "components/Tooltip";
import useUserLocale from "hooks/useUserLocale"; import useUserLocale from "hooks/useUserLocale";
import { dateLocale } from "utils/i18n";
const locales = {
en_US: enUS,
de_DE: de,
es_ES: es,
fa_IR: faIR,
fr_FR: fr,
it_IT: it,
ja_JP: ja,
ko_KR: ko,
pt_BR: ptBR,
pt_PT: pt,
zh_CN: zhCN,
zh_TW: zhTW,
ru_RU: ru,
};
let callbacks = []; let callbacks = [];
@ -59,7 +29,6 @@ type Props = {
shorten?: boolean, shorten?: boolean,
relative?: boolean, relative?: boolean,
format?: string, format?: string,
tooltip?: boolean,
}; };
function LocaleTime({ function LocaleTime({
@ -70,7 +39,6 @@ function LocaleTime({
format, format,
relative, relative,
tooltipDelay, tooltipDelay,
tooltip,
}: Props) { }: Props) {
const userLocale = useUserLocale(); const userLocale = useUserLocale();
const [_, setMinutesMounted] = React.useState(0); // eslint-disable-line no-unused-vars const [_, setMinutesMounted] = React.useState(0); // eslint-disable-line no-unused-vars
@ -88,7 +56,7 @@ function LocaleTime({
}; };
}, []); }, []);
const locale = userLocale ? locales[userLocale] : undefined; const locale = dateLocale(userLocale);
let relativeContent = formatDistanceToNow(Date.parse(dateTime), { let relativeContent = formatDistanceToNow(Date.parse(dateTime), {
addSuffix, addSuffix,
locale, locale,
@ -109,10 +77,6 @@ function LocaleTime({
const content = children || relative ? relativeContent : tooltipContent; const content = children || relative ? relativeContent : tooltipContent;
if (!tooltip) {
return content;
}
return ( return (
<Tooltip tooltip={tooltipContent} delay={tooltipDelay} placement="bottom"> <Tooltip tooltip={tooltipContent} delay={tooltipDelay} placement="bottom">
<time dateTime={dateTime}>{content}</time> <time dateTime={dateTime}>{content}</time>

View File

@ -2,10 +2,11 @@
import ArrowKeyNavigation from "boundless-arrow-key-navigation"; import ArrowKeyNavigation from "boundless-arrow-key-navigation";
import { isEqual } from "lodash"; import { isEqual } from "lodash";
import { observable, action } from "mobx"; import { observable, action } from "mobx";
import { observer } from "mobx-react"; import { observer, inject } from "mobx-react";
import * as React from "react"; import * as React from "react";
import { withTranslation, type TFunction } from "react-i18next"; import { withTranslation, type TFunction } from "react-i18next";
import { Waypoint } from "react-waypoint"; import { Waypoint } from "react-waypoint";
import AuthStore from "stores/AuthStore";
import { DEFAULT_PAGINATION_LIMIT } from "stores/BaseStore"; import { DEFAULT_PAGINATION_LIMIT } from "stores/BaseStore";
import DelayedMount from "components/DelayedMount"; import DelayedMount from "components/DelayedMount";
import PlaceholderList from "components/List/Placeholder"; import PlaceholderList from "components/List/Placeholder";
@ -17,6 +18,7 @@ type Props = {
heading?: React.Node, heading?: React.Node,
empty?: React.Node, empty?: React.Node,
items: any[], items: any[],
auth: AuthStore,
renderItem: (any, index: number) => React.Node, renderItem: (any, index: number) => React.Node,
renderHeading?: (name: React.Element<any> | string) => React.Node, renderHeading?: (name: React.Element<any> | string) => React.Node,
t: TFunction, t: TFunction,
@ -105,7 +107,7 @@ class PaginatedList extends React.Component<Props> {
}; };
render() { render() {
const { items, heading, empty, renderHeading } = this.props; const { items, heading, auth, empty, renderHeading } = this.props;
let previousHeading = ""; let previousHeading = "";
const showLoading = const showLoading =
@ -137,7 +139,11 @@ class PaginatedList extends React.Component<Props> {
// Get what a heading would look like for this item // Get what a heading would look like for this item
const currentDate = const currentDate =
item.updatedAt || item.createdAt || previousHeading; item.updatedAt || item.createdAt || previousHeading;
const currentHeading = dateToHeading(currentDate, this.props.t); const currentHeading = dateToHeading(
currentDate,
this.props.t,
auth.user?.language
);
// If the heading is different to any previous heading then we // If the heading is different to any previous heading then we
// should render it, otherwise the item can go under the previous // should render it, otherwise the item can go under the previous
@ -173,4 +179,4 @@ class PaginatedList extends React.Component<Props> {
export const Component = PaginatedList; export const Component = PaginatedList;
export default withTranslation()<PaginatedList>(PaginatedList); export default withTranslation()<PaginatedList>(inject("auth")(PaginatedList));

View File

@ -4,14 +4,20 @@ import {
isYesterday, isYesterday,
differenceInCalendarWeeks, differenceInCalendarWeeks,
differenceInCalendarMonths, differenceInCalendarMonths,
differenceInCalendarYears,
format as formatDate,
} from "date-fns"; } from "date-fns";
import * as React from "react";
import { type TFunction } from "react-i18next"; import { type TFunction } from "react-i18next";
import LocaleTime from "components/LocaleTime"; import { dateLocale } from "utils/i18n";
export function dateToHeading(dateTime: string, t: TFunction) { export function dateToHeading(
dateTime: string,
t: TFunction,
userLocale: ?string
) {
const date = Date.parse(dateTime); const date = Date.parse(dateTime);
const now = new Date(); const now = new Date();
const locale = dateLocale(userLocale);
if (isToday(date)) { if (isToday(date)) {
return t("Today"); return t("Today");
@ -26,7 +32,7 @@ export function dateToHeading(dateTime: string, t: TFunction) {
// async bundle loading of languages // async bundle loading of languages
const weekDiff = differenceInCalendarWeeks(now, date); const weekDiff = differenceInCalendarWeeks(now, date);
if (weekDiff === 0) { if (weekDiff === 0) {
return <LocaleTime dateTime={dateTime} tooltip={false} format="iiii" />; return formatDate(Date.parse(dateTime), "iiii", { locale });
} }
if (weekDiff === 1) { if (weekDiff === 1) {
@ -42,10 +48,11 @@ export function dateToHeading(dateTime: string, t: TFunction) {
return t("Last month"); return t("Last month");
} }
if (monthDiff <= 12) { const yearDiff = differenceInCalendarYears(now, date);
if (yearDiff === 0) {
return t("This year"); return t("This year");
} }
// If older than the current calendar year then just print the year e.g 2020 // If older than the current calendar year then just print the year e.g 2020
return <LocaleTime dateTime={dateTime} tooltip={false} format="y" />; return formatDate(Date.parse(dateTime), "y", { locale });
} }

36
app/utils/i18n.js Normal file
View File

@ -0,0 +1,36 @@
// @flow
import {
enUS,
de,
faIR,
fr,
es,
it,
ja,
ko,
ptBR,
pt,
zhCN,
zhTW,
ru,
} from "date-fns/locale";
const locales = {
en_US: enUS,
de_DE: de,
es_ES: es,
fa_IR: faIR,
fr_FR: fr,
it_IT: it,
ja_JP: ja,
ko_KR: ko,
pt_BR: ptBR,
pt_PT: pt,
zh_CN: zhCN,
zh_TW: zhTW,
ru_RU: ru,
};
export function dateLocale(userLocale: ?string) {
return userLocale ? locales[userLocale] : undefined;
}