/** * High-Speed Instagram Group Directory Frontend * 48 Groups per page with dynamic "See More" infinite loader */ const PAGE_SIZE = 48; let currentOffset = 0; let currentQuery = ""; let currentType = "All"; let debounceTimer = null; let totalGroupsCount = 0; let loadedGroupsCount = 0; document.addEventListener("DOMContentLoaded", () => { fetchInitialGroups(); setupSearchInput(); initLiveStatsTicker(); }); function setupSearchInput() { const searchInput = document.getElementById("search-input"); const clearBtn = document.getElementById("btn-clear"); searchInput.addEventListener("input", (e) => { currentQuery = e.target.value.trim(); clearBtn.style.display = currentQuery ? "block" : "none"; clearTimeout(debounceTimer); debounceTimer = setTimeout(() => { fetchInitialGroups(); }, 200); }); } function clearSearch() { const searchInput = document.getElementById("search-input"); const clearBtn = document.getElementById("btn-clear"); searchInput.value = ""; currentQuery = ""; clearBtn.style.display = "none"; fetchInitialGroups(); } function pickRandomGroup() { const cards = document.querySelectorAll(".group-card"); if (cards.length === 0) return; const randomIndex = Math.floor(Math.random() * cards.length); const selectedCard = cards[randomIndex]; document.querySelectorAll(".group-card.random-highlight").forEach(c => c.classList.remove("random-highlight")); selectedCard.classList.add("random-highlight"); selectedCard.scrollIntoView({ behavior: "smooth", block: "center" }); showToast("🎲 Lucky Pick selected! Tap Join → to enter."); } function setTypeFilter(type, element) { currentType = type; document.querySelectorAll(".tab, .nav-link, .hero-filter-btn").forEach(t => { if (t.classList.contains("btn-random-pick")) return; const text = t.textContent.trim().toLowerCase(); if (t.dataset && t.dataset.type === type) { t.classList.add("active"); } else if (type === "All" && (text.includes("all") || text.includes("explore"))) { t.classList.add("active"); } else if (type === "group_chat" && (text.includes("group chat") || text.includes("groups"))) { t.classList.add("active"); } else if (type === "broadcast_channel" && text.includes("channel")) { t.classList.add("active"); } else { t.classList.remove("active"); } }); fetchInitialGroups(); } const SUPABASE_REST_URL = "https://upguyzvvegsvzwtkdfxo.supabase.co/rest/v1"; const SUPABASE_ANON_KEY = "sb_publishable_YadXWwBosK-0ffcFAmjaTw_2JM4l2oD"; async function fetchGroupsUniversal(query, type, limit, offset) { try { const resp = await fetch(`/api/groups?q=${encodeURIComponent(query)}&type=${encodeURIComponent(type)}&limit=${limit}&offset=${offset}`); if (resp.ok) { const data = await resp.json(); if (data && data.groups) return data; } } catch (e) { // Fall back to Supabase Cloud API } // Direct Supabase REST API (for static hosting on Hugging Face / Vercel / GitHub Pages) try { let url = `${SUPABASE_REST_URL}/groups?select=*&order=id.desc&limit=${limit}&offset=${offset}`; if (query) { url += `&name=ilike.*${encodeURIComponent(query)}*`; } if (type === "group_chat") { url += `&type_id=eq.0`; } else if (type === "broadcast_channel") { url += `&type_id=eq.1`; } const resp = await fetch(url, { headers: { "apikey": SUPABASE_ANON_KEY, "Authorization": `Bearer ${SUPABASE_ANON_KEY}` } }); if (resp.ok) { const rows = await resp.json(); const formatted = rows.map(r => ({ id: r.id, name: r.name || "Community", token: r.token, invite_url: r.type_id === 1 ? `https://www.instagram.com/channel/${r.token}/` : `https://ig.me/j/${r.token}/`, type: r.type_id === 1 ? "broadcast_channel" : "group_chat", category: r.category || "General", source: r.source || "Web Index", clicks: r.clicks || 0, image_url: r.image_url || "", members_count: r.members_count || 0 })); return { groups: formatted, count: formatted.length }; } } catch (err) { console.error("Supabase fallback error:", err); } return { groups: [], count: 0 }; } async function fetchInitialGroups() { currentOffset = 0; loadedGroupsCount = 0; const grid = document.getElementById("groups-grid"); const emptyState = document.getElementById("empty-state"); const loadMoreWrap = document.getElementById("load-more-wrap"); const countLabel = document.getElementById("results-count"); try { const data = await fetchGroupsUniversal(currentQuery, currentType, PAGE_SIZE, 0); const groups = data.groups || []; loadedGroupsCount = groups.length; if (groups.length === 0) { grid.innerHTML = ""; emptyState.style.display = "block"; loadMoreWrap.style.display = "none"; if (countLabel) countLabel.textContent = "0 communities found"; return; } emptyState.style.display = "none"; grid.innerHTML = groups.map(g => renderCard(g)).join(""); if (groups.length === PAGE_SIZE) { loadMoreWrap.style.display = "flex"; if (countLabel) countLabel.textContent = `Showing 1–${loadedGroupsCount} communities`; } else { loadMoreWrap.style.display = "none"; if (countLabel) countLabel.textContent = `Showing all ${loadedGroupsCount} communities`; } } catch (err) { console.error("Error loading groups:", err); } } async function loadMoreGroups() { currentOffset += PAGE_SIZE; const grid = document.getElementById("groups-grid"); const loadMoreWrap = document.getElementById("load-more-wrap"); const btnSeeMore = document.getElementById("btn-see-more"); const countLabel = document.getElementById("results-count"); btnSeeMore.disabled = true; btnSeeMore.textContent = "Loading 48 More..."; try { const data = await fetchGroupsUniversal(currentQuery, currentType, PAGE_SIZE, currentOffset); const groups = data.groups || []; loadedGroupsCount += groups.length; if (groups.length > 0) { const html = groups.map(g => renderCard(g)).join(""); grid.insertAdjacentHTML("beforeend", html); } if (groups.length === PAGE_SIZE) { loadMoreWrap.style.display = "flex"; btnSeeMore.disabled = false; btnSeeMore.textContent = "See More Communities (Load 48) ↓"; if (countLabel) countLabel.textContent = `Showing 1–${loadedGroupsCount} communities`; } else { loadMoreWrap.style.display = "none"; if (countLabel) countLabel.textContent = `Showing all ${loadedGroupsCount} communities`; } } catch (err) { console.error("Error loading more groups:", err); btnSeeMore.disabled = false; btnSeeMore.textContent = "See More Communities (Load 48) ↓"; } } const AESTHETIC_PALETTES = [ { bg: "linear-gradient(135deg, #FF5858 0%, #F09819 100%)", glow: "rgba(255, 88, 88, 0.4)" }, // Sunset Coral { bg: "linear-gradient(135deg, #8A2387 0%, #E94057 50%, #F27121 100%)", glow: "rgba(233, 64, 87, 0.4)" }, // Instagram Flame { bg: "linear-gradient(135deg, #4FACFE 0%, #00F2FE 100%)", glow: "rgba(79, 172, 254, 0.4)" }, // Cyber Neon { bg: "linear-gradient(135deg, #667EEA 0%, #764BA2 100%)", glow: "rgba(102, 126, 234, 0.4)" }, // Royal Indigo { bg: "linear-gradient(135deg, #F093FB 0%, #F5576C 100%)", glow: "rgba(240, 147, 251, 0.4)" }, // Neon Rose { bg: "linear-gradient(135deg, #11998E 0%, #38EF7D 100%)", glow: "rgba(56, 239, 125, 0.4)" }, // Emerald Glow { bg: "linear-gradient(135deg, #FC466B 0%, #3F5EFB 100%)", glow: "rgba(252, 70, 107, 0.4)" }, // Twilight Electric { bg: "linear-gradient(135deg, #FF758C 0%, #FF7EB3 100%)", glow: "rgba(255, 117, 140, 0.4)" } // Bubblegum Peach ]; function getAestheticAvatar(group, isChannel, titleText) { // Generate deterministic hash index from token or name const seed = (group.token || titleText || "group").split("").reduce((acc, char) => acc + char.charCodeAt(0), 0); const palette = AESTHETIC_PALETTES[seed % AESTHETIC_PALETTES.length]; // Check if custom human title exists (extract 1-2 letter sleek monogram) const isCustomNamed = group.name && !group.name.startsWith("Instagram Community ("); let innerContent = ""; if (isCustomNamed && titleText.length >= 2) { const cleanLetters = titleText.replace(/[^a-zA-Z0-9]/g, "").toUpperCase(); const monogram = cleanLetters.slice(0, 2) || titleText.slice(0, 1).toUpperCase(); innerContent = `${escapeHtml(monogram)}`; } else if (isChannel) { innerContent = ` `; } else { innerContent = ` `; } return `