11import { useMemo } from 'react' ;
22
3- import Dayjs from 'dayjs' ;
4- import relativeTime from 'dayjs/plugin/relativeTime' ;
5-
63import type { UserResponse } from 'stream-chat' ;
74
85import { 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 */
2519export 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
0 commit comments