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!

fix: fit wide labels and complete preview status text (RX-208)

Amruth Pillaib2936fc6ecff
diff --git a/apps/web/src/features/documents/document-card.test.tsx b/apps/web/src/features/documents/document-card.test.tsx
index 6bbd375c4..478a921f8 100644
--- a/apps/web/src/features/documents/document-card.test.tsx
+++ b/apps/web/src/features/documents/document-card.test.tsx
@@ -111,6 +111,10 @@ it("shows a failed preview instead of an endless loading placeholder", async ()
renderCard(letter(new Date("2026-10-19T00:00:00.000Z")));
expect(await screen.findByText("Preview unavailable")).toBeTruthy();
+ const link = screen.getByRole("link", { name: "Untitled letter" });
+ expect(document.getElementById(link.getAttribute("aria-describedby") ?? "")?.textContent).toBe(
+ "Northwind Labs. Preview unavailable",
+ );
});
it("does not fetch letter content while the card is offscreen", () => {
diff --git a/apps/web/src/features/documents/document-card.tsx b/apps/web/src/features/documents/document-card.tsx
index 6ad5f4538..5aba26219 100644
--- a/apps/web/src/features/documents/document-card.tsx
+++ b/apps/web/src/features/documents/document-card.tsx
@@ -117,9 +117,9 @@ function LetterThumbnail({ document, descriptionId }: { document: DocumentSummar
const greeting = letter.data ? letterGreeting(letter.data.greeting) : null;
const lines = letter.data ? letterBodyLines(letter.data.content) : [];
const empty = letter.isSuccess && !greeting && lines.length === 0;
- const description =
- [recipient ? `${recipient}.` : "", greeting ?? "", lines[0] ?? ""].filter(Boolean).join(" ") ||
- (letter.isError ? t`Preview unavailable` : empty ? t`Empty letter` : "");
+ const preview = [greeting, lines[0]].filter(Boolean).join(" ");
+ const status = letter.isError ? t`Preview unavailable` : empty ? t`Empty letter` : "";
+ const description = [recipient ? `${recipient}.` : "", preview, status].filter(Boolean).join(" ");
return (
<>
diff --git a/apps/web/src/features/documents/letter-preview.test.ts b/apps/web/src/features/documents/letter-preview.test.ts
index 1be2b3fb1..31707715f 100644
--- a/apps/web/src/features/documents/letter-preview.test.ts
+++ b/apps/web/src/features/documents/letter-preview.test.ts
@@ -22,6 +22,14 @@ describe("letterBodyLines", () => {
]);
});
+ it("splits lines at attributed break tags too", () => {
+ expect(letterBodyLines('<p>First<br class="tight">Second<br data-x="1" />Third</p>')).toEqual([
+ "First",
+ "Second",
+ "Third",
+ ]);
+ });
+
it("decodes entities the stored letter HTML carries", () => {
expect(letterBodyLines("<p>R&amp;D &amp; design&nbsp;team</p>")).toEqual(["R&D & design team"]);
expect(letterBodyLines("<p>&lt;hello&gt; &#39;world&#39;</p>")).toEqual(["<hello> 'world'"]);
diff --git a/apps/web/src/features/documents/letter-preview.ts b/apps/web/src/features/documents/letter-preview.ts
index 8bac991b5..487995d80 100644
--- a/apps/web/src/features/documents/letter-preview.ts
+++ b/apps/web/src/features/documents/letter-preview.ts
@@ -57,7 +57,7 @@ function decodeEntities(text: string): string {
export function letterBodyLines(content: string | null | undefined, max = LETTER_PREVIEW_LINES): string[] {
if (!content) return [];
const text = decodeEntities(
- stripHtml(content.replace(/<\/(?:p|div|li|h[1-6])>|<br\s*\/?>/gi, "\n").replace(/\r/g, "")),
+ stripHtml(content.replace(/<\/(?:p|div|li|h[1-6])>|<br\b[^>]*>/gi, "\n").replace(/\r/g, "")),
).replace(/\u00a0/g, " ");
return text
.split("\n")
diff --git a/packages/utils/src/string.test.ts b/packages/utils/src/string.test.ts
index 132b924ea..3a5ac12c4 100644
--- a/packages/utils/src/string.test.ts
+++ b/packages/utils/src/string.test.ts
@@ -26,15 +26,18 @@ describe("fitLabel", () => {
expect(fitLabel("Recruiter call", 14)).toBe("Recruiter call");
});
- it("ends a long label with an ellipsis within the limit", () => {
+ it("ends a long narrow label with an ellipsis within the width budget", () => {
expect(fitLabel("a".repeat(20), 18)).toBe(`${"a".repeat(17)}…`);
});
- it("counts code points so an emoji is never cut in half", () => {
- const label = "😀".repeat(20);
- const fitted = fitLabel(label, 18);
- expect(fitted).toBe(`${"😀".repeat(17)}…`);
- expect([...fitted]).toHaveLength(18);
+ it("counts wide glyphs as twice a narrow one", () => {
+ expect(fitLabel("漢".repeat(20), 18)).toBe(`${"漢".repeat(8)}…`);
+ expect(fitLabel("😀".repeat(20), 18)).toBe(`${"😀".repeat(8)}…`);
+ });
+
+ it("keeps a multi-code-point emoji cluster whole", () => {
+ const family = "👨‍👩‍👧‍👦";
+ expect(fitLabel(family.repeat(10), 18)).toBe(`${family.repeat(8)}…`);
});
});
diff --git a/packages/utils/src/string.ts b/packages/utils/src/string.ts
index a6da0981f..ae6420fd7 100644
--- a/packages/utils/src/string.ts
+++ b/packages/utils/src/string.ts
@@ -93,13 +93,83 @@ export function stripHtml(html: string | undefined): string {
}
/**
- * Shortens a label to at most `max` code points, ending with an ellipsis when it was longer. Code points (not UTF-16
- * units) keep astral characters such as emoji whole, so a chart label never ends on half a surrogate pair.
+ * Shortens a label to an approximate `maxWidth`, where a narrow character counts as one unit and a wide glyph (CJK,
+ * Hangul, full-width or emoji) as two. Grapheme clusters stay whole, so an emoji ZWJ sequence or a combining accent is
+ * never cut; `Intl.Segmenter` is used when the runtime provides it, with code points as the fallback.
*/
-export function fitLabel(text: string, max: number): string {
- if (max <= 0) return "";
- const characters = Array.from(text);
- return characters.length > max ? `${characters.slice(0, max - 1).join("")}…` : text;
+type GraphemeSegmenter = { segment(input: string): Iterable<{ segment: string }> };
+const segmenterConstructor = (
+ Intl as unknown as {
+ Segmenter?: new (locale?: string, options?: { granularity: "grapheme" }) => GraphemeSegmenter;
+ }
+).Segmenter;
+const graphemeSegmenter = (() => {
+ if (!segmenterConstructor) return null;
+ try {
+ return new segmenterConstructor(undefined, { granularity: "grapheme" });
+ } catch {
+ return null;
+ }
+})();
+
+/** A small fallback for runtimes without `Intl.Segmenter`: keep combining marks, variation selectors, modifiers and
+ * ZWJ sequences attached to their base, so a label never ends on a partial emoji or a stranded accent. */
+function fallbackGraphemes(text: string): string[] {
+ const parts: string[] = [];
+ for (const character of text) {
+ const code = character.codePointAt(0) ?? 0;
+ const attached =
+ (code >= 0x0300 && code <= 0x036f) ||
+ (code >= 0x1ab0 && code <= 0x1aff) ||
+ (code >= 0x1dc0 && code <= 0x1dff) ||
+ (code >= 0x20d0 && code <= 0x20ff) ||
+ (code >= 0xfe20 && code <= 0xfe2f) ||
+ (code >= 0xfe00 && code <= 0xfe0f) ||
+ (code >= 0x1f3fb && code <= 0x1f3ff) ||
+ code === 0x200d ||
+ code === 0x20e3;
+ const previous = parts.at(-1);
+ if (previous !== undefined && (attached || previous.endsWith("\u200d"))) {
+ parts[parts.length - 1] = previous + character;
+ } else {
+ parts.push(character);
+ }
+ }
+ return parts;
+}
+
+const graphemes = (text: string): string[] =>
+ graphemeSegmenter ? Array.from(graphemeSegmenter.segment(text), (part) => part.segment) : fallbackGraphemes(text);
+
+/** CJK, Hangul, full-width punctuation and emoji occupy roughly twice the width of a Latin glyph. */
+function glyphWidth(grapheme: string): number {
+ const code = grapheme.codePointAt(0) ?? 0;
+ const wide =
+ (code >= 0x1100 && code <= 0x115f) ||
+ (code >= 0x2e80 && code <= 0xa4cf) ||
+ (code >= 0xac00 && code <= 0xd7a3) ||
+ (code >= 0xf900 && code <= 0xfaff) ||
+ (code >= 0xfe30 && code <= 0xfe4f) ||
+ (code >= 0xff00 && code <= 0xff60) ||
+ (code >= 0xffe0 && code <= 0xffe6) ||
+ (code >= 0x1f300 && code <= 0x1faff) ||
+ (code >= 0x20000 && code <= 0x3fffd);
+ return wide ? 2 : 1;
+}
+
+export function fitLabel(text: string, maxWidth = 18): string {
+ if (maxWidth <= 0) return "";
+ const parts = graphemes(text);
+ if (parts.reduce((total, part) => total + glyphWidth(part), 0) <= maxWidth) return text;
+ let width = 0;
+ let end = 0;
+ while (end < parts.length) {
+ const next = glyphWidth(parts[end] ?? "");
+ if (width + next > maxWidth - 1) break;
+ width += next;
+ end++;
+ }
+ return `${parts.slice(0, end).join("")}…`;
}
/** FNV-1a over the text: a short, stable fingerprint. Not for security. */