Skip to content

🤯 LobeHub - Super-Agents on Your Team. Add super-agents to the chats your team already uses.They take on work by themselves and get better every week.

♻️ refactor(time): extract useActivityTime hook and move time keys to common (#14443)

Arvin Xu970733aaeba9
diff --git a/locales/en-US/common.json b/locales/en-US/common.json
index 450b580b309..fc602c6922c 100644
--- a/locales/en-US/common.json
+++ b/locales/en-US/common.json
@@ -448,6 +448,10 @@
"telemetry.title": "Help improve {{appName}}",
"temp": "Temporary",
"terms": "Terms of Service",
+ "time.formatOtherYear": "MMM D, YYYY",
+ "time.formatThisYear": "MMM D",
+ "time.today": "Today",
+ "time.yesterday": "Yesterday",
"unknownError": "Unknown error",
"update": "Update",
"updateAgent": "Update Agent Information",
diff --git a/locales/en-US/discover.json b/locales/en-US/discover.json
index 3172f4b501d..aed4057017b 100644
--- a/locales/en-US/discover.json
+++ b/locales/en-US/discover.json
@@ -602,10 +602,6 @@
"tab.provider": "Provider",
"tab.skill": "Skills",
"tab.user": "User",
- "time.formatOtherYear": "MMM D, YYYY",
- "time.formatThisYear": "MMM D",
- "time.today": "Today",
- "time.yesterday": "Yesterday",
"user.agents": "Agents",
"user.cancel": "Cancel",
"user.downloads": "Downloads",
diff --git a/locales/zh-CN/common.json b/locales/zh-CN/common.json
index f8d38e5bb6d..1a18117fb11 100644
--- a/locales/zh-CN/common.json
+++ b/locales/zh-CN/common.json
@@ -448,6 +448,10 @@
"telemetry.title": "帮助 {{appName}} 做得更好",
"temp": "临时",
"terms": "服务条款",
+ "time.formatOtherYear": "YYYY年M月D日",
+ "time.formatThisYear": "M月D日",
+ "time.today": "今天",
+ "time.yesterday": "昨天",
"unknownError": "未知错误",
"update": "更新",
"updateAgent": "更新助理信息",
diff --git a/locales/zh-CN/discover.json b/locales/zh-CN/discover.json
index 67ade395aaa..68f908450a6 100644
--- a/locales/zh-CN/discover.json
+++ b/locales/zh-CN/discover.json
@@ -602,10 +602,6 @@
"tab.provider": "模型服务商",
"tab.skill": "Skills",
"tab.user": "用户",
- "time.formatOtherYear": "YYYY年M月D日",
- "time.formatThisYear": "M月D日",
- "time.today": "今天",
- "time.yesterday": "昨天",
"user.agents": "助理",
"user.cancel": "取消",
"user.downloads": "下载",
diff --git a/src/components/PublishedTime.tsx b/src/components/PublishedTime.tsx
index fe9a5c8b81e..92a8aa7dcb7 100644
--- a/src/components/PublishedTime.tsx
+++ b/src/components/PublishedTime.tsx
@@ -44,7 +44,7 @@ interface PublishedTimeProps {
template?: string;
}
const PublishedTime: FC<PublishedTimeProps> = ({ date, style, className, template }) => {
- const { t } = useTranslation('discover');
+ const { t } = useTranslation('common');
const time = formatDate(date, t as (key: string) => string, template);
return (
diff --git a/src/features/AgentTasks/AgentTaskDetail/CommentCard.tsx b/src/features/AgentTasks/AgentTaskDetail/CommentCard.tsx
index 43597a62112..78bee3bf80f 100644
--- a/src/features/AgentTasks/AgentTaskDetail/CommentCard.tsx
+++ b/src/features/AgentTasks/AgentTaskDetail/CommentCard.tsx
@@ -1,5 +1,4 @@
import type { TaskDetailActivity } from '@lobechat/types';
-import { formatActivityTime } from '@lobechat/utils/time';
import { Editor, useEditor } from '@lobehub/editor/react';
import {
ActionIcon,
@@ -19,6 +18,7 @@ import { MessageCircle, MoreHorizontal, Pencil, Trash } from 'lucide-react';
import { memo, useCallback, useMemo, useState } from 'react';
import { useTranslation } from 'react-i18next';
+import { useActivityTime } from '@/hooks/useActivityTime';
import { useTaskStore } from '@/store/task';
import { styles } from '../shared/style';
@@ -29,7 +29,6 @@ interface CommentCardProps {
const CommentCard = memo<CommentCardProps>(({ activity }) => {
const { t } = useTranslation('chat');
- const { t: tDiscover } = useTranslation('discover');
const { modal } = App.useApp();
const deleteComment = useTaskStore((s) => s.deleteComment);
const updateComment = useTaskStore((s) => s.updateComment);
@@ -38,10 +37,7 @@ const CommentCard = memo<CommentCardProps>(({ activity }) => {
const [submitting, setSubmitting] = useState(false);
const editor = useEditor();
- const { text: relTime, title: relTimeTitle } = formatActivityTime(activity.time, {
- formatOtherYear: tDiscover('time.formatOtherYear'),
- formatThisYear: tDiscover('time.formatThisYear'),
- });
+ const { text: relTime, title: relTimeTitle } = useActivityTime(activity.time);
const content = activity.content || t('taskDetail.activities.fallback.comment');
const commentId = activity.id;
diff --git a/src/features/AgentTasks/AgentTaskDetail/TaskActivities.tsx b/src/features/AgentTasks/AgentTaskDetail/TaskActivities.tsx
index e404fe668c7..9cf51415bfc 100644
--- a/src/features/AgentTasks/AgentTaskDetail/TaskActivities.tsx
+++ b/src/features/AgentTasks/AgentTaskDetail/TaskActivities.tsx
@@ -1,5 +1,4 @@
import type { BriefType, TaskDetailActivity } from '@lobechat/types';
-import { formatActivityTime } from '@lobechat/utils/time';
import { Accordion, AccordionItem, Avatar, Empty, Flexbox, Icon, Tag, Text } from '@lobehub/ui';
import { cssVar } from 'antd-style';
import type { TFunction } from 'i18next';
@@ -9,6 +8,7 @@ import { useTranslation } from 'react-i18next';
import AgentProfilePopup from '@/features/AgentProfileCard/AgentProfilePopup';
import type { BriefItem } from '@/features/DailyBrief/types';
+import { useActivityTime } from '@/hooks/useActivityTime';
import { useTaskStore } from '@/store/task';
import { taskActivitySelectors, taskDetailSelectors } from '@/store/task/selectors';
@@ -64,12 +64,8 @@ const getRowText = (act: TaskDetailActivity, t: TFunction<'chat'>): string => {
/** Compact one-line row for topic / comment activities. */
const ActivityRow = memo<{ activity: TaskDetailActivity }>(({ activity }) => {
const { t } = useTranslation('chat');
- const { t: tDiscover } = useTranslation('discover');
const TypeIcon = ROW_TYPE_ICON[activity.type as keyof typeof ROW_TYPE_ICON] ?? MessageCircle;
- const { text: relTime, title: relTimeTitle } = formatActivityTime(activity.time, {
- formatOtherYear: tDiscover('time.formatOtherYear'),
- formatThisYear: tDiscover('time.formatThisYear'),
- });
+ const { text: relTime, title: relTimeTitle } = useActivityTime(activity.time);
const text = getRowText(activity, t);
const isAgent = activity.author?.type === 'agent';
diff --git a/src/features/AgentTasks/AgentTaskDetail/TopicCard.tsx b/src/features/AgentTasks/AgentTaskDetail/TopicCard.tsx
index 8a576de37dc..0874ac9c487 100644
--- a/src/features/AgentTasks/AgentTaskDetail/TopicCard.tsx
+++ b/src/features/AgentTasks/AgentTaskDetail/TopicCard.tsx
@@ -1,5 +1,4 @@
import type { TaskDetailActivity } from '@lobechat/types';
-import { formatActivityTime } from '@lobechat/utils/time';
import {
ActionIcon,
Avatar,
@@ -16,6 +15,7 @@ import { memo, useCallback, useEffect, useState } from 'react';
import { useTranslation } from 'react-i18next';
import AgentProfilePopup from '@/features/AgentProfileCard/AgentProfilePopup';
+import { useActivityTime } from '@/hooks/useActivityTime';
import { useTaskStore } from '@/store/task';
import { styles } from '../shared/style';
@@ -36,7 +36,6 @@ interface TopicCardProps {
const TopicCard = memo<TopicCardProps>(({ activity }) => {
const { t } = useTranslation('chat');
- const { t: tDiscover } = useTranslation('discover');
const openTopicDrawer = useTaskStore((s) => s.openTopicDrawer);
const isRunning = activity.status === 'running';
@@ -69,10 +68,7 @@ const TopicCard = memo<TopicCardProps>(({ activity }) => {
if (activity.operationId) void navigator.clipboard.writeText(activity.operationId);
}, [activity.operationId]);
- const { text: startedAt, title: startedAtTitle } = formatActivityTime(activity.time, {
- formatOtherYear: tDiscover('time.formatOtherYear'),
- formatThisYear: tDiscover('time.formatThisYear'),
- });
+ const { text: startedAt, title: startedAtTitle } = useActivityTime(activity.time);
const durationText = isRunning
? formatDuration(elapsed)
: finalDuration != null && finalDuration >= 0
diff --git a/src/features/AgentTasks/features/AgentTaskItem.tsx b/src/features/AgentTasks/features/AgentTaskItem.tsx
index 319e691c38f..18fc980bfdb 100644
--- a/src/features/AgentTasks/features/AgentTaskItem.tsx
+++ b/src/features/AgentTasks/features/AgentTaskItem.tsx
@@ -36,7 +36,7 @@ const toTaskStatus = (status: string): TaskStatus =>
TASK_STATUS_SET.has(status as TaskStatus) ? (status as TaskStatus) : 'backlog';
const AgentTaskItem = memo<TaskItemProps>(({ task, variant = 'default' }) => {
- const { t } = useTranslation('discover');
+ const { t } = useTranslation('common');
const { t: tChat } = useTranslation('chat');
const useFetchTaskDetail = useTaskStore((s) => s.useFetchTaskDetail);
useFetchTaskDetail(task.identifier);
diff --git a/src/features/Conversation/ChatItem/components/Title.tsx b/src/features/Conversation/ChatItem/components/Title.tsx
index a95492932a3..27a5a51a88d 100644
--- a/src/features/Conversation/ChatItem/components/Title.tsx
+++ b/src/features/Conversation/ChatItem/components/Title.tsx
@@ -1,8 +1,9 @@
import { Text } from '@lobehub/ui';
-import dayjs from 'dayjs';
import { memo } from 'react';
import { useTranslation } from 'react-i18next';
+import { useActivityTime } from '@/hooks/useActivityTime';
+
import { type ChatItemProps } from '../type';
export interface TitleProps {
@@ -15,6 +16,7 @@ export interface TitleProps {
const Title = memo<TitleProps>(({ showTitle, time, avatar, titleAddon }) => {
const { t } = useTranslation('chat');
const title = avatar.title || t('untitledAgent');
+ const { text: timeText, title: timeTitle } = useActivityTime(time);
return (
<>
@@ -24,15 +26,15 @@ const Title = memo<TitleProps>(({ showTitle, time, avatar, titleAddon }) => {
</Text>
)}
{showTitle ? titleAddon : undefined}
- {!time ? null : (
+ {!timeText ? null : (
<Text
aria-label="published-date"
as={'time'}
fontSize={12}
- title={dayjs(time).format('YYYY-MM-DD HH:mm:ss')}
+ title={timeTitle}
type={'secondary'}
>
- {dayjs(time).fromNow()}
+ {timeText}
</Text>
)}
</>
diff --git a/src/hooks/useActivityTime.ts b/src/hooks/useActivityTime.ts
new file mode 100644
index 00000000000..b0f197b9793
--- /dev/null
+++ b/src/hooks/useActivityTime.ts
@@ -0,0 +1,29 @@
+import {
+ formatActivityTime,
+ type FormatActivityTimeOptions,
+ type FormattedActivityTime,
+} from '@lobechat/utils/time';
+import { useTranslation } from 'react-i18next';
+
+export type UseActivityTimeOptions = Omit<
+ FormatActivityTimeOptions,
+ 'formatOtherYear' | 'formatThisYear'
+>;
+
+/**
+ * Format a timestamp using the activity-feed convention: relative within
+ * `relativeThresholdMs` (default 24h), absolute date afterwards. The format
+ * strings are pulled from the `common` namespace so callers don't need to
+ * wire up i18n themselves.
+ */
+export const useActivityTime = (
+ time?: string | Date | number | null,
+ options?: UseActivityTimeOptions,
+): FormattedActivityTime => {
+ const { t } = useTranslation('common');
+ return formatActivityTime(time, {
+ ...options,
+ formatOtherYear: t('time.formatOtherYear'),
+ formatThisYear: t('time.formatThisYear'),
+ });
+};
diff --git a/src/locales/default/common.ts b/src/locales/default/common.ts
index f193bedc023..82329cce35e 100644
--- a/src/locales/default/common.ts
+++ b/src/locales/default/common.ts
@@ -530,6 +530,10 @@ export default {
'telemetry.title': 'Help improve {{appName}}',
'temp': 'Temporary',
'terms': 'Terms of Service',
+ 'time.formatOtherYear': 'MMM D, YYYY',
+ 'time.formatThisYear': 'MMM D',
+ 'time.today': 'Today',
+ 'time.yesterday': 'Yesterday',
'unknownError': 'Unknown error',
'update': 'Update',
'updateAgent': 'Update Agent Information',
diff --git a/src/locales/default/discover.ts b/src/locales/default/discover.ts
index 93b71d8ea85..d2975c58858 100644
--- a/src/locales/default/discover.ts
+++ b/src/locales/default/discover.ts
@@ -855,11 +855,6 @@ export default {
'publishedTime': 'Published',
- 'time.formatOtherYear': 'MMM D, YYYY',
- 'time.formatThisYear': 'MMM D',
- 'time.today': 'Today',
- 'time.yesterday': 'Yesterday',
-
'search.placeholder': 'Search by name, description, or keywords...',
'search.result': '{{count}} results about <highlight>{{keyword}}</highlight>',
diff --git a/src/routes/(main)/home/features/components/Time.tsx b/src/routes/(main)/home/features/components/Time.tsx
index 0b1e40f112e..e80523f6327 100644
--- a/src/routes/(main)/home/features/components/Time.tsx
+++ b/src/routes/(main)/home/features/components/Time.tsx
@@ -1,14 +1,10 @@
-import { formatActivityTime } from '@lobechat/utils/time';
import { Text } from '@lobehub/ui';
import { memo } from 'react';
-import { useTranslation } from 'react-i18next';
+
+import { useActivityTime } from '@/hooks/useActivityTime';
export const Time = memo<{ date: string | number | Date }>(({ date }) => {
- const { t } = useTranslation('discover');
- const { text, title } = formatActivityTime(date, {
- formatOtherYear: t('time.formatOtherYear'),
- formatThisYear: t('time.formatThisYear'),
- });
+ const { text, title } = useActivityTime(date);
if (!text) return null;
return (
<Text fontSize={12} style={{ flex: 'none' }} title={title} type={'secondary'}>
diff --git a/src/routes/(main)/memory/features/Time.tsx b/src/routes/(main)/memory/features/Time.tsx
index 7e834e1d8dc..95634057a1c 100644
--- a/src/routes/(main)/memory/features/Time.tsx
+++ b/src/routes/(main)/memory/features/Time.tsx
@@ -1,25 +1,25 @@
import { Text } from '@lobehub/ui';
-import dayjs from 'dayjs';
import { memo } from 'react';
+import { useActivityTime } from '@/hooks/useActivityTime';
+
interface TimeProps {
capturedAt?: Date | number | string;
}
const Time = memo<TimeProps>(({ capturedAt }) => {
- if (!capturedAt) return;
-
- const datetime = dayjs(capturedAt);
+ const { text, title } = useActivityTime(capturedAt);
+ if (!text) return null;
return (
<Text
as={'time'}
fontSize={12}
style={{ display: 'block', flex: 'none' }}
- title={datetime.format('YYYY-MM-DD HH:mm')}
+ title={title}
type={'secondary'}
>
- {datetime.fromNow()}
+ {text}
</Text>
);
});