All files / apps/web/src/email components.tsx

100% Statements 7/7
100% Branches 2/2
100% Functions 3/3
100% Lines 7/7

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61                  70x                   16x     70x   70x         10x                     70x                 12x                      
import React, { createContext, use } from "react";
 
import { Button as RawButton, Link as RawLink } from "@react-email/components";
import { omit } from "remeda";
 
import type { NavigationOptions, RouteTo } from "~app/utils/navigation";
import { cn } from "~components/utils";
import { buildUrl } from "~utils/server/url";
 
const buildAbsoluteUrl = <K extends RouteTo>(
	navigate: NavigationOptions<K>,
	baseUrl: string,
) => {
	/* c8 ignore start */
	if (!baseUrl) {
		throw new Error(`Expected to have base url while generating email`);
		/* c8 ignore stop */
	}
 
	return new URL(buildUrl(navigate), baseUrl).href;
};
 
export const BaseUrlContext = createContext<string | null>(null);
 
export const Button: React.FC<
	Omit<React.ComponentProps<typeof RawButton>, "href"> & {
		navigate: NavigationOptions<RouteTo>;
	}
> = ({ className, navigate, ...props }) => (
	<RawButton
		{...props}
		// oxlint-disable-next-line typescript/no-non-null-assertion
		href={buildAbsoluteUrl(navigate, use(BaseUrlContext)!)}
		className={cn(
			"border-brand bg-brand inline-block rounded-md border px-6 py-3 text-sm font-bold text-white capitalize no-underline",
			className,
		)}
	/>
);
 
export const Link: React.FC<
	Omit<React.ComponentProps<typeof RawLink>, "href"> &
		(
			| {
					navigate: NavigationOptions<RouteTo>;
			  }
			| { href: string }
		)
> = ({ className, ...props }) => (
	<RawLink
		{...omit(props, ["navigate", "href"] as unknown as (keyof typeof props)[])}
		className={cn("text-muted underline", className)}
		href={
			"href" in props
				? props.href
				: // oxlint-disable-next-line typescript/no-non-null-assertion
					buildAbsoluteUrl(props.navigate, use(BaseUrlContext)!)
		}
	/>
);