feat(khira): add real-time SSE live updates and Kanban drag-and-drop

This commit is contained in:
2026-10-27 18:34:24 +05:30
parent 461178b04f
commit db9c1a5840
7 changed files with 467 additions and 8 deletions
+136 -4
View File
@@ -380,7 +380,7 @@ function renderT(anim){
if(S.view==='board'){
put('#board',COLS.map(([k,c])=>{
const rows=f.filter(t=>st(t)===k||(!STAT[st(t)]&&k==='open'));
return`<section class="col"><h3><i style="--c:${c}"></i>${STAT[k]}<small>${rows.length}</small></h3>${rows.map((t,i)=>{
return`<section class="col" data-col="${k}"><h3><i style="--c:${c}"></i>${STAT[k]}<small>${rows.length}</small></h3>${rows.map((t,i)=>{
const p=pri(t),n=(t.remarks||[]).length;
const avElem=avHtml(t.assignee||{avatar:t.assigned_to_avatar||'👤',role:t.assigned_to_role,username:t.assigned_to},26);
const isSel=S.bulkSelected.has(t.id);
@@ -400,7 +400,7 @@ function renderT(anim){
const tagsHtml=(t.tags||[]).length?`<div class="tag-list" style="margin-top:6px">${t.tags.map(tg=>`<span class="tag-pill">${esc(tg)}</span>`).join('')}</div>`:'';
return`<article class="card p-${p.toLowerCase()} ${isSel?'selected':''}" tabindex="0" role="button" data-t="${esc(t.id)}" style="--i:${i}">
return`<article class="card p-${p.toLowerCase()} ${isSel?'selected':''}" draggable="true" tabindex="0" role="button" data-t="${esc(t.id)}" style="--i:${i}">
${chkBox}
<div class="r"><span class="id">${esc(t.id)}</span><span class="tag">${t.ticket_type==='general'?'General':'App'}</span><span class="pb ${p.toLowerCase()}">${p}</span></div>
<h4>${esc(t.title)}</h4>
@@ -410,6 +410,8 @@ function renderT(anim){
</article>`;
}).join('')||'<div class="empty">No tickets</div>'}</section>`;
}).join(''));
initBoardDragDrop();
}else{
const dateGroups=['Today','Yesterday','This Week','Earlier this Month','Older'];
const grouped={};
@@ -438,6 +440,63 @@ function renderT(anim){
put('#title','Tickets');
}
function initBoardDragDrop(){
const cards = $$('#board .card');
const cols = $$('#board .col');
cards.forEach(card => {
card.addEventListener('dragstart', e => {
e.dataTransfer.setData('text/plain', card.dataset.t);
e.dataTransfer.effectAllowed = 'move';
card.classList.add('dragging');
});
card.addEventListener('dragend', () => {
card.classList.remove('dragging');
});
});
cols.forEach(col => {
col.addEventListener('dragover', e => {
e.preventDefault();
e.dataTransfer.dropEffect = 'move';
col.classList.add('drop-hover');
});
col.addEventListener('dragleave', e => {
if (!col.contains(e.relatedTarget)) {
col.classList.remove('drop-hover');
}
});
col.addEventListener('drop', async e => {
e.preventDefault();
col.classList.remove('drop-hover');
const tid = e.dataTransfer.getData('text/plain');
const targetCol = col.dataset.col;
if (!tid || !targetCol) return;
const t = S.t.find(x => x.id === tid);
if (!t) return;
const curSt = st(t);
if (curSt === targetCol) return;
if (targetCol === 'completed') {
S.tid = tid;
doneD();
} else if (targetCol === 'hold') {
S.tid = tid;
holdD();
} else if (targetCol === 'progress') {
await api(`/khira/api/tickets/${encodeURIComponent(tid)}/resume`, 'POST', { remarks: 'Moved to In Progress' });
toast(`Ticket ${tid} moved to In Progress`);
await refresh(false);
} else if (targetCol === 'open') {
await api(`/khira/api/tickets/${encodeURIComponent(tid)}/resume`, 'POST', { remarks: 'Moved to Open' });
toast(`Ticket ${tid} moved to Open`);
await refresh(false);
}
});
});
}
/* ── Errors View ── */
function renderE(anim){
const s=S.es;
@@ -973,7 +1032,8 @@ async function remark(){
if(!b){toast('Write a remark first','err');return}
const r=await api(`/khira/api/tickets/${encodeURIComponent(S.tid)}/remarks`,'POST',{text:b,body:b,links:[]});
if(r._f){toast(r.error,'err');return}
toast('Remark posted');await refresh();sheet(S.tid);
ta.value='';
toast('Remark posted');await refresh(false);sheet(S.tid);
}
const holdD=()=>dlg({title:'Put ticket on hold',sub:'Say what is blocking this ticket.',body:fld('reason','Reason',{t:'area',rows:4,req:1}),ok:'Hold ticket',go:v=>act('hold',{reason:v.reason},'Ticket on hold')});
@@ -1821,6 +1881,77 @@ $('#btnForgot')?.addEventListener('click',()=>{
});
});
/* ── Realtime SSE Engine ── */
let sseSource = null;
let sseReconnectTimer = null;
function updateLiveStatus(online) {
const pill = $('#livePill');
if (!pill) return;
pill.classList.toggle('online', !!online);
pill.classList.toggle('offline', !online);
const lbl = pill.querySelector('.live-lbl');
if (lbl) lbl.textContent = online ? 'Live' : 'Reconnecting...';
}
function initRealtime() {
if (sseSource) {
try { sseSource.close(); } catch(e) {}
sseSource = null;
}
const un = S.u?.username || ls('khira_active_user');
const url = un ? `/khira/api/events?u=${encodeURIComponent(un)}` : '/khira/api/events';
sseSource = new EventSource(url);
sseSource.addEventListener('connected', () => {
updateLiveStatus(true);
});
sseSource.addEventListener('message', async (e) => {
try {
const evt = JSON.parse(e.data);
handleRealtimeEvent(evt);
} catch(err) {
console.warn('Realtime event parse error', err);
}
});
sseSource.onerror = () => {
updateLiveStatus(false);
if (sseSource && sseSource.readyState === EventSource.CLOSED) {
clearTimeout(sseReconnectTimer);
sseReconnectTimer = setTimeout(initRealtime, 3000);
}
};
}
async function handleRealtimeEvent(evt) {
const tId = evt.ticket_id;
// 1. Refresh background datasets silently without layout flicker
await refresh(false);
// 2. If the user currently has the affected ticket sheet open:
if (S.tid && tId && S.tid === tId && $('#sheet')?.open) {
sheet(S.tid);
}
// 3. If currently on Analytics view:
if (S.sec === 'analytics') {
renderA(false);
}
// 4. Subtle visual highlight on affected card
if (tId) {
const card = document.querySelector(`.card[data-t="${tId}"]`);
if (card) {
card.classList.add('live-flash');
setTimeout(() => card.classList.remove('live-flash'), 1200);
}
}
}
/* ── App Initialization ── */
async function boot(u){
S.u=u;
@@ -1828,9 +1959,10 @@ async function boot(u){
$('#app').hidden=false;
updateMe(u);
initKubeLens();
initRealtime();
await refresh(true);
clearInterval(pollId);
pollId=setInterval(()=>refresh(false),8000);
pollId=setInterval(()=>refresh(false),30000);
}
(async function init(){
+9 -3
View File
@@ -9,7 +9,7 @@
<link rel="preconnect" href="https://fonts.googleapis.com">
<link rel="preconnect" href="https://fonts.gstatic.com" crossorigin>
<link href="https://fonts.googleapis.com/css2?family=Roboto+Flex:wght@400;500;600;700&family=Roboto+Mono:wght@400;500&display=swap" rel="stylesheet">
<link rel="stylesheet" href="/khira/styles.css?v=3">
<link rel="stylesheet" href="/khira/styles.css?v=4">
<script>try{document.documentElement.dataset.theme=localStorage.khira_theme||'dark'}catch(e){}</script>
</head>
<body>
@@ -71,7 +71,13 @@
<div class="main">
<header class="bar">
<h1 id="title">Tickets</h1>
<div style="display:flex;align-items:center">
<h1 id="title">Tickets</h1>
<div id="livePill" class="live-status-pill online" title="Real-time connected: Live updates streaming without refresh">
<span class="live-dot"><span class="live-pulse"></span></span>
<span class="live-lbl">Live</span>
</div>
</div>
<span class="sp"></span>
<div id="adminDbTools" class="tr" hidden>
<button class="chip db-btn sync" id="btnSyncBackup" title="Sync Primary DB to Backup DB"><svg width="15" height="15" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" style="margin-right:5px;vertical-align:middle"><polyline points="23 4 23 10 17 10"/><polyline points="1 20 1 14 7 14"/><path d="M3.51 9a9 9 0 0 1 14.85-3.36L23 10M1 14l4.64 4.36A9 9 0 0 0 20.49 15"/></svg><span class="btn-txt">Backup DB</span></button>
@@ -240,6 +246,6 @@
<dialog id="sheet"></dialog>
<dialog id="dlg"></dialog>
<div class="snack" id="snack" role="status"></div>
<script src="/khira/app.js?v=3" defer></script>
<script src="/khira/app.js?v=4" defer></script>
</body>
</html>
+75
View File
@@ -2319,3 +2319,78 @@ dd { word-break: break-word; }
}
.scorecard-item .val.green { color: #10b981; }
.scorecard-item .val.red { color: #ef4444; }
/* ── Realtime Live Status Indicator ── */
.live-status-pill {
display: inline-flex;
align-items: center;
gap: 7px;
padding: 4px 10px;
border-radius: 999px;
background: rgba(16, 185, 129, 0.12);
border: 1px solid rgba(52, 211, 153, 0.3);
color: #34d399;
font-size: 11px;
font-weight: 600;
letter-spacing: 0.04em;
text-transform: uppercase;
margin-left: 14px;
transition: all 0.3s ease;
user-select: none;
}
.live-status-pill.offline {
background: rgba(245, 158, 11, 0.12);
border-color: rgba(251, 191, 36, 0.3);
color: #fbbf24;
}
.live-dot {
position: relative;
width: 8px;
height: 8px;
border-radius: 50%;
background: #10b981;
}
.live-status-pill.offline .live-dot {
background: #f59e0b;
}
.live-pulse {
position: absolute;
top: -3px;
left: -3px;
right: -3px;
bottom: -3px;
border-radius: 50%;
background: rgba(16, 185, 129, 0.5);
animation: live-pulse-anim 2s infinite ease-out;
}
.live-status-pill.offline .live-pulse {
background: rgba(245, 158, 11, 0.5);
}
@keyframes live-pulse-anim {
0% { transform: scale(0.8); opacity: 0.9; }
100% { transform: scale(2.2); opacity: 0; }
}
/* ── Realtime Flash Animation on Board/List ── */
@keyframes card-flash-anim {
0% { box-shadow: 0 0 0 2px #38bdf8, 0 0 20px rgba(56, 189, 248, 0.4); transform: scale(1.02); }
100% { box-shadow: none; transform: scale(1); }
}
.card.live-flash, .li.live-flash {
animation: card-flash-anim 1.2s ease-out;
}
/* ── HTML5 Drag & Drop Styling ── */
.card[draggable="true"] {
cursor: grab;
}
.card.dragging {
opacity: 0.45;
transform: scale(0.96);
cursor: grabbing;
}
.col.drop-hover {
background: rgba(56, 189, 248, 0.08) !important;
border: 1px dashed rgba(56, 189, 248, 0.5) !important;
border-radius: 18px !important;
}