Spaces:
Paused
Paused
File size: 6,667 Bytes
62be87c | 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 | // Admin panel JavaScript functionality
$(document).ready(function() {
// Setup CSRF token for AJAX requests
$.ajaxSetup({
beforeSend: function(xhr, settings) {
if (!/^(GET|HEAD|OPTIONS|TRACE)$/i.test(settings.type) && !this.crossDomain) {
xhr.setRequestHeader("X-CSRFToken", $('meta[name="csrf-token"]').attr('content'));
}
}
});
// Flash message auto-dismiss
setTimeout(function() {
$('.flash').fadeOut();
}, 5000);
// Confirm delete actions
$('.delete-btn').on('click', function(e) {
if (!confirm('Are you sure you want to delete this item?')) {
e.preventDefault();
}
});
// Auto-refresh dashboard stats
if (window.location.pathname === '/admin' || window.location.pathname === '/admin/') {
setInterval(refreshDashboardStats, 30000); // Refresh every 30 seconds
}
});
function refreshDashboardStats() {
$.get('/admin/api/stats', function(data) {
if (data.total_users !== undefined) {
$('.stat-number').eq(0).text(data.total_users);
$('.stat-number').eq(1).text(data.active_users);
$('.stat-number').eq(2).text(data.disabled_users);
$('.stat-number').eq(3).text(data.usage_stats.total_requests || 0);
}
}).fail(function() {
console.log('Failed to refresh dashboard stats');
});
}
// User management functions
function enableUser(token) {
if (confirm('Are you sure you want to enable this user?')) {
$.post(`/admin/users/${token}/enable`, {})
.done(function(data) {
if (data.success) {
location.reload();
} else {
alert('Error: ' + data.error);
}
})
.fail(function() {
alert('Error: Failed to enable user');
});
}
}
function disableUser(token) {
const reason = prompt('Reason for disabling user:');
if (reason) {
$.post(`/admin/users/${token}/disable`, {
reason: reason
})
.done(function(data) {
if (data.success) {
location.reload();
} else {
alert('Error: ' + data.error);
}
})
.fail(function() {
alert('Error: Failed to disable user');
});
}
}
function deleteUser(token) {
if (confirm('Are you sure you want to delete this user? This action cannot be undone.')) {
$.ajax({
url: `/admin/users/${token}`,
type: 'DELETE'
})
.done(function(data) {
if (data.success) {
location.reload();
} else {
alert('Error: ' + data.error);
}
})
.fail(function() {
alert('Error: Failed to delete user');
});
}
}
function rotateUserToken(token) {
if (confirm('Are you sure you want to rotate this user\'s token? The old token will be invalidated.')) {
$.post(`/admin/users/${token}/rotate`, {})
.done(function(data) {
if (data.success) {
alert('Token rotated successfully. New token: ' + data.new_token);
location.reload();
} else {
alert('Error: ' + data.error);
}
})
.fail(function() {
alert('Error: Failed to rotate token');
});
}
}
// Bulk operations
function bulkRefreshQuotas() {
if (confirm('Are you sure you want to refresh quotas for all users?')) {
$.post('/admin/bulk/refresh-quotas', {})
.done(function(data) {
if (data.success) {
alert(`Successfully refreshed quotas for ${data.affected_users} users`);
location.reload();
} else {
alert('Error: ' + data.error);
}
})
.fail(function() {
alert('Error: Failed to refresh quotas');
});
}
}
// Copy to clipboard functionality
function copyToClipboard(text) {
navigator.clipboard.writeText(text).then(function() {
// Show temporary success message
const msg = $('<div class="copy-success">Copied to clipboard!</div>');
$('body').append(msg);
setTimeout(function() {
msg.fadeOut(function() { msg.remove(); });
}, 2000);
}).catch(function(err) {
console.error('Failed to copy to clipboard:', err);
alert('Failed to copy to clipboard');
});
}
// Form validation
function validateUserForm() {
const nickname = $('#nickname').val().trim();
const openaiLimit = $('#openai_limit').val();
const anthropicLimit = $('#anthropic_limit').val();
if (!nickname) {
alert('Please enter a nickname');
return false;
}
if (openaiLimit && (isNaN(openaiLimit) || parseInt(openaiLimit) < 0)) {
alert('OpenAI limit must be a positive number');
return false;
}
if (anthropicLimit && (isNaN(anthropicLimit) || parseInt(anthropicLimit) < 0)) {
alert('Anthropic limit must be a positive number');
return false;
}
return true;
}
// Table sorting
function sortTable(columnIndex) {
const table = document.querySelector('.users-table table');
const tbody = table.querySelector('tbody');
const rows = Array.from(tbody.querySelectorAll('tr'));
rows.sort((a, b) => {
const aText = a.cells[columnIndex].textContent.trim();
const bText = b.cells[columnIndex].textContent.trim();
// Try to parse as numbers first
const aNum = parseFloat(aText);
const bNum = parseFloat(bText);
if (!isNaN(aNum) && !isNaN(bNum)) {
return aNum - bNum;
}
// Otherwise, sort as strings
return aText.localeCompare(bText);
});
tbody.innerHTML = '';
rows.forEach(row => tbody.appendChild(row));
}
// Search functionality
function filterUsers() {
const searchTerm = $('#user-search').val().toLowerCase();
const rows = $('.users-table tbody tr');
rows.each(function() {
const row = $(this);
const text = row.text().toLowerCase();
if (text.includes(searchTerm)) {
row.show();
} else {
row.hide();
}
});
}
// Real-time search
$('#user-search').on('input', function() {
filterUsers();
});
// Export functionality
function exportUsers() {
window.location.href = '/admin/export/users';
}
function exportEvents() {
window.location.href = '/admin/export/events';
} |