feat: add Doris Kitchen import review workflow
This commit is contained in:
@@ -14,6 +14,14 @@ function escapeHtml(value) {
|
||||
.replace(/'/g, ''');
|
||||
}
|
||||
|
||||
function setStatusMessage(message, tone = 'muted') {
|
||||
const status = document.getElementById('search-status');
|
||||
if (!status) return;
|
||||
status.textContent = message || '';
|
||||
status.className = tone;
|
||||
}
|
||||
|
||||
|
||||
function renderSuggestionCards(target, items) {
|
||||
if (!target) return;
|
||||
if (!items.length) {
|
||||
@@ -28,6 +36,10 @@ function renderSuggestionCards(target, items) {
|
||||
const kitchenowl = item.kitchenowl_status && item.kitchenowl_status !== 'not-imported'
|
||||
? '<p class="pill pill-good">KitchenOwl: ' + escapeHtml(item.kitchenowl_status) + '</p>'
|
||||
: '';
|
||||
const canFlagImport = ['created', 'updated', 'duplicate'].includes(String(item.kitchenowl_status || ''));
|
||||
const flagButton = canFlagImport
|
||||
? '<button type="button" class="ghost" data-flag-suggestion="' + escapeHtml(item.id) + '">Flag import issue</button>'
|
||||
: '';
|
||||
return `
|
||||
<article class="suggestion-card" data-suggestion-id="${escapeHtml(item.id)}">
|
||||
<div class="suggestion-top">
|
||||
@@ -45,6 +57,7 @@ function renderSuggestionCards(target, items) {
|
||||
<button type="button" data-decision="approve">Yes, add to KitchenOwl</button>
|
||||
<button type="button" class="ghost" data-decision="later">Save for later</button>
|
||||
<button type="button" class="ghost danger" data-decision="reject">No thanks</button>
|
||||
${flagButton}
|
||||
</div>
|
||||
</article>
|
||||
`;
|
||||
@@ -86,6 +99,45 @@ function renderFailureCards(target, items) {
|
||||
}).join('');
|
||||
}
|
||||
|
||||
function renderImportFlagCards(target, items) {
|
||||
if (!target) return;
|
||||
if (!items.length) {
|
||||
target.textContent = 'No import issues flagged yet.';
|
||||
target.classList.add('muted');
|
||||
return;
|
||||
}
|
||||
target.classList.remove('muted');
|
||||
target.innerHTML = items.map((item) => {
|
||||
const status = String(item.status || 'open');
|
||||
const resolved = status === 'resolved';
|
||||
const history = (item.notes_history || []).slice(-3).reverse().map((entry) => '<li><strong>' + escapeHtml(entry.issue_type || 'issue') + ':</strong> ' + escapeHtml(entry.notes || '') + '</li>').join('');
|
||||
const sourceLink = item.recipe_url ? '<a href="' + escapeHtml(item.recipe_url) + '" target="_blank" rel="noreferrer">Open recipe source</a>' : '<span>No source URL saved</span>';
|
||||
const recipeId = item.kitchenowl_recipe_id ? '<span>KitchenOwl #' + escapeHtml(item.kitchenowl_recipe_id) + '</span>' : '';
|
||||
const resolveButton = resolved ? '' : '<button type="button" class="ghost" data-import-flag-resolve="' + escapeHtml(item.id) + '">Mark resolved</button>';
|
||||
return `
|
||||
<article class="failure-card" data-import-flag-id="${escapeHtml(item.id)}">
|
||||
<div class="suggestion-top">
|
||||
<div>
|
||||
<p class="section-label">${escapeHtml(item.issue_type || 'issue')}</p>
|
||||
<h3>${escapeHtml(item.recipe_title || item.recipe_url || 'KitchenOwl recipe issue')}</h3>
|
||||
</div>
|
||||
<p class="pill ${resolved ? 'status-resolved' : 'status-open'}">${escapeHtml(status)}</p>
|
||||
</div>
|
||||
<p>${escapeHtml(item.notes || '')}</p>
|
||||
<div class="chip-list">
|
||||
${recipeId}
|
||||
<span>Flags: ${escapeHtml(item.flag_count || 1)}</span>
|
||||
<span>Last flagged: ${escapeHtml(item.last_flagged_at || item.updated_at || item.created_at || '')}</span>
|
||||
</div>
|
||||
<div class="flag-meta">${sourceLink}</div>
|
||||
<ul class="flag-history">${history || '<li>No extra notes logged yet.</li>'}</ul>
|
||||
<div class="action-row">${resolveButton}</div>
|
||||
</article>
|
||||
`;
|
||||
}).join('');
|
||||
}
|
||||
|
||||
|
||||
async function loadDashboard() {
|
||||
const data = await fetchJson('/api/dashboard/summary');
|
||||
const counts = data.counts || {};
|
||||
@@ -100,6 +152,7 @@ async function loadDashboard() {
|
||||
setText('count-rejected', counts.rejected || 0);
|
||||
setText('count-plans', counts.plans || 0);
|
||||
setText('count-failed-searches', counts.failed_searches || 0);
|
||||
setText('count-import-flags', counts.open_import_flags || 0);
|
||||
setText('backend-note', 'Storage backend: ' + data.backend);
|
||||
|
||||
const blacklist = document.getElementById('blacklist-items');
|
||||
@@ -122,6 +175,7 @@ async function loadDashboard() {
|
||||
|
||||
renderSuggestionCards(document.getElementById('suggestion-list'), data.recent_suggestions || []);
|
||||
renderFailureCards(document.getElementById('failed-search-preview'), data.recent_failed_searches || []);
|
||||
renderImportFlagCards(document.getElementById('import-flag-preview'), data.recent_import_flags || []);
|
||||
}
|
||||
|
||||
async function handleDecision(event) {
|
||||
@@ -132,13 +186,34 @@ async function handleDecision(event) {
|
||||
const decision = button.dataset.decision;
|
||||
if (!suggestionId || !decision) return;
|
||||
const failurePanel = card ? card.closest('[data-failure-results]') : null;
|
||||
button.disabled = true;
|
||||
const buttons = card ? Array.from(card.querySelectorAll('button')) : [button];
|
||||
const originalText = button.textContent;
|
||||
const workingText = decision === 'approve'
|
||||
? 'Adding to KitchenOwl…'
|
||||
: (decision === 'later' ? 'Saving…' : 'Rejecting…');
|
||||
buttons.forEach((entry) => { entry.disabled = true; });
|
||||
button.textContent = workingText;
|
||||
setStatusMessage(workingText, 'muted');
|
||||
try {
|
||||
const data = await fetchJson('/api/suggestions/' + encodeURIComponent(suggestionId) + '/decision', {
|
||||
method: 'POST',
|
||||
headers: {'Content-Type': 'application/json'},
|
||||
body: JSON.stringify({decision, import_to_kitchenowl: decision === 'approve'}),
|
||||
});
|
||||
|
||||
const suggestion = data.suggestion || {};
|
||||
const title = suggestion.title || 'Recipe';
|
||||
const kitchenowl = data.kitchenowl || {};
|
||||
if (decision === 'approve') {
|
||||
const outcome = kitchenowl.status || suggestion.kitchenowl_status || 'updated';
|
||||
const verb = outcome === 'duplicate' ? 'already exists in' : 'added to';
|
||||
setStatusMessage(title + ' ' + verb + ' KitchenOwl. Status: ' + outcome + '.', 'pill pill-good');
|
||||
} else if (decision === 'later') {
|
||||
setStatusMessage(title + ' saved for later review.', 'pill pill-good');
|
||||
} else {
|
||||
setStatusMessage(title + ' rejected.', 'pill pill-good');
|
||||
}
|
||||
|
||||
await loadDashboard();
|
||||
if (failurePanel && card) {
|
||||
card.remove();
|
||||
@@ -165,12 +240,29 @@ async function handleDecision(event) {
|
||||
}
|
||||
return data;
|
||||
} catch (error) {
|
||||
setStatusMessage('KitchenOwl action failed: ' + error.message, 'pill status-open');
|
||||
alert(error.message);
|
||||
} finally {
|
||||
button.disabled = false;
|
||||
buttons.forEach((entry) => { entry.disabled = false; });
|
||||
button.textContent = originalText;
|
||||
}
|
||||
}
|
||||
|
||||
async function createImportFlag(payload) {
|
||||
return fetchJson('/api/import-flags', {
|
||||
method: 'POST',
|
||||
headers: {'Content-Type': 'application/json'},
|
||||
body: JSON.stringify(payload),
|
||||
});
|
||||
}
|
||||
|
||||
|
||||
async function loadImportsInto(target) {
|
||||
const data = await fetchJson('/api/import-flags');
|
||||
renderImportFlagCards(target, data.items || []);
|
||||
}
|
||||
|
||||
|
||||
async function loadSuggestionsInto(target) {
|
||||
const data = await fetchJson('/api/suggestions');
|
||||
renderSuggestionCards(target, data.items || []);
|
||||
@@ -232,6 +324,33 @@ function wireDashboard() {
|
||||
await handleDecision(event);
|
||||
return;
|
||||
}
|
||||
if (event.target.matches('button[data-flag-suggestion]')) {
|
||||
const button = event.target;
|
||||
const suggestionId = button.dataset.flagSuggestion;
|
||||
const issueType = window.prompt('What kind of import issue is this? (formatting, ingredients, timings, duplicate, other)', 'formatting');
|
||||
if (!issueType) return;
|
||||
const notes = window.prompt('What went wrong with this import?');
|
||||
if (!notes) return;
|
||||
await createImportFlag({suggestion_id: suggestionId, issue_type: issueType, notes});
|
||||
await loadDashboard();
|
||||
const importsTarget = document.getElementById('import-flags-list');
|
||||
if (importsTarget) await loadImportsInto(importsTarget);
|
||||
return;
|
||||
}
|
||||
if (event.target.matches('button[data-import-flag-resolve]')) {
|
||||
const button = event.target;
|
||||
const importFlagId = button.dataset.importFlagResolve;
|
||||
const resolutionNotes = window.prompt('Optional resolution note:', '');
|
||||
await fetchJson('/api/import-flags/' + encodeURIComponent(importFlagId) + '/resolve', {
|
||||
method: 'POST',
|
||||
headers: {'Content-Type': 'application/json'},
|
||||
body: JSON.stringify({resolution_notes: resolutionNotes || ''}),
|
||||
});
|
||||
await loadDashboard();
|
||||
const importsTarget = document.getElementById('import-flags-list');
|
||||
if (importsTarget) await loadImportsInto(importsTarget);
|
||||
return;
|
||||
}
|
||||
if (event.target.matches('button[data-failure-open]')) {
|
||||
const button = event.target;
|
||||
const failureId = button.dataset.failureOpen;
|
||||
@@ -435,6 +554,42 @@ function wirePlanner() {
|
||||
});
|
||||
}
|
||||
|
||||
function wireImports() {
|
||||
const target = document.getElementById('import-flags-list');
|
||||
if (target) {
|
||||
loadImportsInto(target).catch(() => {
|
||||
target.textContent = 'Could not load import flags.';
|
||||
});
|
||||
}
|
||||
|
||||
const form = document.getElementById('import-flag-form');
|
||||
if (!form) return;
|
||||
form.addEventListener('submit', async (event) => {
|
||||
event.preventDefault();
|
||||
const urlEl = document.getElementById('import-flag-url');
|
||||
const titleEl = document.getElementById('import-flag-title');
|
||||
const typeEl = document.getElementById('import-flag-type');
|
||||
const notesEl = document.getElementById('import-flag-notes');
|
||||
const payload = {
|
||||
recipe_url: urlEl ? urlEl.value.trim() : '',
|
||||
recipe_title: titleEl ? titleEl.value.trim() : '',
|
||||
issue_type: typeEl ? typeEl.value : 'formatting',
|
||||
notes: notesEl ? notesEl.value.trim() : '',
|
||||
};
|
||||
if (!payload.notes) {
|
||||
alert('Tell me what is wrong with the import first.');
|
||||
return;
|
||||
}
|
||||
await createImportFlag(payload);
|
||||
if (urlEl) urlEl.value = '';
|
||||
if (titleEl) titleEl.value = '';
|
||||
if (notesEl) notesEl.value = '';
|
||||
await loadDashboard();
|
||||
if (target) await loadImportsInto(target);
|
||||
});
|
||||
}
|
||||
|
||||
|
||||
function wireFailures() {
|
||||
const target = document.getElementById('failures-list');
|
||||
if (target) {
|
||||
@@ -462,6 +617,7 @@ loadDashboard().catch(() => {});
|
||||
wireDashboard();
|
||||
wireSearch();
|
||||
wirePlanner();
|
||||
wireImports();
|
||||
wireFailures();
|
||||
if (document.getElementById('search-results')) {
|
||||
loadSuggestionsInto(document.getElementById('search-results')).catch(() => {});
|
||||
|
||||
Reference in New Issue
Block a user