Repository navigation
Description
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
- As a group manager with mail enabled, open
/group/email-templatesand click Edit on a template that has an English body (Hello there …) and a French body (Bonjour à vous …). - Click into the English body just after
Hello(offset 5), then open the Language select and choose French. - 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
joshunrau commentedon Oct 26, 2026
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.
insertVariablereplaces everything up tocursor.end(EmailTemplateEditor.tsx:48), so a range selected in one language removes characters from the other language's body. With the unit test fixture: selectworld(6-11) inHello world, switch to Spanish, insert{{name}}, and the Spanish body becomesHola m{{name}}.Fix
Use only the second half of the suggested fix. Make the ref (
:33){ body, end, start }, setbodyinonBlur(:112-117), and set it to the spliced result where the caret advances after an insert (:45). IninsertVariable, append whencursor === 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 becausesliceclamps on an empty body. AsetLanguagewrapper would only add the case where both languages hold identical text, and there the old offset is still valid.Don't read
selectionStartoff the textarea at click time instead. A controlled update that changes the text setsvalue, 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:blurBodyWithSelection(6, 11),chooseLanguage('Spanish'), insert, expectes: '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,rerenderwithen: 'Goodbye everyone', insert, expect'Goodbye everyone {{name}}'.:91-105) fails if the advance at:45forgets to store the new body, so it must keep passing unchanged.group-email-templates.spec.ts:email-templates.page.tshas no language locator. AddcreateLanguage(template-create-language, already rendered fromidPrefix) and a method that opens it and clicks theoptionby name.{{url}}onmaintoo. Instead: select French and fillBonjour à vous, select English and fillHello, select French again, clickinsertUrl, thentoHaveValue('Bonjour à vous {{url}}').maingivesBonjo{{url}}ur à vous. Assert the exact value, since the regex at:31matches wherever the tag lands.['en', 'fr'].#1810 also edits
EmailTemplateEditor.tsx, but only the props and theLanguageSelect(:56-61). This fix stays in the ref,insertVariableandonBlur, so either can land first.Triage note written with Claude Code. Code references point at
main@d86740e.