SentinelReview-AI / static /js /script.js
amadmateen's picture
Style: Overhaul visual layout to premium slate-blue with dynamic emerald/rose verdict status colors
c26f85e
Raw History Blame Contribute Delete
7.99 kB
document.addEventListener('DOMContentLoaded', () => {
const reviewForm = document.getElementById('review-form');
const reviewTextarea = document.getElementById('review-text');
const imageInput = document.getElementById('review-image');
const imagePreview = document.getElementById('image-preview');
const previewWrapper = document.getElementById('preview-wrapper');
const uploadPrompt = document.getElementById('upload-prompt');
const removeImgBtn = document.getElementById('remove-img-btn');
const dropZone = document.getElementById('drop-zone');
const analyzeBtn = document.getElementById('analyze-btn');
const loader = document.getElementById('loader');
const resultsEmpty = document.getElementById('results-empty');
const resultContainer = document.getElementById('result-container');
const classificationBadge = document.getElementById('classification-badge');
const classificationText = document.getElementById('classification-text');
const confidenceValue = document.getElementById('confidence-value');
const metricSentiment = document.getElementById('metric-sentiment');
const metricLength = document.getElementById('metric-length');
const metricCapital = document.getElementById('metric-capital');
const metricPunctuation = document.getElementById('metric-punctuation');
const explanationText = document.getElementById('explanation-text');
const uploadedImageContainer = document.getElementById('uploaded-image-container');
const resultImage = document.getElementById('result-image');
const btnCopyExplanation = document.getElementById('btn-copy-explanation');
// 1. File Upload Preview Handler
const handleFileSelect = (file) => {
if (file && file.type.startsWith('image/')) {
const reader = new FileReader();
reader.onload = (e) => {
imagePreview.src = e.target.result;
previewWrapper.style.display = 'block';
uploadPrompt.style.display = 'none';
};
reader.readAsDataURL(file);
}
};
imageInput.addEventListener('change', (e) => {
const file = e.target.files[0];
handleFileSelect(file);
});
removeImgBtn.addEventListener('click', (e) => {
e.stopPropagation(); // Prevent triggering dropZone click
imageInput.value = '';
imagePreview.src = '';
previewWrapper.style.display = 'none';
uploadPrompt.style.display = 'block';
});
// 2. Drag & Drop Zone Handlers
['dragenter', 'dragover'].forEach(eventName => {
dropZone.addEventListener(eventName, (e) => {
e.preventDefault();
dropZone.classList.add('dragover');
}, false);
});
['dragleave', 'drop'].forEach(eventName => {
dropZone.addEventListener(eventName, (e) => {
e.preventDefault();
dropZone.classList.remove('dragover');
}, false);
});
dropZone.addEventListener('drop', (e) => {
const dt = e.dataTransfer;
const file = dt.files[0];
if (file && file.type.startsWith('image/')) {
imageInput.files = dt.files;
handleFileSelect(file);
}
});
// 3. Form Submission Audit Runner
reviewForm.addEventListener('submit', async (e) => {
e.preventDefault();
const textValue = reviewTextarea.value.trim();
if (!textValue) {
alert('Please enter a review text to audit.');
return;
}
// Configure active loader display
resultsEmpty.style.display = 'none';
resultContainer.style.display = 'none';
loader.style.display = 'flex';
analyzeBtn.disabled = true;
try {
const formData = new FormData();
formData.append('review_text', textValue);
if (imageInput.files.length > 0) {
formData.append('review_image', imageInput.files[0]);
}
// Post review to Flask API endpoint
const response = await fetch('/analyze', {
method: 'POST',
body: formData
});
if (!response.ok) {
const errorData = await response.json();
throw new Error(errorData.error || 'Review audit analysis failed.');
}
const data = await response.json();
// Render Classification summary
classificationText.textContent = data.classification.toUpperCase();
classificationBadge.className = 'classification-badge';
classificationBadge.classList.add(data.classification.toLowerCase());
confidenceValue.textContent = data.confidence;
// Toggle dynamic dashboard visual themes (green for verified, red for suspicious)
resultContainer.className = 'editorial-report-wrapper';
resultContainer.classList.add('status-' + data.classification.toLowerCase());
// Render Extracted Vector Metrics
metricSentiment.textContent = data.metrics.sentiment_score;
metricLength.textContent = data.metrics.length;
metricCapital.textContent = `${(data.metrics.capitalization_ratio * 100).toFixed(1)}%`;
metricPunctuation.textContent = data.metrics.punctuation_count;
// Render LLM explanation text
explanationText.textContent = data.explanation;
// Handle audit visual image preview attachment
if (data.image) {
resultImage.src = 'data:image/jpeg;base64,' + data.image;
uploadedImageContainer.style.display = 'block';
} else {
uploadedImageContainer.style.display = 'none';
}
// Trigger result sheet fade-in
resultContainer.style.display = 'flex';
} catch (error) {
alert(error.message || 'An error occurred during auditing.');
console.error('[Audit Error]', error);
resultsEmpty.style.display = 'flex';
} finally {
loader.style.display = 'none';
analyzeBtn.disabled = false;
}
});
// 4. Character Limit Counter
reviewTextarea.addEventListener('input', function() {
const maxLength = 1000;
const currentLength = this.value.length;
let counter = this.parentNode.querySelector('.char-counter');
if (!counter) {
counter = document.createElement('div');
counter.classList.add('char-counter');
counter.style.textAlign = 'right';
counter.style.fontSize = '0.75rem';
counter.style.color = 'var(--text-muted)';
counter.style.marginTop = '0.25rem';
this.parentNode.appendChild(counter);
}
counter.textContent = `${currentLength}/${maxLength} CHARACTER VECTORS`;
if (currentLength >= maxLength) {
counter.style.color = 'var(--suspicious-color)';
} else {
counter.style.color = 'var(--text-muted)';
}
});
// 5. Copy Log Clipboard Handler
btnCopyExplanation.addEventListener('click', () => {
const text = explanationText.textContent;
navigator.clipboard.writeText(text).then(() => {
const originalText = btnCopyExplanation.querySelector('.copy-text').textContent;
btnCopyExplanation.querySelector('.copy-text').textContent = 'COPIED!';
btnCopyExplanation.style.color = 'var(--verified-color)';
setTimeout(() => {
btnCopyExplanation.querySelector('.copy-text').textContent = originalText;
btnCopyExplanation.style.color = 'var(--accent-blue)';
}, 1500);
}).catch(err => {
console.error('Failed to copy to clipboard', err);
});
});
});