// Chats list — единый стиль BuhApp
const ChatsView = {
async render(root) {
root.innerHTML = `
`;
try {
const r = await API.ChatApi.listChats();
const list = document.getElementById('chats-list');
if (!r.chats || r.chats.length === 0) {
list.innerHTML = '💬
Пока нет чатов
Открой карту и напиши кому-нибудь! ';
return;
}
list.className = 'row-list';
list.innerHTML = '';
r.chats.forEach((item) => {
const row = document.createElement('div');
row.className = 'card-b row-list-item';
row.style.padding = '12px';
row.style.cursor = 'pointer';
row.style.display = 'flex';
row.style.gap = '12px';
row.style.alignItems = 'center';
const init = (item.other_name || '?')[0].toUpperCase();
const lastText = item.last_message ? escapeHtml(item.last_message) : 'Нет сообщений';
row.innerHTML = `
${item.other_photo ? '' : init}
${escapeHtml(item.other_name || 'Без имени')}
${item.last_ts ? `
${formatChatTime(item.last_ts)}
` : ''}
${lastText}
${item.unread_count > 0 ? `${item.unread_count}
` : ''}
`;
row.onclick = () => App.openChat(item.chat.id, item.other_id, item.other_name);
list.appendChild(row);
});
} catch (e) {
document.getElementById('chats-list').innerHTML = '⚠️
Ошибка загрузки
Попробуй позже ';
App._toast('Не удалось загрузить чаты', 'err');
}
},
};
function formatChatTime(ts) {
try {
const d = new Date(ts);
const now = new Date();
const diff = (now - d) / 1000;
if (diff < 60) return 'сейчас';
if (diff < 3600) return Math.floor(diff / 60) + 'м';
if (diff < 86400) return Math.floor(diff / 3600) + 'ч';
if (diff < 604800) return Math.floor(diff / 86400) + 'д';
return d.toLocaleDateString('ru-RU', { day: 'numeric', month: 'short' });
} catch { return ''; }
}
window.ChatsView = ChatsView;