All files showHistoryPopup.ts

0% Statements 0/148
0% Branches 0/1
0% Functions 0/1
0% Lines 0/148

Press n or j to go to the next uncovered block, b, p or k for the previous block.

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                                                                                                                                                                                                                                                                                                                                                                                 
import { CommandHistory } from './CommandHistory';
 
 
/** gets an instance of the Command History */
const commandHistory = CommandHistory.getInstance();
 
/**
 * Opens a modal that shows the latest commands and auto-refreshes every second.
 * Storage is unbounded; UI renders only a window (latest PAGE items) for speed.
 * 
 * @returns {void}
 */
export function showHistoryPopup():void{
  if (document.getElementById('aac-history-overlay')) return;
 
  // --- overlay & dialog ---
  const overlay = document.createElement('div');
  overlay.id = 'aac-history-overlay';
  overlay.style.position = 'fixed';
  overlay.style.inset = '0';
  overlay.style.background = 'rgba(0,0,0,0.6)';
  overlay.style.display = 'flex';
  overlay.style.justifyContent = 'center';
  overlay.style.alignItems = 'center';
  overlay.style.zIndex = '9999';
 
  const dialog = document.createElement('div');
  dialog.setAttribute('role', 'dialog');
  dialog.setAttribute('aria-modal', 'true');
  dialog.setAttribute('aria-labelledby', 'aac-history-title');
  Object.assign(dialog.style, {
    width: 'min(680px, 92vw)',
    maxHeight: '82vh',
    background: '#fff',
    borderRadius: '14px',
    padding: '16px',
    boxShadow: '0 10px 30px rgba(0,0,0,0.35)',
    display: 'flex',
    flexDirection: 'column',
    gap: '10px',
  } as CSSStyleDeclaration);
 
  const title = document.createElement('h2');
  title.id = 'aac-history-title';
  title.textContent = 'Command History';
  title.style.margin = '0 0 4px 0';
  title.style.fontSize = '1.3rem';
 
  const controls = document.createElement('div');
  Object.assign(controls.style, {
    display: 'flex',
    gap: '8px',
    alignItems: 'center',
    flexWrap: 'wrap',
  } as CSSStyleDeclaration);
 
  const goLatestBtn = btn('Go to latest');
  const clearBtn = btn('Clear');
  const closeBtn = btn('Close');
  controls.append(goLatestBtn, clearBtn, closeBtn);
 
  const listWrap = document.createElement('div');
  Object.assign(listWrap.style, {
    flex: '1',
    overflow: 'auto',
    background: '#f6f7fb',
    border: '1px solid #e4e6ef',
    borderRadius: '10px',
    padding: '8px',
  } as CSSStyleDeclaration);
 
  const list = document.createElement('ul');
  list.setAttribute('aria-live', 'polite');
  Object.assign(list.style, {
    listStyle: 'none',
    margin: '0',
    padding: '0',
    fontSize: '1.1rem',
  } as CSSStyleDeclaration);
  listWrap.appendChild(list);
 
  dialog.append(title, controls, listWrap);
  overlay.appendChild(dialog);
  document.body.appendChild(overlay);
 
  // --- windowed rendering state ---
  const PAGE = 200; // how many latest items to show in the UI
  let total = commandHistory.getSize();
  let windowEnd = total; // exclusive index
  let windowStart = Math.max(0, windowEnd - PAGE);
  let followTail = true; // always show the newest PAGE items
 
  function renderWindow() {
    const items = commandHistory.getSlice(windowStart, windowEnd);
    list.innerHTML = '';
    for (let i = 0; i < items.length; i++) {
      const entry = items[i];
      const li = document.createElement('li');
      
      // Format the command log entry with timestamp, command name, and status
      const time = entry.timestamp.toLocaleTimeString();
      
      li.innerHTML = `
        <span style="color: #6b7280; font-size: 0.9em;">${time}</span>
        <span style="font-weight: 600; margin: 0 8px;">${entry.commandName}</span>
        <span style="font-weight: normal;">${entry.status}</span>
      `;
      
      Object.assign(li.style, {
        padding: '10px 12px',
        margin: '6px 0',
        borderRadius: '8px',
        background: '#fff',
        border: '1px solid #e4e6ef',
        boxShadow: '0 1px 0 rgba(0,0,0,0.03)',
      } as CSSStyleDeclaration);
      list.appendChild(li);
    }
    listWrap.scrollTop = listWrap.scrollHeight; // stick to bottom
  }
 
  // initial paint
  renderWindow();
 
  // --- AUTO-REFRESH EVERY SECOND ---
  const timerId = window.setInterval(() => {
    const newTotal = commandHistory.getSize();
    if (newTotal !== total) {
      total = newTotal;
      if (followTail) {
        windowEnd = total;
        windowStart = Math.max(0, windowEnd - PAGE);
      }
      renderWindow();
    }
  }, 1000);
 
  // --- controls ---
  goLatestBtn.onclick = () => {
    total = commandHistory.getSize();
    windowEnd = total;
    windowStart = Math.max(0, windowEnd - PAGE);
    followTail = true;
    renderWindow();
  };
 
  clearBtn.onclick = () => {
    commandHistory.clear();
    total = 0;
    windowStart = 0;
    windowEnd = 0;
    renderWindow();
  };
 
  function close() {
    clearInterval(timerId); // important: stop the 1s refresh
    overlay.remove();
  }
  closeBtn.onclick = close;
  overlay.addEventListener('click', (e) => {
    if (e.target === overlay) close();
  });
  window.addEventListener(
    'keydown',
    (e) => {
      if (e.key === 'Escape') close();
    },
    { once: true },
  );
 
  function btn(label: string) {
    const b = document.createElement('button');
    b.textContent = label;
    Object.assign(b.style, {
      padding: '8px 12px',
      border: '1px solid #c9cbd6',
      borderRadius: '8px',
      background: '#ffffff',
      cursor: 'pointer',
      fontSize: '0.95rem',
    } as CSSStyleDeclaration);
    return b;
  }
}