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.

5.2 KB
Raw Blame
import {
  ActionIcon,
  Button,
  Center,
  CopyButton,
  // oxlint-disable-next-line stylex-migration/no-emotion-styles -- ActionIcon renders its svg without a slot
  createStaticStyles,
  Flexbox,
  Icon,
  Input,
  Skeleton,
  Tag,
  Text,
} from '@lobehub/ui';
import { cssVar } from '@lobehub/ui/stylex/cssVar.stylex';
import * as stylex from '@stylexjs/stylex';
import { startCase } from 'es-toolkit/compat';
import { LinkIcon, Share2Icon } from 'lucide-react';
import { type ComponentProps, type ReactNode } from 'react';
import { memo, useState } from 'react';
import { useTranslation } from 'react-i18next';

import Avatar from '@/components/Avatar';
import ImperativeModal from '@/components/ImperativeModal';
import SocialShare from '@/features/SocialShare';
import { useShare } from '@/hooks/useShare';

import CardBanner from '../../components/CardBanner';

const styles = stylex.create({
  banner: {
    overflow: 'hidden',
    borderWidth: '1px',
    borderStyle: 'solid',
    borderColor: cssVar.colorBorderSecondary,
    borderRadius: cssVar.borderRadiusLG,
    backgroundColor: cssVar.colorBgContainer,
    backgroundImage: 'none',
    boxShadow: cssVar.boxShadowTertiary,
  },
  copy: {
    backgroundColor: { 'default': cssVar.colorPrimary, ':hover': cssVar.colorPrimaryHover },
    backgroundImage: 'none',
  },
});

interface ShareButtonProps extends ComponentProps<typeof Button> {
  meta?: {
    avatar?: string | ReactNode;
    desc?: string;
    hashtags?: string[];
    tags?: ReactNode;
    title?: string;
    url: string;
  };
}

const fallback = createStaticStyles(({ css }) => ({
  icon: css`
    border: 1px solid var(--lobe-color-fill-secondary);

    svg {
      fill: var(--lobe-color-text-secondary);
    }

    &:hover {
      border: 1px solid var(--lobe-color-border-secondary);

      svg {
        fill: var(--lobe-color-text);
      }
    }
  `,
}));

const ShareButton = memo<ShareButtonProps>(({ meta, ...rest }) => {
  // X and Weibo go through the four-channel SocialShare above; these are the extras.
  const { reddit, telegram, whatsapp, mastodon } = useShare({
    avatar: '',
    desc: '',
    hashtags: [],
    title: '',
    url: '',
    ...meta,
  });
  const { t } = useTranslation('common');
  const [open, setOpen] = useState(false);

  let content;

  if (meta) {
    content = (
      <Center gap={16} style={{ position: 'relative' }} width={'100%'}>
        <Flexbox align={'center'} className={stylex.props(styles.banner).className} width={'100%'}>
          <CardBanner avatar={meta.avatar} size={640} style={{ height: 72, marginBottom: -36 }} />
          <Center
            flex={'none'}
            height={72}
            width={72}
            style={{
              backgroundColor: cssVar.colorBgContainer,
              borderRadius: '50%',
              overflow: 'hidden',
              zIndex: 2,
            }}
          >
            <Avatar animation avatar={meta.avatar} shape={'square'} size={64} />
          </Center>
          <Center padding={12} width={'100%'}>
            <h3 style={{ fontWeight: 'bold', textAlign: 'center' }}>{meta.title}</h3>
            <Text as={'p'} style={{ color: cssVar.colorTextSecondary, textAlign: 'center' }}>
              {meta.desc}
            </Text>
            {meta.hashtags && (
              <Flexbox horizontal align={'center'} gap={4} justify={'center'} wrap={'wrap'}>
                {meta.hashtags.map((tag, index) => (
                  <Tag key={index}>{startCase(tag).trim()}</Tag>
                ))}
              </Flexbox>
            )}
            {meta.tags}
          </Center>
        </Flexbox>
        <SocialShare
          description={meta.desc}
          shareUrl={meta.url}
          style={{ width: '100%' }}
          title={meta.title || meta.url}
          onLeave={() => setOpen(false)}
        />
        <Flexbox horizontal align={'center'} gap={8} justify={'center'} wrap={'wrap'}>
          {[reddit, telegram, whatsapp, mastodon].map(
            (item) =>
              item.icon && (
                <a href={item.link} key={item.title} rel="noreferrer" target="_blank">
                  <ActionIcon
                    className={fallback.icon}
                    icon={item.icon}
                    size={{ blockSize: 36, borderRadius: 18, size: 16 }}
                    title={item.title}
                  />
                </a>
              ),
          )}
        </Flexbox>
        <Flexbox horizontal align={'center'} gap={8} width={'100%'}>
          <Input value={meta.url} variant={'filled'} />
          <CopyButton
            className={stylex.props(styles.copy).className}
            color={cssVar.colorBgLayout}
            content={meta.url}
            icon={LinkIcon}
            size={{ blockSize: 36, size: 16 }}
          />
        </Flexbox>
      </Center>
    );
  } else {
    content = <Skeleton.Text rows={4} />;
  }

  return (
    <>
      <Button
        aria-label={t('share')}
        icon={<Icon icon={Share2Icon} />}
        size={'large'}
        onClick={() => setOpen(true)}
        {...rest}
      />
      <ImperativeModal
        footer={null}
        open={open}
        title={t('share')}
        width={360}
        onCancel={() => setOpen(false)}
      >
        {content}
      </ImperativeModal>
    </>
  );
});

export default ShareButton;