Skip to content

Commit

Permalink
♻️ Remove use of context from email package (#981)
Browse files Browse the repository at this point in the history
  • Loading branch information
lukevella authored Jan 17, 2024
1 parent 668a4b5 commit e2c4cc7
Show file tree
Hide file tree
Showing 14 changed files with 59 additions and 44 deletions.
1 change: 1 addition & 0 deletions apps/web/src/utils/emails.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,5 +21,6 @@ export const emailClient = new EmailClient({
context: {
logoUrl: absoluteUrl("/logo.png"),
baseUrl: absoluteUrl(""),
domain: absoluteUrl("").replace(/(^\w+:|^)\/\//, ""),
},
});
16 changes: 8 additions & 8 deletions packages/emails/src/send-email.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,9 @@ type Templates = typeof templates;

type TemplateName = keyof typeof templates;

type TemplateProps<T extends TemplateName> = React.ComponentProps<
TemplateComponent<T>
type TemplateProps<T extends TemplateName> = Omit<
React.ComponentProps<TemplateComponent<T>>,
"ctx"
>;
type TemplateComponent<T extends TemplateName> = Templates[T];

Expand Down Expand Up @@ -79,12 +80,11 @@ export class EmailClient {
) {
const Template = templates[templateName] as TemplateComponent<T>;
const html = render(
<EmailContext.Provider value={this.config.context}>
<Template
// eslint-disable-next-line @typescript-eslint/no-explicit-any
{...(options.props as any)}
/>
</EmailContext.Provider>,
<Template
// eslint-disable-next-line @typescript-eslint/no-explicit-any
{...(options.props as any)}
ctx={this.config.context}
/>,
);

try {
Expand Down
16 changes: 1 addition & 15 deletions packages/emails/src/templates/components/email-context.tsx
Original file line number Diff line number Diff line change
@@ -1,19 +1,5 @@
import React from "react";

export type EmailContext = {
logoUrl: string;
baseUrl: string;
};

export const EmailContext = React.createContext<EmailContext>({
logoUrl: "/static/logo.png",
baseUrl: "",
});

export const useEmailContext = () => {
const context = React.useContext(EmailContext);
return {
...context,
domain: context.baseUrl.replace(/(^\w+:|^)\/\//, ""),
};
domain: string;
};
6 changes: 4 additions & 2 deletions packages/emails/src/templates/components/email-layout.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,14 @@ import {
Preview,
} from "@react-email/components";

import { useEmailContext } from "./email-context";
import { EmailContext } from "./email-context";
import { fontFamily, Section, Text } from "./styled-components";

export interface EmailLayoutProps {
preview: string;
recipientName: string;
footNote?: React.ReactNode;
ctx: EmailContext;
}

const containerStyles = {
Expand Down Expand Up @@ -42,8 +43,9 @@ export const EmailLayout = ({
recipientName = "Guest",
children,
footNote,
ctx,
}: React.PropsWithChildren<EmailLayoutProps>) => {
const { logoUrl, baseUrl } = useEmailContext();
const { logoUrl, baseUrl } = ctx;
return (
<Html>
<Head />
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { useEmailContext } from "./email-context";
import { EmailContext } from "./email-context";
import { EmailLayout } from "./email-layout";
import { Button, Link, Text } from "./styled-components";

Expand All @@ -7,6 +7,7 @@ export interface NotificationBaseProps {
title: string;
pollUrl: string;
disableNotificationsUrl: string;
ctx: EmailContext;
}

export interface NotificationEmailProps extends NotificationBaseProps {
Expand All @@ -19,10 +20,12 @@ export const NotificationEmail = ({
disableNotificationsUrl,
preview,
children,
ctx,
}: React.PropsWithChildren<NotificationEmailProps>) => {
const { domain } = useEmailContext();
const { domain } = ctx;
return (
<EmailLayout
ctx={ctx}
recipientName={name}
footNote={
<>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import {
TextProps,
} from "@react-email/components";

import { useEmailContext } from "./email-context";
import { EmailContext } from "./email-context";

export const borderColor = "#E2E8F0";
export const Text = (
Expand All @@ -32,8 +32,8 @@ export const Text = (
);
};

export const Domain = () => {
const { baseUrl, domain } = useEmailContext();
export const Domain = ({ ctx }: { ctx: EmailContext }) => {
const { baseUrl, domain } = ctx;
return <Link href={baseUrl}>{domain}</Link>;
};

Expand Down
5 changes: 4 additions & 1 deletion packages/emails/src/templates/finalized-host.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { Column, Row, Section } from "@react-email/components";

import { EmailContext } from "./components/email-context";
import { EmailLayout } from "./components/email-layout";
import { borderColor, Button, Text } from "./components/styled-components";

Expand All @@ -13,6 +14,7 @@ export interface FinalizeHostEmailProps {
location: string | null;
pollUrl: string;
attendees: string[];
ctx: EmailContext;
}

export const FinalizeHostEmail = ({
Expand All @@ -23,9 +25,10 @@ export const FinalizeHostEmail = ({
dow = "Fri",
date = "Friday, 12th June 2020",
time = "6:00 PM to 11:00 PM BST",
ctx,
}: FinalizeHostEmailProps) => {
return (
<EmailLayout recipientName={name} preview="Final date booked!">
<EmailLayout ctx={ctx} recipientName={name} preview="Final date booked!">
<Text>
<strong>{title}</strong> has been booked for:
</Text>
Expand Down
5 changes: 4 additions & 1 deletion packages/emails/src/templates/finalized-participant.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { Column, Row, Section } from "@react-email/components";

import { EmailContext } from "./components/email-context";
import { EmailLayout } from "./components/email-layout";
import { borderColor, Button, Text } from "./components/styled-components";

Expand All @@ -14,6 +15,7 @@ export interface FinalizeParticipantEmailProps {
location: string | null;
pollUrl: string;
attendees: string[];
ctx: EmailContext;
}

export const FinalizeParticipantEmail = ({
Expand All @@ -25,9 +27,10 @@ export const FinalizeParticipantEmail = ({
dow = "Fri",
date = "Friday, 12th June 2020",
time = "6:00 PM to 11:00 PM BST",
ctx,
}: FinalizeParticipantEmailProps) => {
return (
<EmailLayout recipientName={name} preview="Final date booked!">
<EmailLayout ctx={ctx} recipientName={name} preview="Final date booked!">
<Text>
<strong>{hostName}</strong> has booked <strong>{title}</strong> for the
following date:
Expand Down
12 changes: 7 additions & 5 deletions packages/emails/src/templates/login.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { useEmailContext } from "./components/email-context";
import { EmailContext } from "./components/email-context";
import { EmailLayout } from "./components/email-layout";
import {
Button,
Expand All @@ -13,21 +13,23 @@ interface LoginEmailProps {
name: string;
code: string;
magicLink: string;
ctx: EmailContext;
}

export const LoginEmail = ({
name = "Guest",
code = "123456",
magicLink = "https://rallly.co",
ctx,
}: LoginEmailProps) => {
const { domain } = useEmailContext();
return (
<EmailLayout
ctx={ctx}
footNote={
<>
You&apos;re receiving this email because a request was made to login
to <Domain />. If this wasn&apos;t you, let us know by replying to
this email.
to <Domain ctx={ctx} />. If this wasn&apos;t you, let us know by
replying to this email.
</>
}
recipientName={name}
Expand All @@ -40,7 +42,7 @@ export const LoginEmail = ({
<Heading>Option 1: Magic Link</Heading>
<Text>Click this magic link to log in on this device.</Text>
<Button href={magicLink} id="magicLink">
Log in to {domain}
Log in to {ctx.domain}
</Button>
<Text light={true}>This link will expire in 15 minutes.</Text>
</Card>
Expand Down
2 changes: 2 additions & 0 deletions packages/emails/src/templates/new-comment.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,11 @@ export const NewCommentEmail = ({
authorName = "Someone",
pollUrl = "https://rallly.co",
disableNotificationsUrl = "https://rallly.co",
ctx,
}: NewCommentEmailProps) => {
return (
<NotificationEmail
ctx={ctx}
name={name}
title={title}
pollUrl={pollUrl}
Expand Down
10 changes: 7 additions & 3 deletions packages/emails/src/templates/new-participant-confirmation.tsx
Original file line number Diff line number Diff line change
@@ -1,24 +1,28 @@
import { useEmailContext } from "./components/email-context";
import { EmailContext } from "./components/email-context";
import { EmailLayout } from "./components/email-layout";
import { Button, Domain, Section, Text } from "./components/styled-components";

interface NewParticipantConfirmationEmailProps {
name: string;
title: string;
editSubmissionUrl: string;
ctx: EmailContext;
}
export const NewParticipantConfirmationEmail = ({
title = "Untitled Poll",
name = "John",
editSubmissionUrl = "https://rallly.co",
ctx,
}: NewParticipantConfirmationEmailProps) => {
const { domain } = useEmailContext();
const { domain } = ctx;
return (
<EmailLayout
ctx={ctx}
footNote={
<>
You are receiving this email because a response was submitted on{" "}
<Domain />. If this wasn&apos;t you, please ignore this email.
<Domain ctx={ctx} />. If this wasn&apos;t you, please ignore this
email.
</>
}
recipientName={name}
Expand Down
2 changes: 2 additions & 0 deletions packages/emails/src/templates/new-participant.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,11 @@ export const NewParticipantEmail = ({
participantName = "Someone",
pollUrl = "https://rallly.co",
disableNotificationsUrl = "https://rallly.co",
ctx,
}: NewParticipantEmailProps) => {
return (
<NotificationEmail
ctx={ctx}
name={name}
title={title}
pollUrl={pollUrl}
Expand Down
7 changes: 5 additions & 2 deletions packages/emails/src/templates/new-poll.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { useEmailContext } from "./components/email-context";
import { EmailContext } from "./components/email-context";
import { EmailLayout } from "./components/email-layout";
import { Button, Card, Link, Text } from "./components/styled-components";

Expand All @@ -7,6 +7,7 @@ export interface NewPollEmailProps {
name: string;
adminLink: string;
participantLink: string;
ctx: EmailContext;
}

const ShareLink = ({
Expand Down Expand Up @@ -37,10 +38,12 @@ export const NewPollEmail = ({
name = "John",
adminLink = "https://rallly.co/admin/abcdefg123",
participantLink = "https://rallly.co/invite/wxyz9876",
ctx,
}: NewPollEmailProps) => {
const { baseUrl, domain } = useEmailContext();
const { baseUrl, domain } = ctx;
return (
<EmailLayout
ctx={ctx}
footNote={
<>
You are receiving this email because a new poll was created with this
Expand Down
8 changes: 6 additions & 2 deletions packages/emails/src/templates/register.tsx
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { EmailContext } from "./components/email-context";
import { EmailLayout } from "./components/email-layout";
import {
Domain,
Expand All @@ -9,19 +10,22 @@ import {
interface RegisterEmailProps {
name: string;
code: string;
ctx: EmailContext;
}

export const RegisterEmail = ({
name = "John",
code = "123456",
ctx,
}: RegisterEmailProps) => {
return (
<EmailLayout
ctx={ctx}
footNote={
<>
You&apos;re receiving this email because a request was made to
register an account on <Domain />. If this wasn&apos;t you, please
ignore this email.
register an account on <Domain ctx={ctx} />. If this wasn&apos;t you,
please ignore this email.
</>
}
recipientName={name}
Expand Down

1 comment on commit e2c4cc7

@vercel
Copy link

@vercel vercel bot commented on e2c4cc7 Jan 17, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Successfully deployed to the following URLs:

app – ./

app.rallly.co
app-git-main-rallly.vercel.app
app-rallly.vercel.app

Please sign in to comment.