Skip to content

The open alternative to Salesforce, designed for AI.

3.5 KB
Raw Blame
import { AppChip } from '@/applications/components/AppChip';
import { currentWorkspaceMembersState } from '@/auth/states/currentWorkspaceMembersState';
import { type RoleWithPartialMembers } from '@/settings/roles/types/RoleWithPartialMembers';
import { TableCell } from '@/ui/layout/table/components/TableCell';
import { TableRow } from '@/ui/layout/table/components/TableRow';
import { styled } from '@linaria/react';
import { useAtomStateValue } from '@/ui/utilities/state/jotai/hooks/useAtomStateValue';
import { SettingsPath } from 'twenty-shared/types';
import { getSettingsPath } from 'twenty-shared/utils';
import { IconChevronRight, IconLock } from 'twenty-ui/icon';
import { useIcons } from 'twenty-ui/icon/provider';
import { useTheme, themeCssVariables } from 'twenty-ui/theme';
import { WorkspaceMemberAvatarStack } from '@/workspace-member/components/WorkspaceMemberAvatarStack';
import { useLingui } from '@lingui/react/macro';

const StyledNameCell = styled.div`
  color: ${themeCssVariables.font.color.primary};
  display: flex;
  gap: ${themeCssVariables.spacing[1]};
`;

const StyledAssignedToCell = styled.div`
  display: flex;
  justify-content: flex-end;
`;

const StyledIconLockContainer = styled.div`
  align-items: center;
  display: flex;
  justify-content: flex-end;
`;

const StyledTableRowContainer = styled.div`
  > * {
    &:hover {
      background: ${themeCssVariables.background.transparent.light};
      cursor: pointer;
    }
  }
`;

type SettingsRolesTableRowProps = {
  role: RoleWithPartialMembers;
};

const MAX_VISIBLE_ROLE_MEMBER_AVATARS = 5;

export const SettingsRolesTableRow = ({ role }: SettingsRolesTableRowProps) => {
  const { t } = useLingui();
  const theme = useTheme();
  const { getIcon } = useIcons();
  const Icon = getIcon(role.icon ?? 'IconUser');

  const currentWorkspaceMembers = useAtomStateValue(
    currentWorkspaceMembersState,
  );

  const enrichedWorkspaceMembers = role.workspaceMembers.map(
    (workspaceMember) =>
      currentWorkspaceMembers.find(
        (member) => member.id === workspaceMember.id,
      ) ?? workspaceMember,
  );

  return (
    <StyledTableRowContainer>
      <TableRow
        key={role.id}
        gridAutoColumns="332px 3fr 3fr 1fr"
        mobileGridAutoColumns="3fr 2fr 2fr 35px"
        to={getSettingsPath(SettingsPath.RoleDetail, { roleId: role.id })}
      >
        <TableCell>
          <StyledNameCell>
            <Icon size={theme.icon.size.md} stroke={theme.icon.stroke.sm} />
            {role.label}
            {!role.isEditable && (
              <StyledIconLockContainer>
                <IconLock
                  color={theme.font.color.light}
                  stroke={theme.icon.stroke.sm}
                  size={theme.icon.size.sm}
                />
              </StyledIconLockContainer>
            )}
          </StyledNameCell>
        </TableCell>
        <TableCell>
          <AppChip applicationId={role.applicationId} size="md" />
        </TableCell>
        <TableCell align="right">
          <StyledAssignedToCell>
            <WorkspaceMemberAvatarStack
              defaultAvatarName={t`Workspace member`}
              maxVisible={MAX_VISIBLE_ROLE_MEMBER_AVATARS}
              totalWorkspaceMembersCount={role.workspaceMembers.length}
              workspaceMembers={enrichedWorkspaceMembers}
            />
          </StyledAssignedToCell>
        </TableCell>
        <TableCell align="right" color={theme.font.color.tertiary}>
          <IconChevronRight size={theme.icon.size.md} />
        </TableCell>
      </TableRow>
    </StyledTableRowContainer>
  );
};