Skip to content

Commit 7a8d55b

Browse files
committed
refactor: match timestamp formatting to existing SDK patterns
1 parent e0fec90 commit 7a8d55b

17 files changed

Lines changed: 50 additions & 46 deletions

File tree

package/src/components/ChannelDetailsScreen/__tests__/useUserActivityStatus.test.tsx

Lines changed: 12 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,30 +1,24 @@
11
import React, { type PropsWithChildren } from 'react';
22

33
import { renderHook } from '@testing-library/react-native';
4-
import Dayjs from 'dayjs';
5-
import relativeTime from 'dayjs/plugin/relativeTime';
64
import type { UserResponse } from 'stream-chat';
75

8-
import { TranslationProvider } from '../../../contexts/translationContext/TranslationContext';
6+
import {
7+
TranslationProvider,
8+
type TranslationContextValue,
9+
} from '../../../contexts/translationContext/TranslationContext';
10+
import { Streami18n } from '../../../utils/i18n/Streami18n';
911
import { useUserActivityStatus } from '../hooks/useUserActivityStatus';
1012

11-
Dayjs.extend(relativeTime);
13+
let translators: TranslationContextValue;
14+
15+
beforeAll(async () => {
16+
const i18nInstance = new Streami18n();
17+
translators = (await i18nInstance.getTranslators()) as unknown as TranslationContextValue;
18+
});
1219

1320
const wrapper = ({ children }: PropsWithChildren) => (
14-
<TranslationProvider
15-
value={{
16-
t: ((key: string, options?: Record<string, unknown>) => {
17-
if (options && 'relativeTime' in options) {
18-
return `${key.replace('{{relativeTime}}', String(options.relativeTime))}`;
19-
}
20-
return key;
21-
}) as never,
22-
tDateTimeParser: (input) => Dayjs(input),
23-
userLanguage: 'en',
24-
}}
25-
>
26-
{children}
27-
</TranslationProvider>
21+
<TranslationProvider value={translators}>{children}</TranslationProvider>
2822
);
2923

3024
const userFor = (overrides: Partial<UserResponse> = {}): UserResponse =>

package/src/components/ChannelDetailsScreen/hooks/useUserActivityStatus.ts

Lines changed: 14 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,19 @@
11
import { useMemo } from 'react';
22

3-
import Dayjs from 'dayjs';
4-
import relativeTime from 'dayjs/plugin/relativeTime';
5-
63
import type { UserResponse } from 'stream-chat';
74

85
import { useTranslationContext } from '../../../contexts/translationContext/TranslationContext';
9-
10-
Dayjs.extend(relativeTime);
11-
12-
const hasFromNow = (value: unknown): value is { fromNow: () => string; isValid?: () => boolean } =>
13-
typeof (value as { fromNow?: unknown })?.fromNow === 'function';
6+
import { getDateString } from '../../../utils/i18n/getDateString';
147

158
/**
169
* Returns the localized presence status string for a user:
1710
* - `t('Online')` when `user.online === true`
18-
* - `t('Last seen {{relativeTime}}')` when offline with a valid `last_active`
19-
* - `t('Offline')` otherwise (including `user === undefined`)
11+
* - `t('timestamp/UserActivityStatus')` (e.g. "Last seen 10 minutes ago") when offline
12+
* with a valid `last_active`
13+
* - `t('Offline')` otherwise (including `user === undefined` or an unparseable date)
2014
*
21-
* Uses `tDateTimeParser` from the translation context so the relative-time format
22-
* follows the configured locale.
15+
* The relative time is produced through the shared `getDateString` + translation-key
16+
* pipeline used by message timestamps, so the format follows the configured locale.
2317
* @experimental This hook is experimental and is subject to change.
2418
*/
2519
export const useUserActivityStatus = (user?: UserResponse): string => {
@@ -29,9 +23,14 @@ export const useUserActivityStatus = (user?: UserResponse): string => {
2923
if (user?.online) return t('Online');
3024

3125
if (user?.last_active) {
32-
const parsed = tDateTimeParser(user.last_active);
33-
if (hasFromNow(parsed) && (!parsed.isValid || parsed.isValid())) {
34-
return t('Last seen {{relativeTime}}', { relativeTime: parsed.fromNow() });
26+
const lastSeen = getDateString({
27+
date: user.last_active,
28+
t,
29+
tDateTimeParser,
30+
timestampTranslationKey: 'timestamp/UserActivityStatus',
31+
});
32+
if (typeof lastSeen === 'string') {
33+
return lastSeen;
3534
}
3635
}
3736

package/src/i18n/ar.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -325,7 +325,7 @@
325325
"Edit": "تعديل",
326326
"Files": "الملفات",
327327
"Group Info": "معلومات المجموعة",
328-
"Last seen {{relativeTime}}": "آخر ظهور {{relativeTime}}",
328+
"timestamp/UserActivityStatus": "آخر ظهور {{ timestamp | fromNowFormatter }}",
329329
"Photos & Videos": "الصور ومقاطع الفيديو",
330330
"Pinned Messages": "الرسائل المثبتة",
331331
"View all": "عرض الكل",

package/src/i18n/en.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -326,7 +326,7 @@
326326
"Edit": "Edit",
327327
"Files": "Files",
328328
"Group Info": "Group Info",
329-
"Last seen {{relativeTime}}": "Last seen {{relativeTime}}",
329+
"timestamp/UserActivityStatus": "Last seen {{ timestamp | fromNowFormatter }}",
330330
"Photos & Videos": "Photos & Videos",
331331
"Pinned Messages": "Pinned Messages",
332332
"View all": "View all",

package/src/i18n/es.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -325,7 +325,7 @@
325325
"Edit": "Editar",
326326
"Files": "Archivos",
327327
"Group Info": "Información del grupo",
328-
"Last seen {{relativeTime}}": "Última conexión {{relativeTime}}",
328+
"timestamp/UserActivityStatus": "Última conexión {{ timestamp | fromNowFormatter }}",
329329
"Photos & Videos": "Fotos y videos",
330330
"Pinned Messages": "Mensajes fijados",
331331
"View all": "Ver todo",

package/src/i18n/fr.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -325,7 +325,7 @@
325325
"Edit": "Modifier",
326326
"Files": "Fichiers",
327327
"Group Info": "Informations du groupe",
328-
"Last seen {{relativeTime}}": "Vu pour la dernière fois {{relativeTime}}",
328+
"timestamp/UserActivityStatus": "Vu pour la dernière fois {{ timestamp | fromNowFormatter }}",
329329
"Photos & Videos": "Photos et vidéos",
330330
"Pinned Messages": "Messages épinglés",
331331
"View all": "Tout voir",

package/src/i18n/he.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -325,7 +325,7 @@
325325
"Edit": "ערוך",
326326
"Files": "קבצים",
327327
"Group Info": "פרטי הקבוצה",
328-
"Last seen {{relativeTime}}": "נראה לאחרונה {{relativeTime}}",
328+
"timestamp/UserActivityStatus": "נראה לאחרונה {{ timestamp | fromNowFormatter }}",
329329
"Photos & Videos": "תמונות וסרטונים",
330330
"Pinned Messages": "הודעות מוצמדות",
331331
"View all": "הצג הכל",

package/src/i18n/hi.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -325,7 +325,7 @@
325325
"Edit": "संपादित करें",
326326
"Files": "फ़ाइलें",
327327
"Group Info": "ग्रुप की जानकारी",
328-
"Last seen {{relativeTime}}": "अंतिम बार देखा गया {{relativeTime}}",
328+
"timestamp/UserActivityStatus": "अंतिम बार देखा गया {{ timestamp | fromNowFormatter }}",
329329
"Photos & Videos": "फ़ोटो और वीडियो",
330330
"Pinned Messages": "पिन किए गए संदेश",
331331
"View all": "सभी देखें",

package/src/i18n/it.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -325,7 +325,7 @@
325325
"Edit": "Modifica",
326326
"Files": "File",
327327
"Group Info": "Informazioni del gruppo",
328-
"Last seen {{relativeTime}}": "Ultimo accesso {{relativeTime}}",
328+
"timestamp/UserActivityStatus": "Ultimo accesso {{ timestamp | fromNowFormatter }}",
329329
"Photos & Videos": "Foto e video",
330330
"Pinned Messages": "Messaggi in evidenza",
331331
"View all": "Vedi tutto",

package/src/i18n/ja.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -325,7 +325,7 @@
325325
"Edit": "編集",
326326
"Files": "ファイル",
327327
"Group Info": "グループ情報",
328-
"Last seen {{relativeTime}}": "最終ログイン {{relativeTime}}",
328+
"timestamp/UserActivityStatus": "最終ログイン {{ timestamp | fromNowFormatter }}",
329329
"Photos & Videos": "写真と動画",
330330
"Pinned Messages": "ピン留めされたメッセージ",
331331
"View all": "すべて表示",

0 commit comments

Comments
 (0)