Skip to content

The open alternative to Salesforce, designed for AI.

4.4 KB
Raw Blame
import { UsageSectionSkeleton } from '@/settings/usage/components/UsageSectionSkeleton';
import { useUsageAnalyticsData } from '@/settings/usage/hooks/useUsageAnalyticsData';
import { useUsageValueFormatter } from '@/settings/usage/hooks/useUsageValueFormatter';
import { Select } from '@/ui/input/components/Select';
import { Table } from '@/ui/layout/table/components/Table';
import { TableCell } from '@/ui/layout/table/components/TableCell';
import { TableHeader } from '@/ui/layout/table/components/TableHeader';
import { TableRow } from '@/ui/layout/table/components/TableRow';
import { styled } from '@linaria/react';
import { useState } from 'react';
import { SearchInput } from 'twenty-ui/components/input';
import { Section } from 'twenty-ui/components/layout';
import { IconChevronRight } from 'twenty-ui/icon';
import { Avatar } from 'twenty-ui/primitives/data-display';
import { useTheme, themeCssVariables } from 'twenty-ui/theme';
import { type UsageOperationType } from '~/generated-metadata/graphql';
import { normalizeSearchText } from 'twenty-ui/utilities';
import { useLingui } from '@lingui/react/macro';

const StyledSearchInputContainer = styled.div`
  padding-bottom: ${themeCssVariables.spacing[2]};
`;

const StyledIconChevronRightContainer = styled.div`
  color: ${themeCssVariables.font.color.tertiary};
`;

const GRID_TEMPLATE_COLUMNS = '1fr 120px 36px';

type UsageByUserTableSectionProps = {
  title: string;
  description: string;
  operationTypes?: UsageOperationType[];
  skip?: boolean;
  getDetailPath: (userWorkspaceId: string) => string;
  showAvatar?: boolean;
};

export const UsageByUserTableSection = ({
  title,
  description,
  operationTypes,
  skip,
  getDetailPath,
  showAvatar = false,
}: UsageByUserTableSectionProps) => {
  const { t } = useLingui();
  const theme = useTheme();
  const { formatUsageValue } = useUsageValueFormatter();
  const [searchTerm, setSearchTerm] = useState('');

  const { analytics, isInitialLoading, period, setPeriod, periodOptions } =
    useUsageAnalyticsData({
      operationTypes,
      skip,
    });

  if (isInitialLoading) {
    return <UsageSectionSkeleton />;
  }

  if (!analytics) {
    return null;
  }

  const usageByUser = analytics.usageByUser;

  if (usageByUser.length === 0) {
    return null;
  }

  const filteredUsers = usageByUser.filter((item) => {
    const search = normalizeSearchText(searchTerm);
    const name = normalizeSearchText(item.label ?? item.key);

    return name.includes(search);
  });

  return (
    <Section.Root>
      <Section.Header
        title={title}
        description={description}
        actions={
          <Select
            dropdownId={`${title.replace(/\s+/g, '-').toLowerCase()}-period`}
            value={period}
            options={periodOptions}
            onChange={setPeriod}
            needIconCheck
            selectSizeVariant="small"
          />
        }
      />
      <StyledSearchInputContainer>
        <SearchInput
          placeholder={t`Search for a user...`}
          value={searchTerm}
          onValueChange={setSearchTerm}
        />
      </StyledSearchInputContainer>
      <Table>
        <TableRow gridTemplateColumns={GRID_TEMPLATE_COLUMNS}>
          <TableHeader>{t`Name`}</TableHeader>
          <TableHeader align="right">{t`Usage`}</TableHeader>
          <TableHeader />
        </TableRow>
        {filteredUsers.map((item) => (
          <TableRow
            key={item.key}
            gridTemplateColumns={GRID_TEMPLATE_COLUMNS}
            to={getDetailPath(item.key)}
          >
            <TableCell
              color={themeCssVariables.font.color.primary}
              gap={showAvatar ? themeCssVariables.spacing[2] : undefined}
            >
              {showAvatar && (
                <Avatar
                  shape="circle"
                  size="md"
                  name={item.label ?? item.key}
                  colorSeed={item.key}
                />
              )}
              {item.label ?? item.key}
            </TableCell>
            <TableCell align="right">
              {formatUsageValue(item.workspaceCreditsUsed)}
            </TableCell>
            <TableCell align="center">
              <StyledIconChevronRightContainer>
                <IconChevronRight
                  size={theme.icon.size.md}
                  stroke={theme.icon.stroke.sm}
                />
              </StyledIconChevronRightContainer>
            </TableCell>
          </TableRow>
        ))}
      </Table>
    </Section.Root>
  );
};