forked from snxraven/p2ns
- Centralized common formatting, DOM, and status utilities in `includes/plugins/sdk.js` - Created `sdk.utils.format`, `sdk.utils.dom`, and `sdk.utils.status` namespaces - Refactored `domain.consensus` and `peer.visualize` plugins to use the global SDK - Updated `plugin-handler` to serve SDK utilities globally via `/sdk-utils.js` - Enhanced SDK validation by delegating to the core validation infrastructure - Fixed bugs in metric rendering and missing frontend utility functions
153 lines
3.8 KiB
JavaScript
153 lines
3.8 KiB
JavaScript
/**
|
|
* Utility functions for domain consensus plugin
|
|
* Now using P2NS Plugin SDK for common utilities
|
|
*/
|
|
|
|
/**
|
|
* Format a timestamp to human-readable string
|
|
*/
|
|
function formatTimestamp(timestamp) {
|
|
return window.sdk?.utils?.format?.formatTimestamp(timestamp) || 'Never';
|
|
}
|
|
|
|
/**
|
|
* Format a peer ID to shortened version
|
|
*/
|
|
function formatPeerId(peerId) {
|
|
return window.sdk?.utils?.format?.formatPeerId(peerId) || 'N/A';
|
|
}
|
|
|
|
/**
|
|
* Format a hash to shortened version
|
|
*/
|
|
function formatHash(hash) {
|
|
return window.sdk?.utils?.format?.formatHash(hash) || 'N/A';
|
|
}
|
|
|
|
/**
|
|
* Get status badge class based on consensus status
|
|
*/
|
|
function getStatusBadgeClass(status) {
|
|
return window.sdk?.utils?.status?.getConsensusBadgeClass(status) || 'status-badge no_claims';
|
|
}
|
|
|
|
/**
|
|
* Get status text for consensus status
|
|
*/
|
|
function getStatusText(status) {
|
|
return window.sdk?.utils?.status?.getConsensusText(status) || 'Unknown';
|
|
}
|
|
|
|
/**
|
|
* Get status color for consensus status
|
|
*/
|
|
function getStatusColor(status) {
|
|
return window.sdk?.utils?.status?.getConsensusColor(status) || '#6b7280';
|
|
}
|
|
|
|
/**
|
|
* Calculate quorum percentage
|
|
*/
|
|
function calculateQuorumPercentage(totalVotes, minVotes) {
|
|
if (minVotes === 0) return 0;
|
|
return Math.min(100, Math.round((totalVotes / minVotes) * 100));
|
|
}
|
|
|
|
/**
|
|
* Escape HTML to prevent XSS
|
|
*/
|
|
function escapeHtml(text) {
|
|
return window.sdk?.utils?.dom?.escapeHtml(text) || text;
|
|
}
|
|
|
|
/**
|
|
* Copy text to clipboard
|
|
*/
|
|
async function copyToClipboard(text) {
|
|
if (window.sdk?.utils?.dom?.copyToClipboard) {
|
|
return await window.sdk.utils.dom.copyToClipboard(text);
|
|
}
|
|
return false;
|
|
}
|
|
|
|
/**
|
|
* Debounce function
|
|
*/
|
|
function debounce(func, wait) {
|
|
if (window.sdk?.utils?.dom?.debounce) {
|
|
return window.sdk.utils.dom.debounce(func, wait);
|
|
}
|
|
return func;
|
|
}
|
|
|
|
/**
|
|
* Format number with commas
|
|
*/
|
|
function formatNumber(num) {
|
|
return window.sdk?.utils?.format?.formatNumber(num) || '0';
|
|
}
|
|
|
|
/**
|
|
* Create a progress bar element
|
|
*/
|
|
function createProgressBar(percentage, color = '#3b82f6') {
|
|
const bar = document.createElement('div');
|
|
bar.className = 'w-full progress-bar-container';
|
|
bar.innerHTML = `
|
|
<div class="progress-bar" style="width: ${percentage}%; background-color: ${color};"></div>
|
|
`;
|
|
return bar;
|
|
}
|
|
|
|
/**
|
|
* Update sidebar stats (can be called from any view)
|
|
*/
|
|
async function updateSidebarStats() {
|
|
try {
|
|
// Fetch overview and peers data to get stats
|
|
const [overviewData, peersData] = await Promise.all([
|
|
window.apiClient.getOverview(),
|
|
window.apiClient.getPeers()
|
|
]);
|
|
|
|
const stats = overviewData.stats || {};
|
|
const activePeers = peersData.activePeers || 0;
|
|
|
|
const totalDomainsEl = document.getElementById('statTotalDomains');
|
|
const resolvedEl = document.getElementById('statResolved');
|
|
const activePeersEl = document.getElementById('statActivePeers');
|
|
|
|
if (totalDomainsEl) totalDomainsEl.textContent = formatNumber(stats.totalDomains || 0);
|
|
if (resolvedEl) resolvedEl.textContent = formatNumber(stats.resolved || 0);
|
|
if (activePeersEl) activePeersEl.textContent = formatNumber(activePeers);
|
|
} catch (error) {
|
|
console.error('Error updating sidebar stats:', error);
|
|
// Set to 0 on error
|
|
const totalDomainsEl = document.getElementById('statTotalDomains');
|
|
const resolvedEl = document.getElementById('statResolved');
|
|
const activePeersEl = document.getElementById('statActivePeers');
|
|
if (totalDomainsEl) totalDomainsEl.textContent = '0';
|
|
if (resolvedEl) resolvedEl.textContent = '0';
|
|
if (activePeersEl) activePeersEl.textContent = '0';
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Export functions
|
|
*/
|
|
window.utils = {
|
|
formatTimestamp,
|
|
formatPeerId,
|
|
formatHash,
|
|
getStatusBadgeClass,
|
|
getStatusText,
|
|
getStatusColor,
|
|
calculateQuorumPercentage,
|
|
escapeHtml,
|
|
copyToClipboard,
|
|
debounce,
|
|
formatNumber,
|
|
createProgressBar,
|
|
updateSidebarStats
|
|
};
|