Skip to content

A one-of-a-kind resume builder that keeps your privacy in mind. Completely secure, customizable, portable, open-source and free forever. Try it out today!

35.1 KB
Raw Blame
import type { ApplicationSummary } from "@/features/applications/types";
import type { LetterDraft } from "@/features/letters/store";
import type { CoverLetter } from "@reactive-resume/schema/cover-letter/data";
import type { CSSProperties, ReactElement, ReactNode } from "react";
import { i18n } from "@lingui/core";
import { t } from "@lingui/core/macro";
import { useLingui } from "@lingui/react";
import { Plural, Trans } from "@lingui/react/macro";
import { ORPCError } from "@orpc/client";
import { useQuery } from "@tanstack/react-query";
import { useReducedMotion } from "motion/react";
import { useId, useRef, useState } from "react";
import { aiModelForTask } from "@reactive-resume/ai/task-models";
import { coverLetterTextToHtml, defaultCoverLetterSignOff, greetingName } from "@reactive-resume/resume/cover-letter";
import { Button } from "@reactive-resume/ui/components/button";
import {
	DropdownMenu,
	DropdownMenuContent,
	DropdownMenuItem,
	DropdownMenuSeparator,
	DropdownMenuTrigger,
} from "@reactive-resume/ui/components/dropdown-menu";
import { Icon } from "@reactive-resume/ui/components/icon";
import { Input } from "@reactive-resume/ui/components/input";
import { Label } from "@reactive-resume/ui/components/label";
import { NativeSelect } from "@reactive-resume/ui/components/native-select";
import { Switch, SwitchRow } from "@reactive-resume/ui/components/switch";
import { Textarea } from "@reactive-resume/ui/components/textarea";
import { toast } from "@reactive-resume/ui/components/toast";
import { cn } from "@reactive-resume/utils/style";
import { LetterLanguageField } from "./design-panel";
import { templates } from "@/dialogs/resume/template/data";
import { AiResultUsage } from "@/features/ai/usage";
import { getDisplayLane, getLaneLabel } from "@/features/applications/lanes";
import { applicationsListQueryOptions } from "@/features/applications/queries";
import { useApplicationLanes } from "@/features/applications/use-application-lanes";
import { SetUpAi } from "@/features/assistant/provider-setup";
import { useLetterWords } from "@/features/letters/compose";
import { countWords, LETTER_LENGTH, letterLength } from "@/features/letters/length";
import {
	discardLetterDraft,
	dismissLetterDraftFailure,
	startLetterDraft,
	stopLetterDraft,
	useLetterEditorStore,
} from "@/features/letters/store";
import { useLetterMode } from "@/features/letters/use-letter-mode";
import { ACCENTS, FONT_PAIRINGS, matchFontPairing, rgbaToHex } from "@/features/resume/editor/design/presets";
import { useEditorStore } from "@/features/resume/editor/store";
import { CustomFields } from "@/features/resume/editor/write/basics-card";
import { TextField, validateEmail, WebsiteField } from "@/features/resume/editor/write/fields";
import { RichTextEditor } from "@/features/resume/editor/write/rich-text-editor";
import { AiProviderLoadState } from "@/features/settings/integrations/ai-provider-load-state";
import { useHasUsableAiProvider } from "@/features/settings/integrations/hooks/use-has-usable-ai-provider";
import { getAccountGeneration } from "@/libs/auth/account-scope";
import { getOrpcErrorMessage } from "@/libs/error-message";
import { ENTER_CLASS } from "@/libs/motion";
import { client, orpc } from "@/libs/orpc/client";

const BODY_ID = "letter-body-editor";

const focusBody = () => window.setTimeout(() => document.getElementById(BODY_ID)?.focus(), 50);

const failed = (error: unknown) =>
	toast.add({
		type: "error",
		description: getOrpcErrorMessage(error, { fallback: t`Couldn't save that change. Try again.` }),
	});

/** Saves what's typed, then changes the letter on the server (links, the application, the resume). */
const updateLetter = (changes: Omit<Parameters<typeof client.coverLetters.update>[0], "id" | "expectedRevision">) =>
	useLetterEditorStore.getState().change((letter) =>
		client.coverLetters.update({
			id: letter.id,
			expectedRevision: letter.revision,
			sessionId: useLetterEditorStore.getState().sessionId,
			...changes,
		}),
	);

type SectionProps = { id: string; title: ReactNode; aside?: ReactNode; children: ReactNode };

function Section({ id, title, aside, children }: SectionProps) {
	return (
		<section aria-labelledby={id} className="grid gap-3 px-4 py-5">
			<div className="flex items-center justify-between gap-3">
				{/* A section heading, a level above the 12px field labels under it (the resume builder's card titles). */}
				<h2 id={id} className="text-[15px] font-semibold text-ink">
					{title}
				</h2>
				{aside}
			</div>
			{children}
		</section>
	);
}

/**
 * Write: who the letter is for (the application), to (recipient, company, date) and from (the resume, live or
 * copied), then the body with its length guide and a note on the design. The page beside it updates as you type.
 */
export function LetterWritePanel() {
	const letter = useLetterEditorStore((state) => state.letter);
	const draft = useLetterEditorStore((state) => state.draft);
	const status = useLetterEditorStore((state) => state.status);
	const { data: applications } = useQuery(applicationsListQueryOptions());
	// While History shows an older version on the page, Write would edit the current letter beside it: hold it.
	const viewingVersion = useEditorStore((state) => state.historyVersionId !== null);
	if (!letter) return null;

	const application = applications?.find((item) => item.id === letter.sourceApplicationId) ?? null;
	// While the letter changed elsewhere, typing would only pile onto edits that can't save. Reload first.
	const conflict = status === "conflict";
	const disabled = letter.isLocked || conflict || viewingVersion;

	return (
		<div className="divide-y divide-line">
			{viewingVersion && !letter.isLocked && (
				<p className="flex items-center gap-2 bg-info-soft px-4 py-2.5 text-sm text-info-text">
					<Icon name="clock-counter-clockwise" size={18} />
					<Trans>You're viewing an earlier version. Go back to now in History to edit.</Trans>
				</p>
			)}
			{letter.isLocked && (
				<p className="flex items-center gap-2 bg-sunken px-4 py-2.5 text-sm text-ink-2">
					<Icon name="lock" size={18} />
					<Trans>This letter is locked. Unlock it from its menu to edit.</Trans>
				</p>
			)}
			{conflict && (
				<p className="flex items-center gap-2 bg-warn-soft px-4 py-2.5 text-sm text-warn-text">
					<Icon name="cloud-warning" size={18} />
					<Trans>This letter changed in another tab. Reload above to keep editing; your edits stay in a copy.</Trans>
				</p>
			)}
			<ForSection letter={letter} application={application} applications={applications ?? []} disabled={disabled} />
			<ToSection letter={letter} application={application} disabled={disabled} />
			<FromSection letter={letter} disabled={disabled} />
			<BodySection letter={letter} application={application} disabled={disabled} />
			{/* While a draft shows, the length is the draft's. */}
			<LengthSection content={draft.phase === "streaming" || draft.phase === "ready" ? draft.text : letter.content} />
			<DesignNote letter={letter} />
		</div>
	);
}

/** Today's date in the person's own time zone, as `YYYY-MM-DD`. */
const todayDate = () => new Date().toLocaleDateString("en-CA");

type ForSectionProps = {
	letter: CoverLetter;
	application: ApplicationSummary | null;
	applications: ApplicationSummary[];
	disabled: boolean;
};

/** FOR: the application the letter is for. Linking one fills the recipient; without one, drafting uses the resume. */
function ForSection({ letter, application, applications, disabled }: ForSectionProps) {
	const { lanes } = useApplicationLanes();
	const lane = application ? getDisplayLane(lanes, application) : null;
	const submitted = Boolean(application?.sentCoverLetterVersionId && application.coverLetterId === letter.id);
	const choices = applications.filter((item) => item.status !== "closed" || item.id === application?.id);

	const choose = (next: ApplicationSummary | null) =>
		void updateLetter({
			applicationId: next?.id ?? null,
			// The recipient comes from the application: its company, and its first contact when it has one.
			...(next && letter.layout === "structured"
				? { recipientCompany: next.company, recipientName: next.contacts[0]?.name ?? "" }
				: {}),
		}).catch(failed);

	const menu = (trigger: ReactElement) => (
		<DropdownMenu>
			<DropdownMenuTrigger disabled={disabled || submitted} render={trigger} />
			<DropdownMenuContent align="end" className="max-h-80 w-72 overflow-y-auto">
				{choices.map((item) => (
					<DropdownMenuItem
						key={item.id}
						disabled={Boolean(item.sentCoverLetterVersionId && item.coverLetterId !== letter.id)}
						className="data-disabled:opacity-60"
						onClick={() => choose(item)}
					>
						<Icon name="briefcase" />
						<span className="grid min-w-0">
							<span className="truncate">{item.role}</span>
							<span className="truncate text-xs text-ink-3">{item.company}</span>
							{item.sentCoverLetterVersionId && item.coverLetterId !== letter.id && (
								<span className="text-xs text-ink-3">
									<Trans>Letter already sent</Trans>
								</span>
							)}
						</span>
					</DropdownMenuItem>
				))}
				{choices.length === 0 && (
					<p className="px-2 py-1.5 text-sm text-ink-2">
						<Trans>No applications yet. Add one in Applications first.</Trans>
					</p>
				)}
				{application && (
					<>
						<DropdownMenuSeparator />
						<DropdownMenuItem onClick={() => choose(null)}>
							<Icon name="link-simple-horizontal-break" />
							<Trans>No application</Trans>
						</DropdownMenuItem>
					</>
				)}
			</DropdownMenuContent>
		</DropdownMenu>
	);

	return (
		<Section id="letter-for" title={<Trans>For</Trans>}>
			{submitted && (
				<p className="text-xs text-ink-3">
					<Trans>This letter is recorded as sent. Its application link stays with that submission.</Trans>
				</p>
			)}
			{application ? (
				<div className="flex items-center gap-3 rounded-xl border border-line p-3">
					<span
						aria-hidden="true"
						className="grid size-9 shrink-0 place-items-center rounded-lg bg-sunken font-semibold text-ink-2"
					>
						{application.company.charAt(0).toUpperCase()}
					</span>
					<span className="grid min-w-0 flex-1">
						<span className="truncate text-sm font-medium">{application.role}</span>
						<span className="flex min-w-0 items-center gap-1.5 text-xs text-ink-3">
							<span className="truncate">{application.company}</span>
							<span aria-hidden="true">·</span>
							<span aria-hidden="true" className="size-1.5 shrink-0 rounded-sm" style={{ background: lane?.color }} />
							<span className="truncate">{lane && getLaneLabel(lane)}</span>
						</span>
					</span>
					{menu(
						<Button size="sm" variant="secondary">
							<Trans>Change</Trans>
						</Button>,
					)}
				</div>
			) : (
				<>
					{menu(
						<Button variant="secondary" className="w-full justify-start gap-2">
							<Icon name="briefcase" />
							<Trans>Link an application</Trans>
							<Icon name="caret-down" className="ms-auto text-ink-3" />
						</Button>,
					)}
					<p className="text-xs text-ink-3">
						<Trans>Without one, you fill in the recipient yourself and drafting uses only your resume.</Trans>
					</p>
				</>
			)}
		</Section>
	);
}

type LetterSectionProps = { letter: CoverLetter; application: ApplicationSummary | null; disabled: boolean };

/** TO: name or team, company and date for structured letters; older (freeform) letters keep their recipient block. */
function ToSection({ letter, application, disabled }: LetterSectionProps) {
	const id = useId();
	const edit = useLetterEditorStore((state) => state.edit);

	if (letter.layout === "freeform") {
		return (
			<Section id="letter-to" title={<Trans>To</Trans>}>
				<RichTextEditor
					label={t`Recipient`}
					value={letter.recipient}
					disabled={disabled}
					heightClassName="min-h-[64px] max-h-[200px]"
					onChange={(recipient) => edit({ recipient })}
				/>
				<p className="text-xs text-ink-3">
					<Trans>This letter keeps its recipient as written.</Trans>
				</p>
			</Section>
		);
	}

	return (
		<Section id="letter-to" title={<Trans>To</Trans>}>
			<div className="grid grid-cols-2 gap-3">
				<div className="col-span-2 grid gap-1.5">
					<Label htmlFor={`${id}-name`}>
						<Trans>Name or team</Trans>
					</Label>
					<Input
						id={`${id}-name`}
						value={letter.recipientName}
						maxLength={200}
						placeholder={t`Hiring team`}
						disabled={disabled}
						onChange={(event) => edit({ recipientName: event.target.value })}
					/>
				</div>
				<div className="grid gap-1.5">
					<Label htmlFor={`${id}-company`}>
						<Trans>Company</Trans>
					</Label>
					<Input
						id={`${id}-company`}
						value={letter.recipientCompany}
						maxLength={200}
						disabled={disabled}
						onChange={(event) => edit({ recipientCompany: event.target.value })}
					/>
				</div>
				<div className="grid gap-1.5">
					<Label htmlFor={`${id}-date`}>
						<Trans>Date</Trans>
					</Label>
					<div className="flex gap-1.5">
						<Input
							id={`${id}-date`}
							type="date"
							value={letter.letterDate ?? ""}
							disabled={disabled}
							onChange={(event) => edit({ letterDate: event.target.value || null })}
						/>
						{/* A letter reused later should carry the day it's sent. */}
						{letter.letterDate !== todayDate() && (
							<Button
								variant="ghost"
								size="sm"
								className="h-9 shrink-0"
								disabled={disabled}
								onClick={() => edit({ letterDate: todayDate() })}
							>
								<Trans>Today</Trans>
							</Button>
						)}
					</div>
				</div>
			</div>
			<p className="text-xs text-ink-3">
				{application ? (
					<Trans>Filled from the application. The greeting follows the name.</Trans>
				) : (
					<Trans>The greeting follows the name.</Trans>
				)}
			</p>
		</Section>
	);
}

/** FROM: the resume the sender's details come from, and whether they stay live or are copied into the letter. */
type FromSectionProps = { letter: CoverLetter; disabled: boolean };

function FromSection({ letter, disabled: readOnly }: FromSectionProps) {
	const disabled = useLetterEditorStore((state) => state.changing) || readOnly;
	const edit = useLetterEditorStore((state) => state.edit);
	const id = useId();
	const { data: resumes } = useQuery(orpc.resume.list.queryOptions({ input: {} }));
	const resume = resumes?.find((item) => item.id === letter.sourceResumeId);

	const setResume = (resumeId: string | null) =>
		void updateLetter({
			resumeId,
			// A letter taking a resume for the first time takes its details and design with it.
			...(resumeId && !letter.sourceResumeId ? { senderLinked: true, designLinked: true } : {}),
		}).catch(failed);

	const setLinked = (senderLinked: boolean) =>
		void updateLetter({ senderLinked })
			.then(() =>
				toast.add({
					description: senderLinked ? t`Linked to the resume again` : t`Sender details copied into the letter`,
				}),
			)
			.catch(failed);

	return (
		<Section id="letter-from" title={<Trans>From</Trans>}>
			<div className="grid gap-1.5">
				<Label htmlFor={`${id}-resume`}>
					<Trans>Resume</Trans>
				</Label>
				<NativeSelect
					id={`${id}-resume`}
					value={letter.sourceResumeId ?? ""}
					disabled={disabled}
					onChange={(event) => setResume(event.target.value || null)}
				>
					<option value="">{t`No resume`}</option>
					{resumes?.map((item) => (
						<option key={item.id} value={item.id}>
							{item.name}
						</option>
					))}
				</NativeSelect>
			</div>
			{letter.sourceResumeId ? (
				<SwitchRow
					label={t`Use details from “${resume?.name ?? t`the resume`}”`}
					description={
						letter.senderLinked
							? t`Linked. The header (name, contact details, links and photo) updates when the resume does.`
							: t`Off. Edit this letter’s details below. Turning this on replaces them with the resume’s current details.`
					}
					checked={letter.senderLinked}
					disabled={disabled}
					onCheckedChange={setLinked}
				/>
			) : (
				<p className="text-xs text-ink-3">
					<Trans>Enter your details below. Choosing a resume replaces them and links its design.</Trans>
				</p>
			)}
			{(!letter.sourceResumeId || !letter.senderLinked) && (
				<fieldset disabled={disabled} className="grid min-w-0 grid-cols-2 gap-3">
					<SenderPhoto picture={letter.style.picture} disabled={disabled} />
					<TextField
						label={<Trans>Full name</Trans>}
						wide
						autoComplete="name"
						value={letter.style.basics.name}
						onChange={(name) => edit({ sender: { name } })}
					/>
					<TextField
						label={<Trans>Headline</Trans>}
						wide
						value={letter.style.basics.headline}
						onChange={(headline) => edit({ sender: { headline } })}
					/>
					<TextField
						label={<Trans>Email</Trans>}
						wide
						type="email"
						autoComplete="email"
						validate={validateEmail}
						value={letter.style.basics.email}
						onChange={(email) => edit({ sender: { email } })}
					/>
					<TextField
						label={<Trans>Phone</Trans>}
						type="tel"
						autoComplete="tel"
						value={letter.style.basics.phone}
						onChange={(phone) => edit({ sender: { phone } })}
					/>
					<TextField
						label={<Trans>Location</Trans>}
						value={letter.style.basics.location}
						onChange={(location) => edit({ sender: { location } })}
					/>
					<WebsiteField
						label={<Trans>Website</Trans>}
						value={letter.style.basics.website}
						allowInlineLink={false}
						onChange={(website) => edit({ sender: { website: { url: website.url, label: website.label } } })}
					/>
					<CustomFields
						fields={letter.style.basics.customFields}
						locked={disabled}
						edit={(_key, mutate) => {
							// The latest list, so quick edits in a row build on each other.
							const fields = useLetterEditorStore.getState().letter?.style.basics.customFields ?? [];
							const customFields = structuredClone(fields);
							mutate(customFields);
							edit({ sender: { customFields } });
						}}
					/>
				</fieldset>
			)}
		</Section>
	);
}

type SenderPhotoProps = { picture: CoverLetter["style"]["picture"]; disabled: boolean };

/** The photo the letter copied from its resume, which it can leave off. Uploading one is the resume's job. */
function SenderPhoto({ picture, disabled }: SenderPhotoProps) {
	const edit = useLetterEditorStore((state) => state.edit);
	if (!picture.url) return null;

	return (
		<div className="col-span-full flex items-center gap-3 rounded-lg bg-bg p-2.5">
			<img src={picture.url} alt="" className="size-10 shrink-0 rounded-md object-cover" />
			<p className="min-w-0 flex-1 text-xs leading-4 text-ink-2">
				{picture.hidden ? <Trans>Photo hidden from the page.</Trans> : <Trans>Photo on the page.</Trans>}
			</p>
			<Switch
				aria-label={t`Show photo`}
				checked={!picture.hidden}
				disabled={disabled}
				onCheckedChange={(shown) => edit({ sender: { picture: { hidden: !shown } } })}
			/>
		</div>
	);
}

/**
 * The body, edited here while the page updates. Empty, it offers a draft from the posting and the resume. A draft
 * shows on a green wash until it's kept: it never replaces typed text by itself.
 */
function BodySection({ letter, application, disabled }: LetterSectionProps) {
	const account = getAccountGeneration();
	const sessionId = useLetterEditorStore.getState().sessionId;
	const words = useLetterWords(letter.style.metadata.page.locale);
	const edit = useLetterEditorStore((state) => state.edit);
	const draft = useLetterEditorStore((state) => state.draft);
	const [writing, setWriting] = useState(false);
	const structured = letter.layout === "structured";
	const name = greetingName(letter.recipientName);
	const showEditor = writing || countWords(letter.content) > 0;
	const languageId = useId();

	return (
		<Section id="letter-body" title={<Trans>Letter</Trans>}>
			{/* The greeting and sign-off are written in the letter's language, so it's chosen with the words. */}
			<div className="grid gap-1.5">
				<Label htmlFor={languageId}>
					<Trans>Language</Trans>
				</Label>
				<LetterLanguageField id={languageId} disabled={disabled} />
			</div>
			{structured && (
				<LetterFramingField
					letter={letter}
					field="greeting"
					defaultValue={name ? words.greeting(name) : words.teamGreeting}
					disabled={disabled}
				/>
			)}

			{draft.phase !== "idle" ? (
				<DraftBox
					draft={draft}
					letter={letter}
					application={application}
					onKeep={(text, aiOperationIds) => {
						const current = useLetterEditorStore.getState();
						if (
							getAccountGeneration() !== account ||
							current.sessionId !== sessionId ||
							current.letter?.id !== letter.id ||
							current.letter.revision !== letter.revision ||
							draft.phase !== "ready" ||
							current.draft !== draft ||
							current.letter.isLocked
						)
							return;
						edit({ content: coverLetterTextToHtml(text), aiOperationIds });
						discardLetterDraft();
						setWriting(true);
						toast.add({ description: t`Draft kept. Edit anything you like.` });
						focusBody();
					}}
				/>
			) : showEditor ? (
				<RichTextEditor
					id={BODY_ID}
					label={t`Letter body`}
					value={letter.content}
					disabled={disabled}
					heightClassName="min-h-[240px]"
					improve={{ letter: true, applicationId: application?.id }}
					onChange={(content, options) => {
						const current = useLetterEditorStore.getState();
						if (
							getAccountGeneration() !== account ||
							current.sessionId !== sessionId ||
							current.letter?.id !== letter.id ||
							current.letter.isLocked
						)
							return;
						edit({
							content,
							...(options?.aiOperationIds?.length || options?.removedAiOperationIds?.length
								? {
										aiOperationIds: [
											...new Set([...(current.letter.aiOperationIds ?? []), ...(options.aiOperationIds ?? [])]),
										].filter((id) => !options.removedAiOperationIds?.includes(id)),
									}
								: {}),
						});
					}}
				/>
			) : (
				<EmptyBody
					letter={letter}
					application={application}
					disabled={disabled}
					onWrite={() => {
						setWriting(true);
						focusBody();
					}}
				/>
			)}

			{(letter.aiOperationIds?.length ?? 0) > 0 && <AiResultUsage usage={letter.aiUsage} />}

			{structured && (
				<>
					<LetterFramingField
						letter={letter}
						field="signOff"
						defaultValue={defaultCoverLetterSignOff(letter, words)}
						disabled={disabled}
					/>
					<p className="text-xs text-ink-3">
						<Trans>
							Clear the greeting or sign-off to omit it. Clearing the sign-off also removes the automatic signature.
						</Trans>
					</p>
				</>
			)}
		</Section>
	);
}

type LetterFramingFieldProps = {
	letter: CoverLetter;
	field: "greeting" | "signOff";
	defaultValue: string;
	disabled: boolean;
};

function LetterFramingField({ letter, field, defaultValue, disabled }: LetterFramingFieldProps) {
	const id = useId();
	const edit = useLetterEditorStore((state) => state.edit);
	return (
		<div className="grid gap-1.5">
			<div className="flex items-center justify-between gap-2">
				<Label htmlFor={id}>{field === "greeting" ? <Trans>Greeting</Trans> : <Trans>Sign-off</Trans>}</Label>
				{letter[field] !== null && (
					<Button variant="ghost" size="sm" disabled={disabled} onClick={() => edit({ [field]: null })}>
						<Trans>Use default</Trans>
					</Button>
				)}
			</div>
			<Textarea
				id={id}
				value={letter[field] ?? defaultValue}
				maxLength={2_000}
				disabled={disabled}
				onChange={(event) => edit({ [field]: event.target.value })}
			/>
		</div>
	);
}

type EmptyBodyProps = {
	letter: CoverLetter;
	application: ApplicationSummary | null;
	disabled: boolean;
	onWrite: () => void;
};

function EmptyBody({ letter, application, disabled, onWrite }: EmptyBodyProps) {
	const providerState = useHasUsableAiProvider();
	const { hasUsableProvider } = providerState;
	const draftProvider = providerState.usableProviders[0];
	const draft = draftProvider
		? { label: draftProvider.label, model: aiModelForTask(draftProvider, "capable") }
		: undefined;
	const hasResume = Boolean(letter.sourceResumeId);
	const company = application?.company;
	const canDraft = hasResume || Boolean(application);

	return (
		<div className="grid justify-items-start gap-3 rounded-xl border border-dashed border-line-2 p-4">
			<p className="text-sm text-ink-2">
				{/* Without a provider, drafting isn't on offer yet: say what would make it so. */}
				{canDraft && !providerState.isUnavailable && !hasUsableProvider ? (
					company && hasResume ? (
						<Trans>Start typing, or connect an AI provider to draft from the {company} posting and your resume.</Trans>
					) : company ? (
						<Trans>Start typing, or connect an AI provider to draft from the {company} posting.</Trans>
					) : (
						<Trans>Start typing, or connect an AI provider to draft from your resume.</Trans>
					)
				) : company && hasResume ? (
					<Trans>Start typing, or draft from what we know: the {company} posting and your resume.</Trans>
				) : company ? (
					<Trans>Start typing, or draft from what we know: the {company} posting.</Trans>
				) : hasResume ? (
					<Trans>Start typing, or draft from what we know: your resume.</Trans>
				) : (
					<Trans>Start typing. To draft one, link a resume or an application first.</Trans>
				)}
			</p>
			<div className="flex flex-wrap gap-1.5">
				{canDraft && hasUsableProvider && (
					<Button
						size="sm"
						disabled={disabled}
						className="bg-accent-soft text-accent-text hover:bg-accent-soft hover:brightness-95"
						onClick={() => void startLetterDraft("draft")}
					>
						<Icon name="sparkle" size={16} />
						{application ? <Trans>Draft from the posting</Trans> : <Trans>Draft from your resume</Trans>}
					</Button>
				)}
				<Button size="sm" variant="ghost" disabled={disabled} onClick={onWrite}>
					<Trans>Write it myself</Trans>
				</Button>
			</div>
			{canDraft && hasUsableProvider && (
				<p className="text-xs text-ink-3">
					{application && hasResume
						? t`Sends this letter's resume and the ${application.company} posting to ${providerState.usableProviders[0]?.label ?? t`your AI provider`} to draft the body.`
						: application
							? t`Sends the ${application.company} posting to ${providerState.usableProviders[0]?.label ?? t`your AI provider`} to draft the body.`
							: t`Sends this letter's resume to ${providerState.usableProviders[0]?.label ?? t`your AI provider`} to draft the body.`}
				</p>
			)}
			{canDraft && <AiProviderLoadState state={providerState} />}
			{canDraft && hasUsableProvider && (
				<p className="text-xs text-ink-3">
					<Trans>
						Sends the linked resume, posting and letter details to {draft?.label} · {draft?.model}. Your provider may
						charge.
					</Trans>
				</p>
			)}
			{canDraft && !providerState.isUnavailable && !hasUsableProvider && (
				<p className="text-xs text-ink-3">
					<Trans>Drafting needs an AI provider.</Trans> <SetUpAi />
				</p>
			)}
		</div>
	);
}

type DraftBoxProps = {
	draft: Exclude<LetterDraft, { phase: "idle" }>;
	letter: CoverLetter;
	application: ApplicationSummary | null;
	onKeep: (text: string, aiOperationIds: string[]) => void;
};

/** The draft on its green wash, then the dark chip: Keep, Shorter, More personal, Discard. */
function DraftBox({ draft, letter, application, onKeep }: DraftBoxProps) {
	const reducedMotion = useReducedMotion();
	const [pick, setPick] = useState<{ draft: LetterDraft; index: number } | null>(null);
	// Retrying repeats the last request.
	const last = useRef<{
		variant: "draft" | "shorter" | "personal";
		previous?: string;
		previousAiOperationIds?: string[];
	}>({ variant: "draft" });
	const run = (variant: "draft" | "shorter" | "personal", previous?: string, previousAiOperationIds: string[] = []) => {
		last.current = previous ? { variant, previous, previousAiOperationIds } : { variant };
		void startLetterDraft(variant, previous, previousAiOperationIds);
	};

	if (draft.phase === "failed") {
		const gateway = draft.error instanceof ORPCError && draft.error.code === "BAD_GATEWAY";
		return (
			<div role="alert" className="grid gap-2.5 rounded-xl bg-danger-soft p-3 text-[13px] text-danger-text">
				<span className="flex gap-2">
					<Icon name="warning-circle" size={18} className="shrink-0" />
					{gateway && draft.provider ? (
						<Trans>Drafting stopped: {draft.provider} didn't respond. Nothing on the page changed.</Trans>
					) : (
						<Trans>Drafting stopped. Nothing on the page changed.</Trans>
					)}
				</span>
				<div className="flex gap-1.5">
					<Button
						size="sm"
						variant="secondary"
						onClick={() => run(last.current.variant, last.current.previous, last.current.previousAiOperationIds)}
					>
						<Trans>Try again</Trans>
					</Button>
					<Button size="sm" variant="ghost" onClick={dismissLetterDraftFailure}>
						<Trans>Dismiss</Trans>
					</Button>
				</div>
			</div>
		);
	}

	const streaming = draft.phase === "streaming";
	// Earlier versions stay side by side with the newest: "2 of 3", and Accept or a revision uses the one shown.
	const currentVersion = { text: draft.text, aiOperationIds: draft.aiOperationIds ?? [] };
	const all = streaming ? [currentVersion] : [...draft.versions, currentVersion];
	const index = pick?.draft === draft ? Math.min(pick.index, all.length - 1) : all.length - 1;
	const shown = all[index] ?? currentVersion;
	// Reduced motion shows the whole draft at once.
	const text = streaming && reducedMotion ? "" : shown.text;
	const sources =
		letter.sourceResumeId && application
			? t`Draft · uses only your resume and the posting`
			: application
				? t`Draft · uses only the posting`
				: t`Draft · uses only your resume`;

	return (
		<div className="grid gap-2">
			<div
				aria-live="polite"
				aria-busy={streaming}
				className={cn(
					"min-h-[120px] rounded-lg bg-accent-soft px-3 py-2.5 text-sm leading-relaxed whitespace-pre-wrap outline-[1.5px] transition-[outline-color] duration-standard outline-solid",
					draft.phase === "ready" ? "outline-accent" : "outline-transparent",
				)}
			>
				{text}
				{streaming && (
					<span className="text-ink-3">
						{reducedMotion ? <Trans>Drafting…</Trans> : <span aria-hidden="true">▍</span>}
					</span>
				)}
			</div>
			{streaming && (
				<Button size="sm" variant="ghost" className="w-fit" onClick={stopLetterDraft}>
					<Trans>Stop</Trans>
				</Button>
			)}
			{draft.phase === "ready" && draft.stopped && index === all.length - 1 && (
				<p className="text-xs text-ink-3">
					<Trans>Stopped before the end. Accept it and finish it yourself, or reject it.</Trans>
				</p>
			)}
			{draft.phase === "ready" && all.length > 1 && (
				<div className="flex items-center gap-1 text-xs text-ink-2">
					<Button
						size="icon-xs"
						variant="ghost"
						aria-label={t`Previous version`}
						disabled={index === 0}
						onClick={() => setPick({ draft, index: index - 1 })}
					>
						<Icon name="caret-left" size={16} />
					</Button>
					<span className="font-mono tabular-nums">
						<Trans>
							{i18n.number(index + 1)} of {i18n.number(all.length)}
						</Trans>
					</span>
					<Button
						size="icon-xs"
						variant="ghost"
						aria-label={t`Next version`}
						disabled={index === all.length - 1}
						onClick={() => setPick({ draft, index: index + 1 })}
					>
						<Icon name="caret-right" size={16} />
					</Button>
				</div>
			)}
			{draft.phase === "ready" && (
				<div
					className={cn(ENTER_CLASS, "flex flex-wrap items-center gap-1 rounded-xl bg-ink p-1 ps-3 text-bg shadow-e3")}
				>
					<span className="me-auto flex items-center gap-1.5 py-1 text-[13px] font-medium">
						<Icon name="sparkle" size={16} />
						{sources}
					</span>
					<button
						type="button"
						onClick={() => onKeep(shown.text, shown.aiOperationIds)}
						className="touch-target relative h-8 rounded-lg bg-accent px-3 text-[13px] font-semibold text-on-accent hover:bg-accent-hover"
					>
						<Trans>Accept</Trans>
					</button>
					{(
						[
							["shorter", t`Shorter`],
							["personal", t`More personal`],
						] as const
					).map(([variant, label]) => (
						<button
							key={variant}
							type="button"
							onClick={() => run(variant, shown.text, shown.aiOperationIds)}
							className="touch-target relative h-8 rounded-lg px-2.5 text-[13px] hover:bg-[oklch(1_0_0/0.12)]"
						>
							{label}
						</button>
					))}
					<button
						type="button"
						onClick={discardLetterDraft}
						className="touch-target relative h-8 rounded-lg px-2.5 text-[13px] hover:bg-[oklch(1_0_0/0.12)]"
					>
						<Trans>Reject</Trans>
					</button>
				</div>
			)}
		</div>
	);
}

/** Length: the word count against the 180 to 320 band most recruiters read, as a hint rather than a rule. */
function LengthSection({ content }: { content: string }) {
	const words = countWords(content);
	const length = letterLength(words);
	const color = length === "empty" ? "text-ink-3" : length === "comfortable" ? "text-accent-text" : "text-warn-text";
	const fill = length === "empty" ? "bg-line-2" : length === "comfortable" ? "bg-accent" : "bg-warn";
	const percent = (value: number) => `${(value / LETTER_LENGTH.scale) * 100}%`;

	return (
		<Section
			id="letter-length"
			title={<Trans>Length</Trans>}
			aside={
				<span className={cn("font-mono text-xs font-medium", color)}>
					<Plural value={words} one="# word" other="# words" />
				</span>
			}
		>
			<div aria-hidden="true" className="relative h-1.5 overflow-hidden rounded-sm bg-sunken">
				<span
					className="absolute inset-y-0 bg-line-2"
					style={{
						insetInlineStart: percent(LETTER_LENGTH.min),
						width: percent(LETTER_LENGTH.max - LETTER_LENGTH.min),
					}}
				/>
				<span
					className={cn(
						"absolute inset-y-0 start-0 w-full translate-x-(--fill) rounded-sm transition-[translate] duration-quick ease-enter rtl:-translate-x-(--fill)",
						fill,
					)}
					style={
						{
							"--fill": `${(Math.min(words, LETTER_LENGTH.scale) / LETTER_LENGTH.scale) * 100 - 100}%`,
						} as CSSProperties
					}
				/>
			</div>
			<p className="text-xs text-ink-3">
				{length === "empty" ? (
					<Trans>Most recruiters read 180–320 words. The shaded band shows the range.</Trans>
				) : length === "short" ? (
					<Trans>Short and direct. Fine if the posting asks for brevity.</Trans>
				) : length === "comfortable" ? (
					<Trans>A comfortable length, and it fits on one page.</Trans>
				) : (
					<Trans>Getting long. Try “Shorter”, or cut the least specific paragraph.</Trans>
				)}
			</p>
		</Section>
	);
}

/** DESIGN: what the letter looks like, and where to change it. */
function DesignNote({ letter }: { letter: CoverLetter }) {
	const { i18n } = useLingui();
	const [, setMode] = useLetterMode();
	const { metadata } = letter.style;
	const pairingLabel = FONT_PAIRINGS.find((option) => option.id === matchFontPairing(metadata))?.label;
	const accent = ACCENTS.find((option) => option.hex === rgbaToHex(metadata.design.colors.primary))?.label;
	const pairing = pairingLabel && i18n._(pairingLabel);
	const summary = [templates[metadata.template].name, pairing && t`${pairing} type`, accent && i18n._(accent)]
		.filter(Boolean)
		.join(", ");

	return (
		<Section id="letter-design" title={<Trans>Design</Trans>}>
			<p className="text-sm text-ink-2">
				{letter.designLinked ? (
					<Trans>Matches the resume ({summary}).</Trans>
				) : (
					<Trans>Its own design ({summary}).</Trans>
				)}{" "}
				<button
					type="button"
					onClick={() => setMode("design")}
					className="font-medium text-accent-text underline underline-offset-2"
				>
					<Trans>Change it in Design.</Trans>
				</button>
			</p>
		</Section>
	);
}