const fs = require('fs');
let content = fs.readFileSync('subcontracts.js', 'utf8');

const regex = /const tbody = document\.getElementById\('subcontract-abstracts-tbody'\);\s+if\(!tbody\) return;\s+tbody\.innerHTML = '';/;
const replacement = `
    const activeView = document.querySelector('.view.active, [id^="view-"].active');
    if (!activeView) return;
    const table = activeView.querySelector('table');
    if (!table) return;
    const tbody = table.querySelector('tbody') || table.appendChild(document.createElement('tbody'));
    tbody.innerHTML = '';
`;

if (content.match(regex)) {
    content = content.replace(regex, replacement);
    fs.writeFileSync('subcontracts.js', content, 'utf8');
    console.log('Patched subcontracts.js renderAbstracts');
} else {
    console.log('Regex did not match in subcontracts.js');
}
