Spaces:
Running
Running
File size: 9,829 Bytes
a62f8b4 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 | // UI Component Builders
class UIComponents {
// Toast notifications
static showToast(message, type = 'info', duration = 3000) {
const toast = document.getElementById('toast');
toast.textContent = message;
toast.className = `toast active ${type}`;
setTimeout(() => {
toast.classList.remove('active');
}, duration);
}
// Modal management
static openModal(modalId) {
const modal = document.getElementById(modalId);
if (modal) {
modal.classList.add('active');
}
}
static closeModal(modalId) {
const modal = document.getElementById(modalId);
if (modal) {
modal.classList.remove('active');
}
}
// Clear form
static clearForm(formId) {
const form = document.getElementById(formId);
if (form) {
form.reset();
}
}
// Create action buttons
static createActionButtons(editCallback, deleteCallback) {
const div = document.createElement('div');
div.className = 'action-buttons';
const editBtn = document.createElement('button');
editBtn.className = 'btn-edit';
editBtn.textContent = '✏️ Edit';
editBtn.onclick = editCallback;
const deleteBtn = document.createElement('button');
deleteBtn.className = 'btn-delete';
deleteBtn.textContent = '🗑️ Delete';
deleteBtn.onclick = deleteCallback;
div.appendChild(editBtn);
div.appendChild(deleteBtn);
return div;
}
// Create priority badge
static createPriorityBadge(priority) {
// Convert int to string label
const priorityMap = {
0: 'Low',
1: 'Medium',
2: 'High',
3: 'Critical'
};
// Handle both int and string priority
const priorityLabel = typeof priority === 'number' ? priorityMap[priority] : priority;
const span = document.createElement('span');
span.className = `priority-badge priority-${(priorityLabel || 'medium').toLowerCase()}`;
span.textContent = priorityLabel || 'Medium';
return span;
}
// Convert snake_case/camelCase key to Title Case header label
static formatHeader(key) {
return key
.replace(/_/g, ' ')
.replace(/\b\w/g, c => c.toUpperCase());
}
// Render categories table with dynamic thead derived from API keys
static renderCategoriesTable(categories) {
const thead = document.getElementById('categoriesTableHead');
const tbody = document.getElementById('categoriesTableBody');
tbody.innerHTML = '';
if (!categories || categories.length === 0) {
thead.innerHTML = '';
tbody.innerHTML = '<tr><td colspan="2" style="text-align: center; color: var(--text-muted);">No categories found</td></tr>';
return;
}
// Derive columns from first item's keys
const columns = Object.keys(categories[0]);
// Render thead
const headRow = document.createElement('tr');
[...columns, 'actions'].forEach(col => {
const th = document.createElement('th');
th.textContent = UIComponents.formatHeader(col);
headRow.appendChild(th);
});
thead.innerHTML = '';
thead.appendChild(headRow);
// Render tbody
categories.forEach(category => {
const row = document.createElement('tr');
columns.forEach(col => {
const td = document.createElement('td');
const value = category[col];
if (value === null || value === undefined) {
td.textContent = '—';
} else if (col === 'created_at') {
td.textContent = new Date(value).toLocaleString();
} else if (col === 'embedding' && typeof value === 'string' && value.length > 50) {
td.textContent = value.substring(0, 50) + '…';
td.title = value;
} else {
td.textContent = value;
}
row.appendChild(td);
});
const actionTd = document.createElement('td');
actionTd.appendChild(
UIComponents.createActionButtons(
() => window.editCategory(category),
() => window.deleteCategory(category.id)
)
);
row.appendChild(actionTd);
tbody.appendChild(row);
});
}
// Render intents table with dynamic thead derived from API keys
// categoriesMap: { [id]: label } for looking up category names
static renderIntentsTable(intents, categoriesMap = {}) {
const thead = document.getElementById('intentsTableHead');
const tbody = document.getElementById('intentsTableBody');
tbody.innerHTML = '';
if (!intents || intents.length === 0) {
thead.innerHTML = '';
tbody.innerHTML = '<tr><td colspan="2" style="text-align: center; color: var(--text-muted);">No intents found</td></tr>';
return;
}
// Derive columns from first item, excluding category_label (null, redundant with category_id lookup)
const excludedCols = new Set(['category_label']);
const columns = Object.keys(intents[0]).filter(k => !excludedCols.has(k));
// Render thead
const headRow = document.createElement('tr');
[...columns, 'actions'].forEach(col => {
const th = document.createElement('th');
th.textContent = UIComponents.formatHeader(col);
headRow.appendChild(th);
});
thead.innerHTML = '';
thead.appendChild(headRow);
// Render tbody
intents.forEach(intent => {
const row = document.createElement('tr');
columns.forEach(col => {
const td = document.createElement('td');
const value = intent[col];
if (col === 'priority') {
td.appendChild(UIComponents.createPriorityBadge(value));
} else if (col === 'category_id') {
td.textContent = categoriesMap[value] || value;
} else if (value === null || value === undefined) {
td.textContent = '—';
} else if (col === 'created_at') {
td.textContent = new Date(value).toLocaleString();
} else if (col === 'embedding' && typeof value === 'string' && value.length > 50) {
td.textContent = value.substring(0, 50) + '…';
td.title = value;
} else {
td.textContent = value;
}
row.appendChild(td);
});
const actionTd = document.createElement('td');
actionTd.appendChild(
UIComponents.createActionButtons(
() => window.editIntent(intent),
() => window.deleteIntent(intent.id)
)
);
row.appendChild(actionTd);
tbody.appendChild(row);
});
}
// Populate category dropdown
static populateCategoryDropdown(categories, selectId = 'intentCategory') {
const select = document.getElementById(selectId);
if (!select) return;
const currentValue = select.value;
select.innerHTML = '<option value="">Select a category</option>';
categories.forEach(category => {
const option = document.createElement('option');
option.value = category.id;
option.textContent = category.label;
select.appendChild(option);
});
if (currentValue) {
select.value = currentValue;
}
}
// Populate filter dropdowns
static populateFilterDropdowns(categories) {
const categoryFilter = document.getElementById('categoryFilter');
if (categoryFilter) {
const currentValue = categoryFilter.value;
categoryFilter.innerHTML = '<option value="">All Categories</option>';
categories.forEach(category => {
const option = document.createElement('option');
option.value = category.id;
option.textContent = category.label;
categoryFilter.appendChild(option);
});
if (currentValue) {
categoryFilter.value = currentValue;
}
}
}
// Update dashboard stats
static updateDashboardStats(categories, intents) {
const criticalCount = intents.filter(i => i.priority === 3 || i.priority === 'Critical').length;
const els = {
totalCategories: categories.length,
totalIntents: intents.length,
criticalCount,
};
Object.entries(els).forEach(([id, val]) => {
const el = document.getElementById(id);
if (el) el.textContent = val;
});
}
// Update status indicator
static updateStatus(connected = true) {
const statusDot = document.querySelector('.status-dot');
const statusText = document.getElementById('statusText');
if (connected) {
statusDot.style.backgroundColor = 'var(--accent-green)';
statusText.textContent = 'Connected';
statusText.style.color = 'var(--text-secondary)';
} else {
statusDot.style.backgroundColor = 'var(--accent-red)';
statusText.textContent = 'Disconnected';
statusText.style.color = 'var(--accent-red)';
}
}
}
|