All files / src/composables useAdminAttentionCounts.js

89% Statements 89/100
80% Branches 20/25
75% Functions 3/4
89% Lines 89/100

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 1161x 1x 1x 1x 1x 1x 1x 1x           1x   1x   1x 1x 1x 1x 1x 1x 1x 1x   1x   1x   1x 29x 29x   29x 7x 1x 1x 1x 6x 6x 6x 6x 6x 6x 6x 7x 1x 1x 5x 5x 7x 7x 7x 7x 7x 7x 7x 7x 1x 1x 1x 1x 7x   29x           29x 35x       35x   29x 29x 3x 3x 3x 1x 1x 1x 1x 3x         3x 1x 1x 1x 1x 3x 1x 1x 1x 1x 3x 29x   29x 29x 29x 29x 29x 29x 29x 29x 29x 29x  
/**
 * useAdminAttentionCounts — powers the red dot + hover tooltip on the
 * global top-nav "Admin" link.
 *
 * Reactive singleton (same pattern as useSupportInbox; not Pinia). Polls
 * `GET /api/admin/attention/counts` every 60s while an admin is logged in.
 * Exposes a `total` ref + a `breakdown` ref the tooltip can render.
 */
 
import { ref, computed } from 'vue';
import { useAuthStore } from '../stores/auth';
import { getApiBaseUrl } from '../config/api';
 
const POLL_MS = 60_000;
 
// ── Module-level singleton state ───────────────────────────────────────────
 
const total = ref(0);
const breakdown = ref({
  invite_requests: 0,
  channel_requests: 0,
  support_inbox: 0,
  content_reports: 0,
});
const lastError = ref(null);
 
let pollHandle = null;
 
// ── Public API ─────────────────────────────────────────────────────────────
 
export function useAdminAttentionCounts() {
  const authStore = useAuthStore();
  const isAdmin = computed(() => authStore.isAdmin.value);
 
  async function fetchCounts() {
    if (!isAdmin.value) {
      total.value = 0;
      return;
    }
    try {
      const resp = await fetch(
        `${getApiBaseUrl()}/api/admin/attention/counts`,
        {
          headers: authStore.getAuthHeaders(),
        }
      );
      if (!resp.ok) {
        throw new Error(`HTTP ${resp.status}`);
      }
      const data = await resp.json();
      breakdown.value = {
        invite_requests: data.invite_requests || 0,
        channel_requests: data.channel_requests || 0,
        support_inbox: data.support_inbox || 0,
        content_reports: data.content_reports || 0,
      };
      total.value = data.total || 0;
      lastError.value = null;
    } catch (e) {
      // Badge is best-effort — log and leave the previous count visible.
      console.warn('useAdminAttentionCounts: fetch failed', e);
      lastError.value = e.message;
    }
  }
 
  function startPolling() {
    if (pollHandle) return;
    fetchCounts();
    pollHandle = setInterval(fetchCounts, POLL_MS);
  }
 
  function stopPolling() {
    if (pollHandle) {
      clearInterval(pollHandle);
      pollHandle = null;
    }
  }
 
  // Pluralizing helper for the tooltip — keeps the template clean.
  const tooltip = computed(() => {
    const parts = [];
    const b = breakdown.value;
    if (b.invite_requests > 0) {
      parts.push(
        `${b.invite_requests} invite request${b.invite_requests === 1 ? '' : 's'}`
      );
    }
    if (b.channel_requests > 0) {
      parts.push(
        `${b.channel_requests} channel request${b.channel_requests === 1 ? '' : 's'}`
      );
    }
    if (b.support_inbox > 0) {
      parts.push(
        `${b.support_inbox} unread support message${b.support_inbox === 1 ? '' : 's'}`
      );
    }
    if (b.content_reports > 0) {
      parts.push(
        `${b.content_reports} content report${b.content_reports === 1 ? '' : 's'}`
      );
    }
    return parts.length ? parts.join(' · ') : 'No items need attention';
  });
 
  return {
    total,
    breakdown,
    tooltip,
    lastError,
    fetchCounts,
    startPolling,
    stopPolling,
  };
}