fix: remount AI translation editor on value change, not disabled transition (#8084)

This commit is contained in:
Dhruwang Jariwala
2026-05-21 15:39:57 +05:30
committed by GitHub
parent db87a588b5
commit d5bbafcf90
@@ -26,17 +26,21 @@ export const RichTextTranslationInput = ({
}: RichTextTranslationInputProps) => {
const [firstRender, setFirstRender] = useState(true);
const [editorKey, setEditorKey] = useState(0);
const prevDisabledRef = useRef(disabled);
// Separates external value changes (e.g. AI fill) from the editor's own write-back so we
// only remount for the former.
const lastWrittenRef = useRef(value);
// Suppresses Lexical's mount-time empty listener fire which would otherwise clobber an
// externally-applied value back to "".
const initialContentSetRef = useRef(false);
// Remount the editor when AI translation finishes (disabled transitions from true → false)
// so the editor picks up the externally populated value.
useEffect(() => {
if (prevDisabledRef.current && !disabled) {
if (value !== lastWrittenRef.current) {
lastWrittenRef.current = value;
initialContentSetRef.current = false;
setEditorKey((k) => k + 1);
setFirstRender(true);
}
prevDisabledRef.current = disabled;
}, [disabled]);
}, [value]);
return (
<div className={disabled ? "cursor-not-allowed rounded-md opacity-60" : "rounded-md"}>
@@ -47,7 +51,12 @@ export const RichTextTranslationInput = ({
firstRender={firstRender}
setFirstRender={setFirstRender}
getText={() => md.render(value)}
setText={(v: string) => onChange(path, v)}
setText={(v: string) => {
if (!initialContentSetRef.current && v === "") return;
initialContentSetRef.current = true;
lastWrittenRef.current = v;
onChange(path, v);
}}
localSurvey={localSurvey}
elementId={elementId}
selectedLanguageCode={languageCode}