diff --git a/assets/js/AdminBoard.js b/assets/js/AdminBoard.js index 57a1973..1d13191 100644 --- a/assets/js/AdminBoard.js +++ b/assets/js/AdminBoard.js @@ -439,123 +439,11 @@ const fetchAllEncryptedCards = async (isRefresh = false) => { selectedSort = sortSelect.value } - if (selectedSort === 'name') { - // Sort alphabetically by the minter's name - finalCards.sort((a, b) => { - const nameA = a.decryptedCardData.minterName?.toLowerCase() || '' - const nameB = b.decryptedCardData.minterName?.toLowerCase() || '' - return nameA.localeCompare(nameB) - }) - } else if (selectedSort === 'recent-comments') { - // We need each card's newest comment timestamp for sorting - for (let card of finalCards) { - card.newestCommentTimestamp = await getNewestAdminCommentTimestamp(card.card.identifier) - } - // Then sort descending by newest comment - finalCards.sort((a, b) => - (b.newestCommentTimestamp || 0) - (a.newestCommentTimestamp || 0) - ) - } else if (selectedSort === 'least-votes') { - // TODO: Add the logic to sort by LEAST total ADMIN votes, then totalYesWeight - const minterGroupMembers = await fetchMinterGroupMembers() - const minterAdmins = await fetchMinterGroupAdmins() - for (const finalCard of finalCards) { - try { - const pollName = finalCard.decryptedCardData.poll - // If card or poll is missing, default to zero - if (!pollName) { - finalCard._adminTotalVotes = 0 - finalCard._yesWeight = 0 - continue - } - const pollResults = await fetchPollResults(pollName) - if (!pollResults || pollResults.error) { - finalCard._adminTotalVotes = 0 - finalCard._yesWeight = 0 - continue - } - // Pull only the adminYes/adminNo/totalYesWeight from processPollData - const { - adminYes, - adminNo, - totalYesWeight - } = await processPollData( - pollResults, - minterGroupMembers, - minterAdmins, - finalCard.decryptedCardData.creator, - finalCard.card.identifier - ) - finalCard._adminTotalVotes = adminYes + adminNo - finalCard._yesWeight = totalYesWeight - } catch (error) { - console.warn(`Error fetching or processing poll for card ${finalCard.card.identifier}:`, error) - finalCard._adminTotalVotes = 0 - finalCard._yesWeight = 0 - } - } - // Sort ascending by (adminYes + adminNo), then descending by totalYesWeight - finalCards.sort((a, b) => { - const diffAdminTotal = a._adminTotalVotes - b._adminTotalVotes - if (diffAdminTotal !== 0) return diffAdminTotal - // If there's a tie, show the card with higher yesWeight first - return b._yesWeight - a._yesWeight - }) - } else if (selectedSort === 'most-votes') { - // TODO: Add the logic to sort by MOST total ADMIN votes, then totalYesWeight - const minterGroupMembers = await fetchMinterGroupMembers() - const minterAdmins = await fetchMinterGroupAdmins() - for (const finalCard of finalCards) { - try { - const pollName = finalCard.decryptedCardData.poll - if (!pollName) { - finalCard._adminTotalVotes = 0 - finalCard._yesWeight = 0 - continue - } - const pollResults = await fetchPollResults(pollName) - if (!pollResults || pollResults.error) { - finalCard._adminTotalVotes = 0 - finalCard._yesWeight = 0 - continue - } - const { - adminYes, - adminNo, - totalYesWeight - } = await processPollData( - pollResults, - minterGroupMembers, - minterAdmins, - finalCard.decryptedCardData.creator, - finalCard.card.identifier - ) - finalCard._adminTotalVotes = adminYes + adminNo - finalCard._yesWeight = totalYesWeight - } catch (error) { - console.warn(`Error fetching or processing poll for card ${finalCard.card.identifier}:`, error) - finalCard._adminTotalVotes = 0 - finalCard._yesWeight = 0 - } - } - // Sort descending by (adminYes + adminNo), then descending by totalYesWeight - finalCards.sort((a, b) => { - const diffAdminTotal = b._adminTotalVotes - a._adminTotalVotes - if (diffAdminTotal !== 0) return diffAdminTotal - return b._yesWeight - a._yesWeight - }) - } else { - // Sort cards by timestamp (most recent first) - finalCards.sort((a, b) => { - const timestampA = a.card.updated || a.card.created || 0 - const timestampB = b.card.updated || b.card.created || 0 - return timestampB - timestampA; - }) - } + const sortedFinalCards = await sortCards(finalCards, selectedSort, "admin") encryptedCardsContainer.innerHTML = "" - const finalVisualFilterCards = finalCards.filter(({card}) => { + const finalVisualFilterCards = sortedFinalCards.filter(({card}) => { const showKickedBanned = document.getElementById('admin-show-kicked-banned-checkbox')?.checked ?? false const showHiddenAdminCards = document.getElementById('admin-show-hidden-checkbox')?.checked ?? false @@ -1257,23 +1145,6 @@ const handleBanMinter = async (minterName) => { } } -const getNewestAdminCommentTimestamp = async (cardIdentifier) => { - try { - const comments = await fetchEncryptedComments(cardIdentifier) - if (!comments || comments.length === 0) { - return 0 - } - const newestTimestamp = comments.reduce((acc, comment) => { - const cTime = comment.updated || comment.created || 0 - return cTime > acc ? cTime : acc - }, 0) - return newestTimestamp - } catch (err) { - console.error('Failed to get newest comment timestamp:', err) - return 0 - } -} - const deleteAdminCard = async (cardIdentifier) => { try { const confirmed = confirm("Are you sure you want to delete this card? This action cannot be undone.") diff --git a/assets/js/MinterBoard.js b/assets/js/MinterBoard.js index 27edb80..2be1819 100644 --- a/assets/js/MinterBoard.js +++ b/assets/js/MinterBoard.js @@ -418,30 +418,13 @@ const loadCards = async (cardIdentifierPrefix) => { selectedSort = sortSelect.value } - if (selectedSort === 'name') { - finalCards.sort((a, b) => { - const nameA = a.name?.toLowerCase() || '' - const nameB = b.name?.toLowerCase() || '' - return nameA.localeCompare(nameB) - }) - } else if (selectedSort === 'recent-comments') { - // If you need the newest comment timestamp - for (let card of finalCards) { - card.newestCommentTimestamp = await getNewestCommentTimestamp(card.identifier) - } - finalCards.sort((a, b) => - (b.newestCommentTimestamp || 0) - (a.newestCommentTimestamp || 0) - ) - } else if (selectedSort === 'least-votes') { - await applyVoteSortingData(finalCards, /* ascending= */ true) - } else if (selectedSort === 'most-votes') { - await applyVoteSortingData(finalCards, /* ascending= */ false) - } - // else 'newest' => do nothing (already sorted newest-first by your process functions). + const sortedFinalCards = isARBoard + ? await sortCards(finalCards, selectedSort, "ar") + : await sortCards(finalCards, selectedSort, "minter") // Create the 'finalCardsArray' that includes the data, etc. let finalCardsArray = [] cardsContainer.innerHTML = '' - for (const card of finalCards) { + for (const card of sortedFinalCards) { try { const skeletonHTML = createSkeletonCardHTML(card.identifier) cardsContainer.insertAdjacentHTML("beforeend", skeletonHTML) @@ -560,71 +543,6 @@ const verifyMinter = async (minterName) => { } } -const applyVoteSortingData = async (cards, ascending = true) => { - const minterGroupMembers = await fetchMinterGroupMembers() - const minterAdmins = await fetchMinterGroupAdmins() - - for (const card of cards) { - try { - const cardDataResponse = await qortalRequest({ - action: "FETCH_QDN_RESOURCE", - name: card.name, - service: "BLOG_POST", - identifier: card.identifier, - }) - if (!cardDataResponse || !cardDataResponse.poll) { - card._adminVotes = 0 - card._adminYes = 0 - card._minterVotes = 0 - card._minterYes = 0 - continue - } - const pollResults = await fetchPollResults(cardDataResponse.poll); - const { adminYes, adminNo, minterYes, minterNo } = await processPollData( - pollResults, - minterGroupMembers, - minterAdmins, - cardDataResponse.creator, - card.identifier - ) - card._adminVotes = adminYes + adminNo - card._adminYes = adminYes - card._minterVotes = minterYes + minterNo - card._minterYes = minterYes - } catch (error) { - console.warn(`Error fetching or processing poll for card ${card.identifier}:`, error) - card._adminVotes = 0 - card._adminYes = 0 - card._minterVotes = 0 - card._minterYes = 0 - } - } - - if (ascending) { - // least votes first - cards.sort((a, b) => { - const diffAdminTotal = a._adminVotes - b._adminVotes - if (diffAdminTotal !== 0) return diffAdminTotal - const diffAdminYes = a._adminYes - b._adminYes - if (diffAdminYes !== 0) return diffAdminYes - const diffMinterTotal = a._minterVotes - b._minterVotes - if (diffMinterTotal !== 0) return diffMinterTotal - return a._minterYes - b._minterYes - }) - } else { - // most votes first - cards.sort((a, b) => { - const diffAdminTotal = b._adminVotes - a._adminVotes - if (diffAdminTotal !== 0) return diffAdminTotal - const diffAdminYes = b._adminYes - a._adminYes - if (diffAdminYes !== 0) return diffAdminYes - const diffMinterTotal = b._minterVotes - a._minterVotes - if (diffMinterTotal !== 0) return diffMinterTotal - return b._minterYes - a._minterYes - }) - } -} - const removeSkeleton = (cardIdentifier) => { const skeletonCard = document.getElementById(`skeleton-${cardIdentifier}`) if (skeletonCard) { @@ -1879,26 +1797,6 @@ const getMinterAvatar = async (minterName) => { } } -const getNewestCommentTimestamp = async (cardIdentifier) => { - try { - // fetchCommentsForCard returns resources each with at least 'created' or 'updated' - const comments = await fetchCommentsForCard(cardIdentifier) - if (!comments || comments.length === 0) { - // No comments => fallback to 0 (or card's own date, if you like) - return 0 - } - // The newest can be determined by comparing 'updated' or 'created' - const newestTimestamp = comments.reduce((acc, c) => { - const cTime = c.updated || c.created || 0 - return (cTime > acc) ? cTime : acc - }, 0) - return newestTimestamp - } catch (err) { - console.error('Failed to get newest comment timestamp:', err) - return 0 - } -} - const deleteCard = async (cardIdentifier) => { try { const confirmed = confirm("Are you sure you want to delete this card? This action cannot be undone.") diff --git a/assets/js/Shared.js b/assets/js/Shared.js index b03885d..996e440 100644 --- a/assets/js/Shared.js +++ b/assets/js/Shared.js @@ -163,9 +163,9 @@ const fetchAllKickBanTxData = async () => { finalBanTxs, pendingBanTxs, } - } +} - const partitionTransactions = (txSearchResults) => { +const partitionTransactions = (txSearchResults) => { const finalTx = [] const pendingTx = [] @@ -178,7 +178,140 @@ const fetchAllKickBanTxData = async () => { } return { finalTx, pendingTx }; +} + +const sortCards = async (cardsArray, selectedSort, board) => { + // Default sort is by newest if none provided + if (!selectedSort) selectedSort = 'newest' + switch (selectedSort) { + case 'name': + // Sort by name + cardsArray.sort((a, b) => { + const nameA = (board === "admin") + ? (a.decryptedCardData?.minterName || '').toLowerCase() + : ((board === "ar") + ? (a.minterName?.toLowerCase() || '') + : (a.name?.toLowerCase() || '') + ) + const nameB = (board === "admin") + ? (b.decryptedCardData?.minterName || '').toLowerCase() + : ((board === "ar") + ? (b.minterName?.toLowerCase() || '') + : (b.name?.toLowerCase() || '') + ) + return nameA.localeCompare(nameB) + }) + break + case 'recent-comments': + // Sort by newest comment timestamp + for (let card of cardsArray) { + const cardIdentifier = (board === "admin") + ? card.card.identifier + : card.identifier + card.newestCommentTimestamp = await getNewestCommentTimestamp(cardIdentifier, board) + } + cardsArray.sort((a, b) => { + return (b.newestCommentTimestamp || 0) - (a.newestCommentTimestamp || 0) + }) + break + case 'least-votes': + await applyVoteSortingData(cardsArray, /* ascending= */ true) + break + case 'most-votes': + await applyVoteSortingData(cardsArray, /* ascending= */ false) + break + default: + // Sort by date + cardsArray.sort((a, b) => { + const timestampA = (board === "admin") + ? a.card.updated || a.card.created || 0 + : a.updated || a.created || 0 + const timestampB = (board === "admin") + ? b.card.updated || b.card.created || 0 + : b.updated || b.created || 0 + return timestampB - timestampA; + }) + break } - - - \ No newline at end of file + return cardsArray +} + +const getNewestCommentTimestamp = async (cardIdentifier, board) => { + try { + const comments = (board === "admin") ? await fetchEncryptedComments(cardIdentifier) : await fetchCommentsForCard(cardIdentifier) + if (!comments || comments.length === 0) { + return 0 + } + const newestTimestamp = comments.reduce((acc, c) => { + const cTime = c.updated || c.created || 0 + return (cTime > acc) ? cTime : acc + }, 0) + return newestTimestamp + } catch (err) { + console.error('Failed to get newest comment timestamp:', err) + return 0 + } +} + +const applyVoteSortingData = async (cards, ascending = true) => { + const minterGroupMembers = await fetchMinterGroupMembers() + const minterAdmins = await fetchMinterGroupAdmins() + for (const card of cards) { + try { + const cardDataResponse = await qortalRequest({ + action: "FETCH_QDN_RESOURCE", + name: card.name, + service: "BLOG_POST", + identifier: card.identifier, + }) + if (!cardDataResponse || !cardDataResponse.poll) { + card._adminVotes = 0 + card._adminYes = 0 + card._minterVotes = 0 + card._minterYes = 0 + continue + } + const pollResults = await fetchPollResults(cardDataResponse.poll); + const { adminYes, adminNo, minterYes, minterNo } = await processPollData( + pollResults, + minterGroupMembers, + minterAdmins, + cardDataResponse.creator, + card.identifier + ) + card._adminVotes = adminYes + adminNo + card._adminYes = adminYes + card._minterVotes = minterYes + minterNo + card._minterYes = minterYes + } catch (error) { + console.warn(`Error fetching or processing poll for card ${card.identifier}:`, error) + card._adminVotes = 0 + card._adminYes = 0 + card._minterVotes = 0 + card._minterYes = 0 + } + } + if (ascending) { + // least votes first + cards.sort((a, b) => { + const diffAdminTotal = a._adminVotes - b._adminVotes + if (diffAdminTotal !== 0) return diffAdminTotal + const diffAdminYes = a._adminYes - b._adminYes + if (diffAdminYes !== 0) return diffAdminYes + const diffMinterTotal = a._minterVotes - b._minterVotes + if (diffMinterTotal !== 0) return diffMinterTotal + return a._minterYes - b._minterYes + }) + } else { + // most votes first + cards.sort((a, b) => { + const diffAdminTotal = b._adminVotes - a._adminVotes + if (diffAdminTotal !== 0) return diffAdminTotal + const diffAdminYes = b._adminYes - a._adminYes + if (diffAdminYes !== 0) return diffAdminYes + const diffMinterTotal = b._minterVotes - a._minterVotes + if (diffMinterTotal !== 0) return diffMinterTotal + return b._minterYes - a._minterYes + }) + } +}