Skip to content

Job application tracker and AI-powered job search assistant. Helps job seekers manage their search journey with AI resume review, job matching, task logging, and application analytics, all while keeping your data private.

feat(cover-letter-export): add CoverLetterExportSettings model, storage key and buildLetterStyles

Khuram Niaz31733a3bc025
diff --git a/__tests__/coverLetterExportSettings.spec.ts b/__tests__/coverLetterExportSettings.spec.ts
new file mode 100644
index 0000000..c30b99b
--- /dev/null
+++ b/__tests__/coverLetterExportSettings.spec.ts
@@ -0,0 +1,101 @@
+import { describe, it, expect } from "vitest";
+import {
+ coerceCoverLetterExportSettings,
+ COVER_LETTER_NUMERIC_FIELDS,
+ defaultCoverLetterExportSettings,
+} from "@/models/coverLetterExport.model";
+
+describe("defaultCoverLetterExportSettings", () => {
+ it("is the letter's own literals", () => {
+ expect(defaultCoverLetterExportSettings).toEqual({
+ font: "helvetica",
+ fontSize: 11,
+ lineHeight: 1.4,
+ marginVertical: 54,
+ marginHorizontal: 54,
+ paragraphSpacing: 10,
+ });
+ });
+
+ it("lists exactly the five numeric fields the panel steps", () => {
+ expect(COVER_LETTER_NUMERIC_FIELDS).toEqual([
+ "fontSize",
+ "lineHeight",
+ "marginVertical",
+ "marginHorizontal",
+ "paragraphSpacing",
+ ]);
+ });
+});
+
+describe("coerceCoverLetterExportSettings", () => {
+ it("falls back to the defaults for anything that is not an object", () => {
+ for (const value of [null, undefined, 7, "helvetica", []]) {
+ expect(coerceCoverLetterExportSettings(value)).toEqual(
+ defaultCoverLetterExportSettings,
+ );
+ }
+ });
+
+ it("keeps every valid value", () => {
+ const stored = {
+ font: "times",
+ fontSize: 12,
+ lineHeight: 1.5,
+ marginVertical: 72,
+ marginHorizontal: 60,
+ paragraphSpacing: 6,
+ };
+ expect(coerceCoverLetterExportSettings(stored)).toEqual(stored);
+ });
+
+ it("rejects an unknown font and falls back", () => {
+ expect(coerceCoverLetterExportSettings({ font: "comic-sans" }).font).toBe(
+ "helvetica",
+ );
+ });
+
+ it("fills a missing key from the defaults", () => {
+ const result = coerceCoverLetterExportSettings({ fontSize: 13 });
+ expect(result.fontSize).toBe(13);
+ expect(result.marginVertical).toBe(54);
+ expect(result.paragraphSpacing).toBe(10);
+ });
+
+ it("clamps out-of-range numbers to the field's own bounds", () => {
+ const result = coerceCoverLetterExportSettings({
+ fontSize: 400,
+ lineHeight: -5,
+ marginHorizontal: 1000,
+ paragraphSpacing: 99,
+ });
+ expect(result.fontSize).toBe(16);
+ expect(result.lineHeight).toBe(1);
+ expect(result.marginHorizontal).toBe(90);
+ expect(result.paragraphSpacing).toBe(24);
+ });
+
+ it("replaces garbage values with the letter's defaults", () => {
+ const result = coerceCoverLetterExportSettings({
+ fontSize: "big",
+ lineHeight: null,
+ paragraphSpacing: NaN,
+ });
+ expect(result.fontSize).toBe(11);
+ expect(result.lineHeight).toBe(1.4);
+ expect(result.paragraphSpacing).toBe(10);
+ });
+
+ // The merged spec table carries the resume's fields too; iterating it
+ // would leak sectionSpacing/entrySpacing into a letter settings object.
+ it("never carries resume-only fields through", () => {
+ const result = coerceCoverLetterExportSettings({
+ sectionSpacing: 20,
+ entrySpacing: 20,
+ template: "professional",
+ });
+ expect(result).not.toHaveProperty("sectionSpacing");
+ expect(result).not.toHaveProperty("entrySpacing");
+ expect(result).not.toHaveProperty("template");
+ });
+});
diff --git a/__tests__/coverLetterStyles.spec.ts b/__tests__/coverLetterStyles.spec.ts
new file mode 100644
index 0000000..fe9f3be
--- /dev/null
+++ b/__tests__/coverLetterStyles.spec.ts
@@ -0,0 +1,102 @@
+import { describe, it, expect } from "vitest";
+import { defaultCoverLetterExportSettings } from "@/models/coverLetterExport.model";
+import { buildLetterStyles } from "@/components/profile/cover-letter-pdf/styles/letter.styles";
+
+const letter = (
+ patch: Partial<typeof defaultCoverLetterExportSettings> = {},
+) => buildLetterStyles({ ...defaultCoverLetterExportSettings, ...patch });
+
+describe("buildLetterStyles at the defaults", () => {
+ const s = letter().styles;
+
+ it("keeps the page as the shipped sheet has it", () => {
+ expect(s.page).toEqual({
+ fontFamily: "Helvetica",
+ fontSize: 11,
+ paddingTop: 54,
+ paddingBottom: 54,
+ paddingHorizontal: 54,
+ color: "#000000",
+ lineHeight: 1.4,
+ });
+ });
+
+ it("sizes the letterhead off the base", () => {
+ expect(s.name.fontSize).toBe(16);
+ expect(s.name.fontFamily).toBe("Helvetica-Bold");
+ expect(s.headline.fontSize).toBe(11);
+ expect(s.contactLine.fontSize).toBe(10);
+ expect(s.contactLine.color).toBe("#444444");
+ });
+
+ it("gives the date the base size", () => {
+ expect(s.date.fontSize).toBe(11);
+ });
+
+ it("puts paragraphSpacing on bodyText.marginBottom", () => {
+ expect(s.bodyText).toEqual({ fontSize: 11, marginBottom: 10 });
+ });
+
+ it("exposes the full HtmlStyleSet so a hand-edited letter still renders", () => {
+ const { htmlStyles } = letter();
+ expect(Object.keys(htmlStyles).sort()).toEqual(
+ [
+ "bodyText",
+ "bold",
+ "boldItalic",
+ "bullet",
+ "bulletChar",
+ "h2text",
+ "italic",
+ "listRow",
+ "listText",
+ ].sort(),
+ );
+ expect(htmlStyles.bulletChar).toBe("•");
+ expect(htmlStyles.bold.fontFamily).toBe("Helvetica-Bold");
+ });
+});
+
+describe("the settings reach exactly the properties they own", () => {
+ // The base size is assigned, never scaled through pt, so a half-point
+ // setting survives intact.
+ it("assigns a half-point font size rather than rounding it", () => {
+ const s = letter({ fontSize: 11.5 }).styles;
+ expect(s.page.fontSize).toBe(11.5);
+ expect(s.bodyText.fontSize).toBe(11.5);
+ expect(s.date.fontSize).toBe(11.5);
+ });
+
+ it("scales only the derived sizes with the base", () => {
+ const s = letter({ fontSize: 22 }).styles;
+ expect(s.name.fontSize).toBe(32);
+ expect(s.contactLine.fontSize).toBe(20);
+ });
+
+ // The mirror of the resume rule: a spacing setting reaches its own
+ // properties and no others.
+ it("moves nothing but bodyText.marginBottom when paragraphSpacing changes", () => {
+ const base = letter().styles;
+ const wide = letter({ paragraphSpacing: 24 }).styles;
+
+ expect(wide.bodyText.marginBottom).toBe(24);
+ for (const key of Object.keys(base) as (keyof typeof base)[]) {
+ if (key === "bodyText") continue;
+ expect(wide[key]).toEqual(base[key]);
+ }
+ });
+
+ it("drives the page padding from the two margin settings", () => {
+ const s = letter({ marginVertical: 72, marginHorizontal: 36 }).styles;
+ expect(s.page.paddingTop).toBe(72);
+ expect(s.page.paddingBottom).toBe(72);
+ expect(s.page.paddingHorizontal).toBe(36);
+ });
+
+ it("switches the whole font quartet", () => {
+ const s = letter({ font: "times" }).styles;
+ expect(s.page.fontFamily).toBe("Times-Roman");
+ expect(s.name.fontFamily).toBe("Times-Bold");
+ expect(s.italic.fontFamily).toBe("Times-Italic");
+ });
+});
diff --git a/src/components/profile/cover-letter-pdf/styles/letter.styles.ts b/src/components/profile/cover-letter-pdf/styles/letter.styles.ts
new file mode 100644
index 0000000..45e2193
--- /dev/null
+++ b/src/components/profile/cover-letter-pdf/styles/letter.styles.ts
@@ -0,0 +1,79 @@
+import { StyleSheet } from "@react-pdf/renderer";
+import { HtmlStyleSet } from "@/components/pdf-export/types";
+import { styleTokens } from "@/components/pdf-export/tokens";
+import type { CoverLetterExportSettings } from "@/models/coverLetterExport.model";
+
+export function buildLetterStyles(settings: CoverLetterExportSettings) {
+ const t = styleTokens(settings);
+
+ const styles = StyleSheet.create({
+ page: {
+ fontFamily: t.font.regular,
+ fontSize: t.fontSize,
+ paddingTop: t.marginVertical,
+ paddingBottom: t.marginVertical,
+ paddingHorizontal: t.marginHorizontal,
+ color: "#000000",
+ lineHeight: t.lineHeight,
+ },
+ name: {
+ fontSize: t.pt(16),
+ fontFamily: t.font.bold,
+ marginBottom: 4,
+ },
+ headline: {
+ fontSize: t.fontSize,
+ marginBottom: 2,
+ },
+ contactRow: { flexDirection: "row", flexWrap: "wrap" },
+ contactLine: {
+ fontSize: t.pt(10),
+ color: "#444444",
+ marginBottom: 2,
+ },
+ link: { color: "#000000", textDecoration: "none" },
+ rule: {
+ borderBottomWidth: 0.5,
+ borderBottomColor: "#000000",
+ marginTop: 8,
+ marginBottom: 16,
+ },
+ date: {
+ fontSize: t.fontSize,
+ marginBottom: 16,
+ },
+ bold: { fontFamily: t.font.bold },
+ italic: { fontFamily: t.font.italic },
+ boldItalic: { fontFamily: t.font.boldItalic },
+ // paragraphSpacing reaches exactly this one property. Every other gap
+ // in the letterhead is a plain literal. Read off `settings`, not `t` —
+ // the shared tokens expose only the five universal values, exactly as
+ // resumeStyleTokens adds the resume's two rather than pushing them down.
+ bodyText: { fontSize: t.fontSize, marginBottom: settings.paragraphSpacing },
+ h2text: {
+ fontFamily: t.font.bold,
+ fontSize: t.pt(13),
+ marginBottom: 3,
+ marginTop: 4,
+ },
+ listRow: { flexDirection: "row", marginBottom: 1 },
+ bullet: { width: t.pt(14), fontSize: t.fontSize },
+ listText: { flex: 1, fontSize: t.fontSize },
+ });
+
+ const htmlStyles: HtmlStyleSet = {
+ bodyText: styles.bodyText,
+ bold: styles.bold,
+ italic: styles.italic,
+ boldItalic: styles.boldItalic,
+ h2text: styles.h2text,
+ listRow: styles.listRow,
+ bullet: styles.bullet,
+ listText: styles.listText,
+ bulletChar: "•",
+ };
+
+ return { styles, htmlStyles };
+}
+
+export type LetterStyles = ReturnType<typeof buildLetterStyles>["styles"];
diff --git a/src/lib/constants.ts b/src/lib/constants.ts
index aca700d..39607b7 100644
--- a/src/lib/constants.ts
+++ b/src/lib/constants.ts
@@ -40,6 +40,7 @@ export const APP_CONSTANTS = {
JOBS_VIEW_MODE_STORAGE_KEY: "jobs-view-mode",
RESUME_PREVIEW_FIT_STORAGE_KEY: "resume-preview-fit",
RESUME_EXPORT_SETTINGS_STORAGE_KEY: "resume-export-settings",
+ COVER_LETTER_EXPORT_SETTINGS_STORAGE_KEY: "cover-letter-export-settings",
SIDEBAR_DOM_ID: "app-sidebar",
// Paired so the rail width and its matching content offset can't drift.
SIDEBAR_WIDTH: {
diff --git a/src/models/coverLetterExport.model.ts b/src/models/coverLetterExport.model.ts
new file mode 100644
index 0000000..a301c02
--- /dev/null
+++ b/src/models/coverLetterExport.model.ts
@@ -0,0 +1,67 @@
+import {
+ clampNumericField,
+ PDF_FONT_LABELS,
+ type NumericSetting,
+ type PdfFont,
+} from "./pdfExport.model";
+
+export interface CoverLetterExportSettings {
+ font: PdfFont;
+ fontSize: number;
+ lineHeight: number;
+ marginVertical: number;
+ marginHorizontal: number;
+ paragraphSpacing: number;
+}
+
+// The explicit list the coercion iterates. Iterating the merged
+// PDF_NUMERIC_SETTINGS table would leak the resume's spacing fields in.
+export const COVER_LETTER_NUMERIC_FIELDS = [
+ "fontSize",
+ "lineHeight",
+ "marginVertical",
+ "marginHorizontal",
+ "paragraphSpacing",
+] as const satisfies readonly NumericSetting[];
+
+export type CoverLetterNumericSetting =
+ (typeof COVER_LETTER_NUMERIC_FIELDS)[number];
+
+// A letter is a one-page document with more air than a resume: wider
+// margins, and a real gap between paragraphs instead of the resume's 2pt.
+export const defaultCoverLetterExportSettings: CoverLetterExportSettings = {
+ font: "helvetica",
+ fontSize: 11,
+ lineHeight: 1.4,
+ marginVertical: 54,
+ marginHorizontal: 54,
+ paragraphSpacing: 10,
+};
+
+// Rebuilt from a spread of the defaults, so a stored object can only ever
+// contribute values — never extra keys, and never a missing one.
+export function coerceCoverLetterExportSettings(
+ stored: unknown,
+): CoverLetterExportSettings {
+ const next = { ...defaultCoverLetterExportSettings };
+ if (!stored || typeof stored !== "object" || Array.isArray(stored)) {
+ return next;
+ }
+ const raw = stored as Record<string, unknown>;
+
+ if (typeof raw.font === "string" && raw.font in PDF_FONT_LABELS) {
+ next.font = raw.font as PdfFont;
+ }
+
+ // clampNumericField returns the passed default for anything that is not a
+ // finite number, so a missing key needs no guard.
+ for (const field of COVER_LETTER_NUMERIC_FIELDS) {
+ next[field] = clampNumericField(
+ field,
+ raw[field],
+ defaultCoverLetterExportSettings[field],
+ );
+ }
+
+ return next;
+}