Skip to content

The open alternative to Salesforce, designed for AI.

7.4 KB
Raw Blame
import { styled } from '@linaria/react';

import { ActivityRow } from '@/activities/components/ActivityRow';
import { EmailThreadNotShared } from '@/activities/emails/components/EmailThreadNotShared';
import { MessageChannelLogo } from '@/activities/emails/components/MessageChannelLogo';
import { getEmailParticipantAvatarColorSeed } from '@/activities/emails/utils/getEmailParticipantAvatarColorSeed';
import { getTimelineThreadLabel } from '@/activities/emails/utils/getTimelineThreadLabel';
import { isChatTimelineThreadChannel } from '@/activities/emails/utils/isChatTimelineThreadChannel';
import { useOpenRecordInSidePanel } from '@/side-panel/hooks/useOpenRecordInSidePanel';
import { CoreObjectNameSingular } from 'twenty-shared/types';
import { isDefined } from 'twenty-shared/utils';
import { Avatar, Tag } from 'twenty-ui/primitives/data-display';
import { useTheme, themeCssVariables } from 'twenty-ui/theme';
import {
  MessageChannelVisibility,
  type TimelineThread,
} from '~/generated/graphql';
import { formatToHumanReadableDate } from '~/utils/date-utils';
import { getAbsoluteImageUrl } from '~/utils/image/getAbsoluteImageUrl';
import { useLingui } from '@lingui/react/macro';

const StyledHeading = styled.div<{ unread: boolean }>`
  display: flex;
  max-width: 20%;
  overflow: hidden;
  width: fit-content;
`;

const StyledParticipantsContainer = styled.div`
  align-items: flex-start;
  display: flex;
`;

const StyledAvatarWrapper = styled.div`
  margin-left: calc(-1 * ${themeCssVariables.spacing[1]});
`;

const StyledSenderNames = styled.span`
  display: flex;
  margin: ${themeCssVariables.spacing[0]} ${themeCssVariables.spacing[1]};
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
`;

const StyledThreadCount = styled.span`
  color: ${themeCssVariables.font.color.tertiary};
`;

const StyledSubjectAndBody = styled.div`
  align-items: center;
  display: flex;
  flex: 1;
  gap: ${themeCssVariables.spacing[2]};
  overflow: hidden;
`;

const StyledSubject = styled.span`
  color: ${themeCssVariables.font.color.primary};
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
`;

const StyledBody = styled.span`
  color: ${themeCssVariables.font.color.tertiary};
  flex: 1;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
`;

const StyledReceivedAt = styled.div`
  font-size: ${themeCssVariables.font.size.sm};
  font-weight: ${themeCssVariables.font.weight.regular};
  padding: ${themeCssVariables.spacing[0]} ${themeCssVariables.spacing[1]};
`;

const StyledChannelLogo = styled.div`
  display: flex;
  flex-shrink: 0;
`;

type EmailThreadPreviewProps = {
  thread: TimelineThread;
  divider?: boolean;
};

type LastAvatar = {
  displayedName: string | undefined;
  avatarUrl: string | undefined;
  isCountIcon: boolean;
  placeholderColorSeed: string | undefined;
};

export const EmailThreadPreview = ({
  thread,
  divider = false,
}: EmailThreadPreviewProps) => {
  const { t } = useLingui();
  const theme = useTheme();
  const { openRecordInSidePanel } = useOpenRecordInSidePanel();

  const visibility = thread.visibility;
  const isChatThread = isChatTimelineThreadChannel(thread.channel);

  const senderNames =
    thread.firstParticipant.displayName +
    (thread?.lastTwoParticipants?.[0]?.displayName
      ? `, ${thread.lastTwoParticipants?.[0]?.displayName}`
      : '') +
    (thread?.lastTwoParticipants?.[1]?.displayName
      ? `, ${thread.lastTwoParticipants?.[1]?.displayName}`
      : '');

  const lastParticipant = thread?.lastTwoParticipants?.[1];

  const {
    displayedName,
    avatarUrl,
    isCountIcon,
    placeholderColorSeed,
  }: LastAvatar =
    thread.participantCount > 3
      ? {
          displayedName: `${thread.participantCount}`,
          avatarUrl: '',
          isCountIcon: true,
          placeholderColorSeed: undefined,
        }
      : {
          displayedName: lastParticipant?.displayName,
          avatarUrl: lastParticipant?.avatarUrl,
          isCountIcon: false,
          placeholderColorSeed: isDefined(lastParticipant)
            ? getEmailParticipantAvatarColorSeed(lastParticipant)
            : undefined,
        };

  const handleThreadClick = () => {
    const canOpen =
      thread.visibility === MessageChannelVisibility.SHARE_EVERYTHING;

    if (canOpen) {
      openRecordInSidePanel({
        recordId: thread.id,
        objectNameSingular: CoreObjectNameSingular.MessageThread,
      });
    }
  };

  const isDisabled = visibility !== MessageChannelVisibility.SHARE_EVERYTHING;
  const label = getTimelineThreadLabel(thread);
  const draftTag = thread.lastMessageIsDraft ? (
    <Tag color="orange">{t`Draft`}</Tag>
  ) : null;

  return (
    <ActivityRow
      onClick={handleThreadClick}
      disabled={isDisabled}
      divider={divider}
      label={label}
    >
      <StyledHeading unread={!thread.read}>
        <StyledParticipantsContainer>
          <Avatar
            src={getAbsoluteImageUrl(thread?.firstParticipant?.avatarUrl)}
            name={thread.firstParticipant.displayName}
            colorSeed={getEmailParticipantAvatarColorSeed(
              thread.firstParticipant,
            )}
            shape="circle"
          />
          {isDefined(thread?.lastTwoParticipants?.[0]) && (
            <StyledAvatarWrapper>
              <Avatar
                src={getAbsoluteImageUrl(
                  thread.lastTwoParticipants[0].avatarUrl,
                )}
                name={thread.lastTwoParticipants[0].displayName}
                colorSeed={getEmailParticipantAvatarColorSeed(
                  thread.lastTwoParticipants[0],
                )}
                shape="circle"
              />
            </StyledAvatarWrapper>
          )}
          {displayedName && (
            <StyledAvatarWrapper>
              <Avatar
                src={getAbsoluteImageUrl(avatarUrl)}
                name={displayedName}
                colorSeed={placeholderColorSeed}
                shape="circle"
                color={isCountIcon ? theme.grayScale.gray11 : undefined}
                backgroundColor={
                  isCountIcon ? theme.grayScale.gray2 : undefined
                }
              />
            </StyledAvatarWrapper>
          )}
        </StyledParticipantsContainer>

        <StyledSenderNames>{senderNames}</StyledSenderNames>
        {!isChatThread && (
          <StyledThreadCount>
            {thread.numberOfMessagesInThread}
          </StyledThreadCount>
        )}
      </StyledHeading>

      <StyledSubjectAndBody>
        {visibility === MessageChannelVisibility.METADATA && (
          <>
            {draftTag}
            <EmailThreadNotShared visibility={visibility} />
          </>
        )}
        {visibility === MessageChannelVisibility.SUBJECT && (
          <>
            {draftTag}
            {!isChatThread && <StyledSubject>{thread.subject}</StyledSubject>}
            <EmailThreadNotShared visibility={visibility} />
          </>
        )}
        {visibility === MessageChannelVisibility.SHARE_EVERYTHING && (
          <>
            {draftTag}
            {!isChatThread && <StyledSubject>{thread.subject}</StyledSubject>}
            <StyledBody>{thread.lastMessageBody}</StyledBody>
          </>
        )}
      </StyledSubjectAndBody>
      <StyledReceivedAt>
        {formatToHumanReadableDate(thread.lastMessageReceivedAt)}
      </StyledReceivedAt>
      <StyledChannelLogo>
        <MessageChannelLogo channel={thread.channel} />
      </StyledChannelLogo>
    </ActivityRow>
  );
};