Skip to content

The open alternative to Salesforce, designed for AI.

2.9 KB
Raw Blame
import { styled } from '@linaria/react';
import { Avatar } from 'twenty-ui/primitives/data-display';
import { useTheme, themeCssVariables } from 'twenty-ui/theme';

import { currentWorkspaceState } from '@/auth/states/currentWorkspaceState';
import { USAGE_LIMIT_SPENDER_TYPE_ICONS } from '@/settings/billing/constants/UsageLimitSpenderTypeIcons';
import { type UsageLimitRow } from '@/settings/billing/types/UsageLimitRow';
import { isKeyOfRecord } from '@/settings/billing/utils/isKeyOfRecord';
import { useAtomStateValue } from '@/ui/utilities/state/jotai/hooks/useAtomStateValue';
import { DEFAULT_WORKSPACE_LOGO } from '@/ui/navigation/navigation-drawer/constants/DefaultWorkspaceLogo';
import { getWorkspaceAvatarColorSeed } from '@/workspace/utils/getWorkspaceAvatarColorSeed';
import { getAbsoluteImageUrl } from '~/utils/image/getAbsoluteImageUrl';

const StyledContainer = styled.div`
  align-items: center;
  display: flex;
  gap: ${themeCssVariables.spacing[2]};
  min-width: 0;
`;

const StyledIcon = styled.div`
  color: ${themeCssVariables.font.color.tertiary};
  display: flex;
  flex-shrink: 0;
`;

const StyledName = styled.span`
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
`;

type SettingsBillingLimitSpenderCellProps = {
  row: UsageLimitRow;
};

export const SettingsBillingLimitSpenderCell = ({
  row,
}: SettingsBillingLimitSpenderCellProps) => {
  const theme = useTheme();
  const currentWorkspace = useAtomStateValue(currentWorkspaceState);

  const isWorkspaceWide = row.spenderType === 'workspace';
  const name = row.spenderName;

  const SpenderIcon = isKeyOfRecord(
    USAGE_LIMIT_SPENDER_TYPE_ICONS,
    row.spenderType,
  )
    ? USAGE_LIMIT_SPENDER_TYPE_ICONS[row.spenderType]
    : undefined;

  const renderVisual = () => {
    if (isWorkspaceWide) {
      return (
        <Avatar
          name={currentWorkspace?.displayName ?? ''}
          colorSeed={getWorkspaceAvatarColorSeed(currentWorkspace?.displayName)}
          src={getAbsoluteImageUrl(
            currentWorkspace?.logo ?? DEFAULT_WORKSPACE_LOGO,
          )}
          shape="square"
          size="sm"
        />
      );
    }

    if (row.spenderType === 'userWorkspace') {
      return (
        <Avatar
          name={name}
          src={row.spenderAvatarUrl}
          shape="circle"
          size="sm"
        />
      );
    }

    if (row.spenderType === 'application') {
      return (
        <Avatar
          name={name}
          src={row.spenderAvatarUrl}
          shape="square"
          size="sm"
        />
      );
    }

    return (
      <StyledIcon>
        {SpenderIcon && (
          <SpenderIcon
            size={theme.icon.size.sm}
            stroke={theme.icon.stroke.sm}
          />
        )}
      </StyledIcon>
    );
  };

  return (
    <StyledContainer>
      {renderVisual()}
      <StyledName>{name}</StyledName>
    </StyledContainer>
  );
};