Skip to content

Commit bb157c8

Browse files
authored
Merge pull request #84 from Bilb/fix-memoize-full-date
fix: memoize formatFullDate calls
2 parents f27014d + 1e2eff3 commit bb157c8

File tree

6 files changed

+27
-13
lines changed

6 files changed

+27
-13
lines changed

ts/components/conversation/Timestamp.tsx

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import useUpdate from 'react-use/lib/useUpdate';
33
import styled from 'styled-components';
44
import { CONVERSATION } from '../../session/constants';
55
import { getConversationItemString } from '../../util/i18n/formatting/conversationItemTimestamp';
6-
import { formatFullDate } from '../../util/i18n/formatting/generics';
6+
import { useFormatFullDate } from '../../hooks/useFormatFullDate';
77

88
type Props = {
99
timestamp?: number;
@@ -37,6 +37,7 @@ export const Timestamp = (props: Props) => {
3737
useInterval(update, UPDATE_FREQUENCY);
3838

3939
const { timestamp, isConversationSearchResult } = props;
40+
const formattedFullDate = useFormatFullDate(timestamp);
4041

4142
if (timestamp === null || timestamp === undefined) {
4243
return null;
@@ -48,7 +49,7 @@ export const Timestamp = (props: Props) => {
4849
if (timestamp !== CONVERSATION.LAST_JOINED_FALLBACK_TIMESTAMP) {
4950
dateString = getConversationItemString(new Date(timestamp));
5051

51-
title = formatFullDate(new Date(timestamp));
52+
title = formattedFullDate;
5253
}
5354

5455
if (isConversationSearchResult) {

ts/components/conversation/header/ConversationHeader.tsx

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ import { SessionButtonColor, SessionButton, SessionButtonType } from '../../basi
2828
import { useAreGroupsCreatedAsNewGroupsYet } from '../../../state/selectors/releasedFeatures';
2929
import { ConvoHub } from '../../../session/conversations';
3030
import { ConversationTypeEnum } from '../../../models/types';
31+
import { Constants } from '../../../session';
3132

3233
export const ConversationHeaderWithDetails = () => {
3334
const isSelectionMode = useIsMessageSelectionMode();
@@ -145,7 +146,9 @@ function RecreateGroupButton() {
145146
ConversationTypeEnum.PRIVATE
146147
);
147148
if (!memberConvo.get('active_at')) {
148-
memberConvo.set({ active_at: 1 });
149+
memberConvo.set({
150+
active_at: Constants.CONVERSATION.LAST_JOINED_FALLBACK_TIMESTAMP,
151+
});
149152
await memberConvo.commit();
150153
}
151154
/* eslint-enable no-await-in-loop */

ts/components/conversation/message/message-content/MessageContent.tsx

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ import { MessageHighlighter } from './MessageHighlighter';
2626
import { MessageLinkPreview } from './MessageLinkPreview';
2727
import { MessageQuote } from './MessageQuote';
2828
import { MessageText } from './MessageText';
29-
import { formatFullDate } from '../../../../util/i18n/formatting/generics';
29+
import { useFormatFullDate } from '../../../../hooks/useFormatFullDate';
3030

3131
export type MessageContentSelectorProps = Pick<
3232
MessageRenderingProps,
@@ -125,16 +125,16 @@ export const MessageContent = (props: Props) => {
125125
shouldHighlightMessage,
126126
]);
127127

128+
const toolTipTitle = useFormatFullDate(contentProps?.serverTimestamp || contentProps?.timestamp);
129+
128130
if (!contentProps) {
129131
return null;
130132
}
131133

132-
const { direction, text, timestamp, serverTimestamp, previews, quote } = contentProps;
134+
const { direction, text, previews, quote } = contentProps;
133135

134136
const hasContentBeforeAttachment = !isEmpty(previews) || !isEmpty(quote) || !isEmpty(text);
135137

136-
const toolTipTitle = formatFullDate(new Date(serverTimestamp || timestamp));
137-
138138
return (
139139
<StyledMessageContent
140140
className={classNames('module-message__container', `module-message__container--${direction}`)}

ts/components/conversation/message/message-item/DateBreak.tsx

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,7 @@
11
import styled from 'styled-components';
22
import { DURATION } from '../../../../session/constants';
3-
import {
4-
formatFullDate,
5-
formatRelativeTimestampWithLocale,
6-
} from '../../../../util/i18n/formatting/generics';
3+
import { formatRelativeTimestampWithLocale } from '../../../../util/i18n/formatting/generics';
4+
import { useFormatFullDate } from '../../../../hooks/useFormatFullDate';
75

86
const DateBreakContainer = styled.div``;
97

@@ -20,12 +18,13 @@ const DateBreakText = styled.div`
2018

2119
export const MessageDateBreak = (props: { timestamp: number; messageId: string }) => {
2220
const { timestamp, messageId } = props;
21+
const formattedFullDate = useFormatFullDate(timestamp);
2322
// if less than 7 days, we display the "last Thursday at 4:10" syntax
2423
// otherwise, we display the date + time separately
2524
const text =
2625
Date.now() - timestamp <= DURATION.DAYS * 7
2726
? formatRelativeTimestampWithLocale(timestamp)
28-
: formatFullDate(new Date(timestamp));
27+
: formattedFullDate;
2928

3029
return (
3130
<DateBreakContainer id={`date-break-${messageId}`}>

ts/hooks/useFormatFullDate.tsx

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
import { useMemo } from 'react';
2+
import { formatFullDate } from '../util/i18n/formatting/generics';
3+
import { CONVERSATION } from '../session/constants';
4+
5+
export function useFormatFullDate(timestampMs?: number) {
6+
return useMemo(() => {
7+
if (!timestampMs || timestampMs === CONVERSATION.LAST_JOINED_FALLBACK_TIMESTAMP) {
8+
return '';
9+
}
10+
return formatFullDate(new Date(timestampMs));
11+
}, [timestampMs]);
12+
}

ts/localization/constants.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -96,4 +96,3 @@ export type CrowdinLocale = (typeof crowdinLocales)[number];
9696
export function isCrowdinLocale(locale: string): locale is CrowdinLocale {
9797
return crowdinLocales.includes(locale as CrowdinLocale);
9898
}
99-

0 commit comments

Comments
 (0)