'use client';
import { UAParser } from 'ua-parser-js';
import { api } from '~/trpc/react';
import { Separator } from '@usesend/ui/src/separator';
import { EmailStatusBadge, EmailStatusIcon } from './email-status-badge';
import { formatDate } from 'date-fns';
import { motion } from 'framer-motion';
import { EmailStatus } from '@prisma/client';
import { JsonValue } from '@prisma/client/runtime/library';
import {
SesBounce,
SesClick,
SesComplaint,
SesDeliveryDelay,
SesOpen,
} from '~/types/aws-types';
import {
BOUNCE_ERROR_MESSAGES,
COMPLAINT_ERROR_MESSAGES,
DELIVERY_DELAY_ERRORS,
} from '~/lib/constants/ses-errors';
import CancelEmail from './cancel-email';
import { useEffect } from 'react';
import { useState } from 'react';
export default function EmailDetails({ emailId }: { emailId: string }) {
const emailQuery = api.email.getEmail.useQuery({ id: emailId });
return (
{/*
From
{emailQuery.data?.from}
To
{emailQuery.data?.to}
Subject
{emailQuery.data?.subject}
*/}
{/*
{emailQuery.data?.to}
*/}
Subject: {emailQuery.data?.subject}
From: {emailQuery.data?.from}
{emailQuery.data?.latestStatus === 'SCHEDULED' &&
emailQuery.data?.scheduledAt ? (
<>
Scheduled at
{formatDate(
emailQuery.data?.scheduledAt,
"MMM dd'th', hh:mm a",
)}
>
) : null}
{emailQuery.data?.latestStatus !== 'SCHEDULED' ? (
Events History
{emailQuery.data?.emailEvents.map((evt) => (
{formatDate(evt.createdAt, 'MMM dd, hh:mm a')}
))}
) : null}
);
}
const EmailPreview = ({ html }: { html: string }) => {
const [show, setShow] = useState(false);
useEffect(() => {
const timer = setTimeout(() => {
setShow(true);
}, 200);
return () => clearTimeout(timer);
}, []);
if (!show) {
return (
);
}
return (
);
};
const EmailStatusText = ({
status,
data,
}: {
status: EmailStatus;
data: JsonValue;
}) => {
if (status === 'SENT') {
return (
We received your request and sent the email to recipient's server.
);
} else if (status === 'DELIVERED') {
return Mail is successfully delivered to the recipient.
;
} else if (status === 'DELIVERY_DELAYED') {
const _errorData = data as unknown as SesDeliveryDelay;
const errorMessage = DELIVERY_DELAY_ERRORS[_errorData.delayType];
return {errorMessage}
;
} else if (status === 'BOUNCED') {
const _errorData = data as unknown as SesBounce;
_errorData.bounceType;
return (
{getErrorMessage(_errorData)}
Type
{_errorData.bounceType}
Sub Type
{_errorData.bounceSubType}
SMTP response
{_errorData.bouncedRecipients[0]?.diagnosticCode}
);
} else if (status === 'FAILED') {
const _errorData = data as unknown as { error: string };
return {_errorData.error}
;
} else if (status === 'OPENED') {
const _data = data as unknown as SesOpen;
const userAgent = getUserAgent(_data.userAgent);
return (
{userAgent.os.name ? (
) : null}
{userAgent.browser.name ? (
Browser
{userAgent.browser.name}
) : null}
);
} else if (status === 'CLICKED') {
const _data = data as unknown as SesClick;
const userAgent = getUserAgent(_data.userAgent);
return (
{userAgent.os.name ? (
) : null}
{userAgent.browser.name ? (
Browser
{userAgent.browser.name}
) : null}
);
} else if (status === 'COMPLAINED') {
const _errorData = data as unknown as SesComplaint;
return (
{getComplaintMessage(_errorData.complaintFeedbackType)}
);
} else if (status === 'CANCELLED') {
return This scheduled email was cancelled
;
} else if (status === 'SUPPRESSED') {
return (
This email was suppressed because this email is previously either
bounced or the recipient complained.
);
}
return {status}
;
};
const getErrorMessage = (data: SesBounce) => {
if (data.bounceType === 'Permanent') {
return BOUNCE_ERROR_MESSAGES[data.bounceType][
data.bounceSubType as
| 'General'
| 'NoEmail'
| 'Suppressed'
| 'OnAccountSuppressionList'
];
} else if (data.bounceType === 'Transient') {
return BOUNCE_ERROR_MESSAGES[data.bounceType][
data.bounceSubType as
| 'General'
| 'MailboxFull'
| 'MessageTooLarge'
| 'ContentRejected'
| 'AttachmentRejected'
];
} else if (data.bounceType === 'Undetermined') {
return BOUNCE_ERROR_MESSAGES.Undetermined;
}
};
const getComplaintMessage = (errorType: string) => {
return COMPLAINT_ERROR_MESSAGES[
errorType as keyof typeof COMPLAINT_ERROR_MESSAGES
];
};
const getUserAgent = (userAgent: string) => {
const parser = new UAParser(userAgent);
return {
browser: parser.getBrowser(),
os: parser.getOS(),
device: parser.getDevice(),
};
};