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.

2.5 KB
Raw Blame
'use client';

import { ActionIcon } from '@lobehub/ui';
import { Share2 } from 'lucide-react';
import { memo } from 'react';
import { useTranslation } from 'react-i18next';

import { useActiveWorkspaceId } from '@/business/client/hooks/useActiveWorkspaceId';
import { withSuspense } from '@/components/withSuspense';
import { MOBILE_HEADER_ICON_SIZE } from '@/const/layoutTokens';
import { useAgentContext } from '@/features/Conversation/useAgentContext';
import { useShareModal } from '@/features/ShareModal';
import { LazySharePopover as SharePopover } from '@/features/SharePopover/lazy';
import { usePermission } from '@/hooks/usePermission';
import { useServerConfigStore } from '@/store/serverConfig';
import { serverConfigSelectors } from '@/store/serverConfig/selectors';
import { useHeaderActionSize } from '@/styles/density';

interface ShareButtonProps {
  mobile?: boolean;
  open?: boolean;
  setOpen?: (open: boolean) => void;
}

const ShareButton = memo<ShareButtonProps>(({ mobile, setOpen, open }) => {
  const { openShareModal } = useShareModal({ open, setOpen });
  const { t } = useTranslation('common');
  const { agentId, topicId } = useAgentContext();
  const enableTopicLinkShare = useServerConfigStore(serverConfigSelectors.enableBusinessFeatures);
  const { allowed: canShare, reason } = usePermission('edit_own_content');
  const workspaceId = useActiveWorkspaceId();
  const headerActionSmallSize = useHeaderActionSize(true);

  // Hide share button when no topic exists (no messages sent yet)
  if (!topicId) return null;
  // Inside a workspace teammates already reach the topic and collaborate through
  // comments, which take this desktop header slot (TopicCommentButton) while
  // Share moves into the header "..." menu. The mobile header has no such menu,
  // so it keeps the button as the only entry to the link-sharing popover.
  if (workspaceId && !mobile) return null;

  const iconButton = (
    <ActionIcon
      disabled={!canShare}
      icon={Share2}
      size={mobile ? MOBILE_HEADER_ICON_SIZE : headerActionSmallSize}
      title={canShare ? t('share') : reason}
      tooltipProps={{
        placement: 'bottom',
      }}
      onClick={enableTopicLinkShare || !canShare ? undefined : openShareModal}
    />
  );

  if (!canShare) return iconButton;

  return (
    <>
      {enableTopicLinkShare ? (
        <SharePopover agentId={agentId} onOpenModal={openShareModal}>
          {iconButton}
        </SharePopover>
      ) : (
        iconButton
      )}
    </>
  );
});

export default withSuspense(ShareButton);