Skip to content

Email template placeholder is inserted at the previous language's caret position after switching language #1681

Open

Description

@joshunrau

After switching the template language, EmailTemplateEditor inserts a placeholder at the caret position left over from the previous language's body. bodyCursorRef records the caret only when the body textarea loses focus, and nothing clears it when language changes. When the user switches language and clicks an insert button such as {{url}} without first clicking into the new body, the tag goes in at the old offset. That offset belongs to a different string, so the tag usually lands in the middle of a word or sentence instead of at the end. "Reset" in the New User Email Template section replaces the body the same way and leaves the same stale caret behind. This affects every template editor: the group remote-assignment templates (create and edit) and the new-user welcome template.

Where

apps/web/src/components/EmailTemplateEditor/EmailTemplateEditor.tsx:30-50:

const [language, setLanguage] = React.useState<Language>(resolvedLanguage);
// Where the caret was when the body last lost focus, ...
const bodyCursorRef = React.useRef<null | { end: number; start: number }>(null);
// ...
const insertVariable = (edit: (template: MailTemplate) => void, variable: string) => {
  const tag = `{{${variable}}}`;
  const cursor = bodyCursorRef.current;
  if (cursor === null) {
    edit({ ...template, body: { ...template.body, [language]: body ? `${body} ${tag}` : tag } });
    return;
  }
  bodyCursorRef.current = { end: cursor.start + tag.length, start: cursor.start + tag.length };
  edit({
    ...template,
    body: { ...template.body, [language]: body.slice(0, cursor.start) + tag + body.slice(cursor.end) }
  });
};

EmailTemplateEditor.tsx:56-61: onChange={setLanguage} switches the body without resetting the ref.

Reproduce

  1. As a group manager with mail enabled, open /group/email-templates and click Edit on a template that has an English body (Hello there …) and a French body (Bonjour à vous …).
  2. Click into the English body just after Hello (offset 5), then open the Language select and choose French.
  3. Without clicking into the French body, click {{url}}.

Actual: the French body becomes Bonjo{{url}}ur à vous …. The tag lands at the English caret offset, in the middle of a word.
Expected: after a language switch, a placeholder is added at the end of the current body until the user places the caret in it.

Tests

apps/web/src/components/EmailTemplateEditor/__tests__/EmailTemplateEditor.test.tsx: it('should append a placeholder after a language switch, since the remembered caret belongs to the other language's body').

testing/src/specs/group-email-templates.spec.ts: extend should insert a placeholder into the body from the toolbar with a language switch before the insert, and assert that the tag ends the new body.

Suggested fix

Clear bodyCursorRef.current whenever the language changes. Wrap setLanguage in a handler that sets the ref to null and then switches. To cover "Reset" too, also clear the ref when the current language's body no longer matches the text the caret was recorded against: store that text next to the caret and compare before using the offset.

Activity

  1. added
    Bug
    Priority: Low
    Difficulty: Low
    Area: Web
    Good First Issue
    on Oct 7, 2026
  2. joshunrau commented on Oct 26, 2026

    @joshunrau
    CollaboratorAuthor

    Still reproduces on main (d86740e). The cited lines have not moved, and no commit since the issue was filed touches the editor.

    A stale selection also deletes text. insertVariable replaces everything up to cursor.end (EmailTemplateEditor.tsx:48), so a range selected in one language removes characters from the other language's body. With the unit test fixture: select world (6-11) in Hello world, switch to Spanish, insert {{name}}, and the Spanish body becomes Hola m{{name}}.

    Fix

    Use only the second half of the suggested fix. Make the ref (:33) { body, end, start }, set body in onBlur (:112-117), and set it to the spliced result where the caret advances after an insert (:45). In insertVariable, append when cursor === null || cursor.body !== body. That one comparison covers the language switch, Reset, and the create form's clear after a successful create (CreateTemplateForm.tsx:37), which is harmless today only because slice clamps on an empty body. A setLanguage wrapper would only add the case where both languages hold identical text, and there the old offset is still valid.

    Don't read selectionStart off the textarea at click time instead. A controlled update that changes the text sets value, and the HTML spec then moves the caret to the end, so the second of two consecutive inserts would land at the end.

    Tests

    EmailTemplateEditor.test.tsx:

    • Write the issue's language-switch test with a selection: blurBodyWithSelection(6, 11), chooseLanguage('Spanish'), insert, expect es: 'Hola mundo {{name}}'. That checks the position and that no text is lost.
    • it('should append a placeholder when the body was replaced after the caret was recorded, so Reset does not reuse a stale offset'): blur at 5, rerender with en: 'Goodbye everyone', insert, expect 'Goodbye everyone {{name}}'.
    • The existing consecutive-insert test (:91-105) fails if the advance at :45 forgets to store the new body, so it must keep passing unchanged.

    group-email-templates.spec.ts:

    • email-templates.page.ts has no language locator. Add createLanguage (template-create-language, already rendered from idPrefix) and a method that opens it and clicks the option by name.
    • Extending the toolbar test as the issue suggests does not catch the bug: the other language starts empty in the create form, and an empty body gives {{url}} on main too. Instead: select French and fill Bonjour à vous, select English and fill Hello, select French again, click insertUrl, then toHaveValue('Bonjour à vous {{url}}'). main gives Bonjo{{url}}ur à vous. Assert the exact value, since the regex at :31 matches wherever the tag lands.
    • Stick to English and French. fix(web): scope email template languages to active languages #1810 limits the select to the instance's active languages, and the e2e instance runs on the default ['en', 'fr'].

    #1810 also edits EmailTemplateEditor.tsx, but only the props and the LanguageSelect (:56-61). This fix stays in the ref, insertVariable and onBlur, so either can land first.

    Triage note written with Claude Code. Code references point at main @ d86740e.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    Area: WebBugDifficulty: LowGood First IssuePriority: Low

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions