Add relay count to post time display: shows '21r - 1h' format on each post card, tracking successful relay count from broadcastProgress events

This commit is contained in:
Laan Tungir
2026-06-30 09:28:38 -04:00
parent 4abb4bf33c
commit 456f0e1f2e
2 changed files with 53 additions and 11 deletions

View File

@@ -915,16 +915,56 @@ export function formatTimeAgo(timestamp) {
}
// Store timestamps for real-time updates
const timeAgoElements = new Map(); // element -> timestamp
const timeAgoElements = new Map(); // element -> { timestamp, eventId }
// Track relay publish counts per event ID from broadcastProgress events.
// Keyed by event ID → number of successful relay publishes.
const relayCountByEventId = new Map();
// Listen for broadcast progress events to capture final relay counts.
// The worker emits ndkBroadcastProgress with phase 'done' containing the
// total successful count. We store it so post cards can show "21r - 1h".
if (typeof window !== 'undefined') {
window.addEventListener('ndkBroadcastProgress', (event) => {
const d = event.detail;
if (!d || d.phase !== 'done') return;
const eventId = d.eventId;
if (!eventId) return;
relayCountByEventId.set(eventId, d.successful || 0);
// Update any already-rendered time elements for this event.
timeAgoElements.forEach((info, element) => {
if (info.eventId === eventId && element.isConnected) {
element.textContent = formatTimeAgoWithRelays(info.timestamp, info.eventId);
}
});
});
}
/**
* Format time ago with optional relay count prefix.
* Shows "21r - 1h" when a relay count is available, else just "1h".
* @param {number} timestamp - Unix timestamp in seconds
* @param {string} [eventId] - Optional event ID to look up relay count
* @returns {string}
*/
function formatTimeAgoWithRelays(timestamp, eventId) {
const timeStr = formatTimeAgo(timestamp);
if (eventId && relayCountByEventId.has(eventId)) {
const count = relayCountByEventId.get(eventId);
return `${count}r - ${timeStr}`;
}
return timeStr;
}
/**
* Register an element for time updates
* @param {HTMLElement} element - The element to update
* @param {number} timestamp - Unix timestamp in seconds
* @param {string} [eventId] - Optional event ID for relay count display
*/
export function registerTimeAgo(element, timestamp) {
timeAgoElements.set(element, timestamp);
element.textContent = formatTimeAgo(timestamp);
export function registerTimeAgo(element, timestamp, eventId) {
timeAgoElements.set(element, { timestamp, eventId });
element.textContent = formatTimeAgoWithRelays(timestamp, eventId);
}
/**
@@ -932,9 +972,9 @@ export function registerTimeAgo(element, timestamp) {
* Call this periodically (e.g., every 30 seconds)
*/
export function updateTimeAgos() {
timeAgoElements.forEach((timestamp, element) => {
timeAgoElements.forEach((info, element) => {
if (element.isConnected) {
element.textContent = formatTimeAgo(timestamp);
element.textContent = formatTimeAgoWithRelays(info.timestamp, info.eventId);
} else {
// Clean up detached elements
timeAgoElements.delete(element);
@@ -1085,10 +1125,12 @@ export function renderFooterRow(eventId, eventData, options = {}) {
container.appendChild(nutzapItem);
container.appendChild(midControls);
// Time as the final item in the same row
// Time as the final item in the same row.
// Pass eventId so registerTimeAgo can prepend the relay count
// (e.g., "21r - 1h") when a broadcastProgress 'done' event arrives.
const timeEl = document.createElement('span');
timeEl.className = 'divPostTime';
registerTimeAgo(timeEl, eventData.created_at);
registerTimeAgo(timeEl, eventData.created_at, eventId);
container.appendChild(timeEl);
footerRow.appendChild(container);

View File

@@ -1,5 +1,5 @@
{
"VERSION": "v0.7.69",
"VERSION_NUMBER": "0.7.69",
"BUILD_DATE": "2026-06-30T13:19:30.001Z"
"VERSION": "v0.7.70",
"VERSION_NUMBER": "0.7.70",
"BUILD_DATE": "2026-06-30T13:28:38.113Z"
}