Files
Momento/memento-note/scripts/fix-translation-ui.js
sepehr 635e516616
All checks were successful
Deploy to Production / Build and Deploy (push) Successful in 46s
fix(ai): live update for translation language input + preset sync
2026-05-03 01:42:22 +02:00

52 lines
2.2 KiB
JavaScript

const fs = require('fs');
const path = require('path');
const filePath = path.join(__dirname, '..', 'components', 'contextual-ai-chat.tsx');
let src = fs.readFileSync(filePath, 'utf8');
// Sync presets buttons
const OLD_PRESET_CLICK = `onClick={() => setTranslateTarget(l)}`;
const NEW_PRESET_CLICK = `onClick={() => { setTranslateTarget(l); setCustomLangInput(l); }}`;
if (src.includes(OLD_PRESET_CLICK)) {
src = src.replace(OLD_PRESET_CLICK, NEW_PRESET_CLICK);
console.log('Fix 1: Preset buttons sync updated');
} else {
console.error('Fix 1: OLD_PRESET_CLICK not found');
process.exit(1);
}
// Sync input and enable live update
const OLD_INPUT_LOGIC = `onChange={e => setCustomLangInput(e.target.value)}\r\n onKeyDown={e => { if (e.key === 'Enter' && customLangInput.trim()) { setTranslateTarget(customLangInput.trim()); setCustomLangInput('') } }}`;
// Fallback for different line endings if needed, but let's try matching a smaller part first
const OLD_ONCHANGE = `onChange={e => setCustomLangInput(e.target.value)}`;
const NEW_ONCHANGE = `onChange={e => { const val = e.target.value; setCustomLangInput(val); setTranslateTarget(val); }}`;
const OLD_ONKEYDOWN = `onKeyDown={e => { if (e.key === 'Enter' && customLangInput.trim()) { setTranslateTarget(customLangInput.trim()); setCustomLangInput('') } }}`;
const NEW_ONKEYDOWN = `onKeyDown={e => { if (e.key === 'Enter' && translateTarget.trim()) { handleAction(action, translateTarget.trim()); } }}`;
if (src.includes(OLD_ONCHANGE)) {
src = src.replace(OLD_ONCHANGE, NEW_ONCHANGE);
console.log('Fix 2: Input onChange updated');
} else {
console.error('Fix 2: OLD_ONCHANGE not found');
process.exit(1);
}
if (src.includes(OLD_ONKEYDOWN)) {
src = src.replace(OLD_ONKEYDOWN, NEW_ONKEYDOWN);
console.log('Fix 3: Input onKeyDown updated');
} else {
console.error('Fix 3: OLD_ONKEYDOWN not found');
// Try without \r if it failed
const altOnKeyDown = OLD_ONKEYDOWN.replace('\r\n', '\n');
if (src.includes(altOnKeyDown)) {
src = src.replace(altOnKeyDown, NEW_ONKEYDOWN);
console.log('Fix 3: Input onKeyDown updated (alt line ending)');
} else {
process.exit(1);
}
}
fs.writeFileSync(filePath, src);
console.log('Done!');