File size: 11,457 Bytes
845d6d1
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
/* ═══════════════════════════════════════════════════════════════════════════
   ResearchIT β€” Client-Side JavaScript
   Theme toggle, toasts, progressive loading messages, optimistic UI,
   abstract expand/collapse, HTMX event handlers.
   ═══════════════════════════════════════════════════════════════════════════ */

// ── Theme Toggle ─────────────────────────────────────────────────────────────

function toggleTheme() {
  const html = document.documentElement;
  const current = html.getAttribute('data-theme');
  const next = current === 'dark' ? 'light' : 'dark';
  html.setAttribute('data-theme', next);
  localStorage.setItem('researchit-theme', next);
  updateThemeIcons();
}

function updateThemeIcons() {
  const theme = document.documentElement.getAttribute('data-theme');
  // Desktop icon
  document.querySelectorAll('.theme-icon-sun').forEach(el => {
    el.classList.toggle('hidden', theme !== 'dark');
  });
  document.querySelectorAll('.theme-icon-moon').forEach(el => {
    el.classList.toggle('hidden', theme !== 'light');
  });
  // Mobile text
  document.querySelectorAll('.theme-text-dark').forEach(el => {
    el.classList.toggle('hidden', theme !== 'dark');
  });
  document.querySelectorAll('.theme-text-light').forEach(el => {
    el.classList.toggle('hidden', theme !== 'light');
  });
}

// ── Toast Notification System ────────────────────────────────────────────────

function showToast(message, type = 'info', duration = 3000) {
  const container = document.getElementById('toast-container');
  if (!container) return;

  const iconMap = {
    success: 'βœ“',
    error: 'βœ•',
    info: 'β„Ή',
  };

  const toast = document.createElement('div');
  toast.className = `rit-toast rit-toast-${type}`;
  toast.innerHTML = `
    <span class="rit-toast-icon">${iconMap[type] || 'β„Ή'}</span>
    <span>${message}</span>
  `;

  container.appendChild(toast);

  setTimeout(() => {
    toast.classList.add('toast-exit');
    toast.addEventListener('animationend', () => toast.remove());
  }, duration);
}

// ── Progressive Loading Messages ─────────────────────────────────────────────

class ProgressMessages {
  constructor(containerId, messages) {
    this.container = document.getElementById(containerId);
    this.messages = messages || [
      'Encoding your query with BGE-M3…',
      'Searching across 1.6M papers…',
      'Ranking the best matches for you…',
      'Almost there β€” curating results…',
    ];
    this.index = 0;
    this.timer = null;
  }

  start(intervalMs = 2500) {
    if (!this.container) return;
    this.index = 0;
    this._show();
    this.timer = setInterval(() => {
      this.index = Math.min(this.index + 1, this.messages.length - 1);
      this._show();
    }, intervalMs);
  }

  stop() {
    if (this.timer) {
      clearInterval(this.timer);
      this.timer = null;
    }
  }

  _show() {
    if (!this.container) return;
    this.container.innerHTML = `
      <span class="rit-progress-msg">${this.messages[this.index]}
        <span class="rit-progress-dots"><span></span><span></span><span></span></span>
      </span>
    `;
  }
}

// Global progress message instances
let searchProgress = null;
let recProgress = null;

// ── Abstract Expand / Collapse ───────────────────────────────────────────────

function toggleAbstract(btn) {
  const card = btn.closest('.paper-card');
  if (!card) return;
  const abstract = card.querySelector('.abstract-text');
  if (!abstract) return;

  const isExpanded = abstract.classList.contains('expanded');
  abstract.classList.toggle('expanded');
  btn.textContent = isExpanded ? '↓ Show more' : '↑ Show less';
}

// ── Coming Soon Click Handler ────────────────────────────────────────────────

function handleComingSoon(e) {
  e.preventDefault();
  e.stopPropagation();
  showToast('This feature is coming soon!', 'info', 2500);
}

// ── Search Loading State ─────────────────────────────────────────────────────

function initSearchHandlers() {
  const searchForm = document.getElementById('search-form');
  if (!searchForm) return;

  searchProgress = new ProgressMessages('search-progress-msgs', [
    'Rewriting query with Groq AI…',
    'Encoding with BGE-M3 embeddings…',
    'Searching Qdrant + Zilliz vectors…',
    'Fusing results with RRF…',
    'Reranking the best matches…',
    'Almost there…',
  ]);
}

// ── Recommendation Loading State ─────────────────────────────────────────────

function initRecHandlers() {
  recProgress = new ProgressMessages('rec-progress-msgs', [
    'Analyzing your interest clusters…',
    'Searching across 1.6M papers…',
    'Running LightGBM reranker…',
    'Diversifying with MMR…',
    'Curating your personal feed…',
  ]);
}

// ── HTMX Global Event Handlers ───────────────────────────────────────────────

document.addEventListener('DOMContentLoaded', function () {
  updateThemeIcons();
  initSearchHandlers();
  initRecHandlers();

  // ── Before HTMX Request ──────────────────────────────────────────────────
  document.body.addEventListener('htmx:beforeRequest', function (evt) {
    const target = evt.detail.target;
    if (!target) return;

    // Search results loading
    if (target.id === 'search-results') {
      const loadingEl = document.getElementById('search-loading');
      const resultsEl = document.getElementById('search-results');
      if (loadingEl) loadingEl.classList.remove('hidden');
      if (resultsEl) resultsEl.classList.add('opacity-20', 'pointer-events-none');
      // Hide recs
      const recWrapper = document.getElementById('rec-wrapper');
      if (recWrapper) recWrapper.classList.add('hidden');
      // Swap button text
      document.querySelectorAll('.search-btn-text').forEach(el => el.classList.add('hidden'));
      document.querySelectorAll('.search-btn-loading').forEach(el => el.classList.remove('hidden'));
      // Start progress messages
      if (searchProgress) searchProgress.start(2000);
    }

    // Recommendations loading
    if (target.id === 'rec-section') {
      if (recProgress) recProgress.start(2000);
    }
  });

  // ── After HTMX Request (success or error) ────────────────────────────────
  document.body.addEventListener('htmx:afterRequest', function (evt) {
    const target = evt.detail.target;
    if (!target) return;

    // Search results loaded
    if (target.id === 'search-results') {
      const loadingEl = document.getElementById('search-loading');
      const resultsEl = document.getElementById('search-results');
      if (loadingEl) loadingEl.classList.add('hidden');
      if (resultsEl) resultsEl.classList.remove('opacity-20', 'pointer-events-none');
      // Restore button
      document.querySelectorAll('.search-btn-text').forEach(el => el.classList.remove('hidden'));
      document.querySelectorAll('.search-btn-loading').forEach(el => el.classList.add('hidden'));
      // Stop progress messages
      if (searchProgress) searchProgress.stop();
    }

    // Recommendations loaded
    if (target.id === 'rec-section') {
      if (recProgress) recProgress.stop();
    }
  });

  // ── HTMX Error Handler ─────────────────────────────────────────────────
  document.body.addEventListener('htmx:responseError', function (evt) {
    const target = evt.detail.target;
    if (target) {
      // Show friendly error for search/rec failures
      if (target.id === 'search-results' || target.id === 'rec-section') {
        target.innerHTML = `
          <div class="rit-error-state">
            <div class="text-3xl mb-3 opacity-70">⚑</div>
            <p class="font-medium mb-1">Our servers are thinking extra hard</p>
            <p class="text-sm opacity-60 mb-4">The request took longer than expected. Please try again.</p>
            <button class="btn btn-primary btn-sm" onclick="location.reload()">
              ↻ Try again
            </button>
          </div>
        `;
      }
      // Stop any progress messages
      if (searchProgress) searchProgress.stop();
      if (recProgress) recProgress.stop();
    }
  });

  // ── Optimistic Save Feedback ─────────────────────────────────────────────
  // After a save action completes, show a toast
  document.body.addEventListener('htmx:afterSwap', function (evt) {
    const target = evt.detail.target;
    if (!target) return;

    // Check if this was a save action (the target id starts with 'actions-')
    if (target.id && target.id.startsWith('actions-')) {
      // Check if it now contains a "saved" state
      const savedBtn = target.querySelector('.rit-btn-save.saved, .btn-success');
      if (savedBtn) {
        savedBtn.classList.add('save-success');
        showToast('Paper saved to your library', 'success', 2500);
      }
    }

    // Card dismissed (card removed from DOM)
    if (target.id && target.id.startsWith('paper-') && target.innerHTML.trim() === '') {
      showToast('Paper dismissed', 'info', 2000);
    }
  });

  // ── Mobile Menu Toggle ─────────────────────────────────────────────────
  const menuToggle = document.querySelector('.mobile-menu-toggle');
  if (menuToggle) {
    menuToggle.addEventListener('change', function () {
      const menu = this.closest('.flex-none').querySelector('.mobile-menu');
      if (menu) {
        menu.style.display = this.checked ? 'flex' : 'none';
      }
    });
  }

  // ── Set Active Nav Link ────────────────────────────────────────────────
  const path = window.location.pathname;
  document.querySelectorAll('.rit-nav-link, .rit-bottom-nav-item').forEach(link => {
    const href = link.getAttribute('href');
    if (href === path || (href === '/' && path === '/')) {
      link.classList.add('active');
    } else if (href !== '/' && path.startsWith(href)) {
      link.classList.add('active');
    }
  });
});