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.

8.1 KB
Raw Blame
'use client';

import { DEFAULT_USER_AVATAR_URL } from '@lobechat/const';
import type { NotificationMetadata } from '@lobechat/types';
import {
  Avatar,
  Block,
  Button,
  ContextMenuTrigger,
  Flexbox,
  Icon,
  Text,
  useModalContext,
} from '@lobehub/ui';
import { cssVar } from '@lobehub/ui/stylex/cssVar.stylex';
import * as stylex from '@stylexjs/stylex';
import { ArchiveIcon, BellIcon, ImageIcon, MegaphoneIcon, VideoIcon } from 'lucide-react';
import type { MouseEvent } from 'react';
import { memo, useCallback, useMemo } from 'react';
import { useTranslation } from 'react-i18next';

import AgentAvatar from '@/components/AgentAvatar';
import { ProductLogo } from '@/components/Branding';
import { useWorkspaceAwareNavigate } from '@/features/Workspace/useWorkspaceAwareNavigate';
import { useAppOrigin } from '@/hooks/useAppOrigin';
import type { NativeContextMenuItem } from '@/libs/contextMenu/types';
import { useInterfaceDensity } from '@/styles/density';
import { openTrustedExternalUrl } from '@/utils/openTrustedExternalUrl';

import { formatNotificationRelativeTime } from './formatNotificationRelativeTime';
import { createNotificationDetailModal } from './NotificationDetailModal';
import { getNotificationTabRoutes } from './notificationTabRoutes';
import { resolveNotificationAction } from './resolveNotificationAction';
import { toNotificationPreview } from './toNotificationPreview';
import { useNotificationAgentMeta } from './useNotificationAgentMeta';

const styles = stylex.create({
  unreadDot: {
    flexShrink: 0,
    width: '8px',
    height: '8px',
    marginBlockStart: '7px',
    borderRadius: '50%',
    backgroundColor: cssVar.colorInfo,
    backgroundImage: 'none',
  },
  // Centers the dot on the first 20px compact text line.
  unreadDotCompact: { marginBlockStart: '6px' },
});

const TYPE_ICON_MAP: Record<string, typeof BellIcon> = {
  image_generation_completed: ImageIcon,
  system_announcement: MegaphoneIcon,
  video_generation_completed: VideoIcon,
};

interface NotificationItemProps {
  actionUrl?: string | null;
  content: string;
  context?: string | null;
  createdAt: Date | string;
  id: string;
  isRead: boolean;
  metadata?: NotificationMetadata | null;
  onArchive: (id: string) => void;
  onMarkAsRead: (id: string) => void;
  title: string;
  type: string;
}

const NotificationItem = memo<NotificationItemProps>(
  ({
    id,
    type,
    title,
    content,
    context,
    createdAt,
    isRead,
    actionUrl,
    metadata,
    onMarkAsRead,
    onArchive,
  }) => {
    const { i18n, t } = useTranslation('notification');
    const compact = useInterfaceDensity() === 'compact';
    const avatarSize = compact ? 24 : 32;
    const { close } = useModalContext();
    const navigate = useWorkspaceAwareNavigate();
    const appOrigin = useAppOrigin();
    const TypeIcon = TYPE_ICON_MAP[type] || BellIcon;
    const dateLocale = i18n.resolvedLanguage || i18n.language;
    const preview = useMemo(() => toNotificationPreview(content), [content]);
    const actor = metadata?.actor;
    const agent = useNotificationAgentMeta(actionUrl, metadata);

    const handleMarkAsRead = useCallback(() => {
      if (!isRead) onMarkAsRead(id);
    }, [id, isRead, onMarkAsRead]);

    const handleAction = useCallback(() => {
      handleMarkAsRead();

      if (!actionUrl) return;

      const action = resolveNotificationAction(actionUrl, {
        appOrigin,
        desktopTabRoutes: getNotificationTabRoutes(),
      });

      if (action.type === 'external') {
        openTrustedExternalUrl(action.url);
      } else {
        navigate(action.path);
        close();
      }
    }, [actionUrl, appOrigin, close, handleMarkAsRead, navigate]);

    // The row only shows a two-line preview, so a click must always give access
    // to the full body — the detail modal is the only place a content-only
    // notification (no actionUrl) can be read in full.
    const handleOpenDetail = useCallback(() => {
      handleMarkAsRead();
      createNotificationDetailModal({
        content,
        context,
        createdAt,
        onAction: actionUrl && !metadata?.transfer ? handleAction : undefined,
        title,
      });
    }, [actionUrl, content, context, createdAt, handleAction, handleMarkAsRead, metadata, title]);

    const handleActionClick = useCallback(
      (event: MouseEvent) => {
        event.stopPropagation();
        handleAction();
      },
      [handleAction],
    );

    const handleArchive = useCallback(() => onArchive(id), [id, onArchive]);

    const contextMenuItems: NativeContextMenuItem[] = [
      {
        icon: ArchiveIcon,
        key: 'archive',
        label: t('inbox.archive'),
        onClick: handleArchive,
        sfSymbol: 'archivebox',
      },
    ];

    return (
      <ContextMenuTrigger items={contextMenuItems}>
        <Block
          aria-label={title}
          gap={4}
          paddingBlock={compact ? 8 : 12}
          paddingInline={compact ? 16 : 20}
          style={{ cursor: 'pointer' }}
          variant="borderless"
          onClick={handleOpenDetail}
        >
          <Flexbox horizontal align="flex-start" gap={compact ? 8 : 12}>
            {!isRead && (
              <span {...stylex.props(styles.unreadDot, compact && styles.unreadDotCompact)} />
            )}
            {actor ? (
              <Avatar
                alt={actor.name}
                avatar={actor.avatar || DEFAULT_USER_AVATAR_URL}
                shape="circle"
                size={avatarSize}
                style={{ flex: 'none' }}
              />
            ) : agent ? (
              <AgentAvatar
                agentId={agent.id}
                alt={agent.title}
                avatar={agent.avatar}
                background={agent.backgroundColor}
                name={agent.title}
                shape="circle"
                size={avatarSize}
                style={{ flex: 'none' }}
              />
            ) : (
              <ProductLogo size={avatarSize} style={{ flex: 'none' }} />
            )}
            <Flexbox flex={1} gap={compact ? 2 : 6} style={{ minWidth: 0, overflow: 'hidden' }}>
              <Text
                ellipsis={{ rows: 2 }}
                fontSize={compact ? 13 : undefined}
                wordBreak="break-word"
              >
                {preview}
              </Text>
              <Flexbox horizontal align="center" gap={6}>
                {/* The actor snapshot exists so the reader can tell WHO acted
                    without recognizing the avatar — render the name visibly. */}
                {actor?.name && (
                  <Text
                    ellipsis
                    fontSize={12}
                    style={{ flexShrink: 0, maxWidth: 160 }}
                    type="secondary"
                  >
                    {actor.name}
                  </Text>
                )}
                <Text fontSize={12} style={{ flexShrink: 0 }} type="secondary">
                  {formatNotificationRelativeTime(createdAt, dateLocale)}
                </Text>
                {context && (
                  <>
                    <Icon color={cssVar.colorTextDescription} icon={TypeIcon} size={12} />
                    <Text ellipsis fontSize={12} type="secondary">
                      {context}
                    </Text>
                  </>
                )}
              </Flexbox>
            </Flexbox>
            {/* Always reserve the action column so text width stays aligned across rows */}
            <Flexbox
              horizontal
              align="center"
              flex="none"
              justify="flex-end"
              style={{ alignSelf: 'stretch', width: 144 }}
            >
              {/* Transfer-linked rows keep an actionUrl purely as the email
                  CTA's landing target (workspace home). In-app that target is
                  where the user already is, so no dead "view detail" button. */}
              {actionUrl && !metadata?.transfer && (
                <Button onClick={handleActionClick}>{t('inbox.viewDetail')}</Button>
              )}
            </Flexbox>
          </Flexbox>
        </Block>
      </ContextMenuTrigger>
    );
  },
);

NotificationItem.displayName = 'NotificationItem';

export default NotificationItem;