// static/js/components/consistency.js
import { API_BASE_URL, inconsistencyData, setInconsistencyData, CONSISTENCY_POLL_INTERVAL } from '../store/configStore.js';
import { request } from '../api/baseApi.js';
import { showModal, hideModal } from './modals.js';
export async function checkConsistency() {
const button = document.getElementById('consistency-button');
if (!button) return;
const hasPreviousConflict = inconsistencyData !== null;
button.textContent = 'Checking...';
button.classList.add('loading');
button.classList.remove('consistent', 'inconsistent', 'error', 'loading'); // Reset before applying new state
try {
const data = await request('/is-consistent');
const consistencyStatus = data.consistent;
const isNewConflict = consistencyStatus.inconsistent === true;
const stateChanged = isNewConflict !== hasPreviousConflict;
if (isNewConflict) {
button.textContent = '🚨 CONFLICT';
button.classList.add('inconsistent');
button.disabled = false;
setInconsistencyData(consistencyStatus);
} else {
button.textContent = '✅ Consistent';
button.classList.add('consistent');
button.disabled = true;
setInconsistencyData(null);
hideModal('consistencyModal');
}
} catch (error) {
button.classList.add('error');
button.textContent = '❌ Error';
button.disabled = true;
setInconsistencyData(null);
console.error("Consistency check failed:", error);
}
}
export function showConsistencyModal() {
if (!inconsistencyData || inconsistencyData.inconsistent === false) return;
const cacheOnly = inconsistencyData['cache-only'] || {};
const dbOnly = inconsistencyData['db-only'] || {};
document.getElementById('cache-only-count').textContent = Object.keys(cacheOnly).length;
document.getElementById('cache-only-data').textContent = JSON.stringify(cacheOnly, null, 2);
document.getElementById('db-only-count').textContent = Object.keys(dbOnly).length;
document.getElementById('db-only-data').textContent = JSON.stringify(dbOnly, null, 2);
showModal('consistencyModal');
}
export async function resolveConsistency(strategy) {
try {
await request(`/resolve-consistency?strategy=${strategy}`, { method: 'POST' });
alert(`Consistency resolution (${strategy}) successful.`);
checkConsistency();
} catch (error) {
alert(`Failed to resolve consistency. Error: ${error.message}`);
}
}