import { showToast } from '../components/toast.js';
const API = (url) => fetch('/api' + url, { headers: { Authorization: `Bearer ${localStorage.getItem('token')}` }}).then(r => r.json());
export async function render(container) {
container.innerHTML = `
`;
let offset = 0;
const limit = 50;
async function loadActivity(append = false) {
try {
const items = await API(`/activity?limit=${limit}&offset=${offset}`);
const list = document.getElementById('activityList');
if (!append) list.innerHTML = '';
if (items.length === 0 && offset === 0) {
list.innerHTML = 'No activity yet
Actions will appear here as you use the system.
';
return;
}
const html = items.map(item => {
const time = new Date(item.created_at * 1000);
const timeStr = time.toLocaleDateString('en-US', { month: 'short', day: 'numeric' }) + ' ' +
time.toLocaleTimeString('en-US', { hour: '2-digit', minute: '2-digit' });
const icon = getActionIcon(item.action);
return `
${icon}
${item.user_name || item.user_email || 'System'}
${formatAction(item.action)}
${item.details ? `
${item.details}
` : ''}
${timeStr}
`;
}).join('');
if (append) {
list.insertAdjacentHTML('beforeend', html);
} else {
list.innerHTML = html;
}
document.getElementById('loadMoreBtn').style.display = items.length >= limit ? '' : 'none';
} catch (err) {
showToast(err.message, 'error');
}
}
document.getElementById('loadMoreBtn').onclick = () => {
offset += limit;
loadActivity(true);
};
loadActivity();
}
function getActionIcon(action) {
if (action.includes('DELETE')) return '🗑';
if (action.includes('POST') && action.includes('content')) return '📤';
if (action.includes('POST') && action.includes('provision')) return '🔗';
if (action.includes('POST') && action.includes('assignment')) return '📋';
if (action.includes('alert')) return '🔔';
if (action.includes('PUT')) return '✎';
if (action.includes('POST')) return '➕';
return '📄';
}
function formatAction(action) {
return action
.replace('POST /api/', 'created ')
.replace('PUT /api/', 'updated ')
.replace('DELETE /api/', 'deleted ')
.replace('/provision/pair', 'paired a device')
.replace('/content/remote', 'added remote content')
.replace('/content', 'content')
.replace('/devices/:id', 'device')
.replace('/assignments/device/:deviceId', 'playlist assignment')
.replace('/assignments/:id', 'assignment')
.replace('/layouts', 'layout')
.replace('/widgets', 'widget')
.replace('/schedules', 'schedule')
.replace('/walls', 'video wall')
.replace('alert:device_offline', 'alert: device went offline');
}
export function cleanup() {}