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.

4.8 KB
Raw Blame
import { BRANDING_PROVIDER } from '@lobechat/business-const';
import { Avatar, cssVar, Divider, Flexbox, Skeleton, Tag, Text } from '@lobehub/ui';
import * as stylex from '@stylexjs/stylex';
import { memo } from 'react';
import { useTranslation } from 'react-i18next';

import { BrandingProviderCard } from '@/business/client/features/BrandingProviderCard';
import { ProviderCombine, ProviderIcon } from '@/components/LobeIcons';
import { useIsDark } from '@/hooks/useIsDark';
import { useDensityTokens } from '@/styles/density';
import { type AiProviderListItem } from '@/types/aiProvider';

import EnableSwitch from './EnableSwitch';
import { styles } from './style';

const isCodingPlanProvider = (id: string) => id.endsWith('codingplan');

/**
 * ChatGPT is an OpenAI subscription, so its icon config points at the OpenAI
 * mark — ProviderCombine renders the OpenAI wordmark and the card reads as a
 * duplicate of the OpenAI one right beside it. Name these providers after the
 * plan instead, the way the detail page header does. Doubao Speech borrows the Doubao
 * mark, whose wordmark reads 豆包大模型 (the Ark models), so it is named too.
 */
const sharesVendorMark = (id: string) => id === 'chatgpt' || id === 'doubaospeech';

interface ProviderCardProps extends AiProviderListItem {
  loading?: boolean;
  onProviderSelect: (provider: string) => void;
}
const ProviderCard = memo<ProviderCardProps>(
  ({ id, description, name, enabled, source, logo, loading, onProviderSelect }) => {
    const { t } = useTranslation('providers');
    const isDarkMode = useIsDark();
    const { pageTitleFontSize } = useDensityTokens();

    if (loading)
      return (
        <Flexbox
          gap={24}
          padding={16}
          className={
            stylex.props(isDarkMode ? styles.containerDark : styles.containerLight).className
          }
        >
          <Skeleton />
        </Flexbox>
      );

    if (id === BRANDING_PROVIDER) {
      return <BrandingProviderCard />;
    }

    return (
      <Flexbox
        gap={24}
        className={
          stylex.props(isDarkMode ? styles.containerDark : styles.containerLight).className
        }
      >
        <Flexbox gap={12} padding={16} width={'100%'}>
          <div
            style={{ cursor: 'pointer' }}
            onClick={() => {
              onProviderSelect(id);
            }}
          >
            <Flexbox gap={12} width={'100%'}>
              <Flexbox horizontal align={'center'} justify={'space-between'}>
                {source === 'builtin' ? (
                  <Flexbox horizontal align={'center'} gap={8}>
                    {sharesVendorMark(id) ? (
                      <Flexbox horizontal align={'center'} gap={12}>
                        <ProviderIcon
                          provider={id}
                          size={24}
                          style={{ borderRadius: 6 }}
                          type={'avatar'}
                        />
                        <Text style={{ fontSize: pageTitleFontSize, fontWeight: 'bold' }}>
                          {name || id}
                        </Text>
                      </Flexbox>
                    ) : (
                      <ProviderCombine
                        provider={id}
                        size={24}
                        style={{ color: cssVar.colorText }}
                        title={name}
                      />
                    )}
                    {isCodingPlanProvider(id) && <Tag color={'geekblue'}>{'Coding Plan'}</Tag>}
                  </Flexbox>
                ) : (
                  <Flexbox horizontal align={'center'} gap={12}>
                    {logo ? (
                      <Avatar alt={name || id} avatar={logo} size={28} />
                    ) : (
                      <ProviderIcon
                        provider={id}
                        size={24}
                        style={{ borderRadius: 6 }}
                        type={'avatar'}
                      />
                    )}
                    <Text style={{ fontSize: pageTitleFontSize, fontWeight: 'bold' }}>
                      {name || id}
                    </Text>
                  </Flexbox>
                )}
              </Flexbox>
              <Text
                className={stylex.props(styles.desc).className}
                ellipsis={{
                  rows: 2,
                }}
              >
                {source === 'custom'
                  ? description
                  : description && t(`${id}.description`, { defaultValue: description })}
              </Text>
            </Flexbox>
          </div>
          <Divider style={{ margin: '4px 0' }} />
          <Flexbox horizontal justify={'space-between'}>
            <div />
            <EnableSwitch enabled={enabled} id={id} />
          </Flexbox>
        </Flexbox>
      </Flexbox>
    );
  },
);

export default ProviderCard;