|
|
|
|
@ -16,6 +16,7 @@
@@ -16,6 +16,7 @@
|
|
|
|
|
let commentCount = $state(0); |
|
|
|
|
let zapTotal = $state(0); |
|
|
|
|
let zapCount = $state(0); |
|
|
|
|
let latestResponseTime = $state<number | null>(null); |
|
|
|
|
let loadingStats = $state(true); |
|
|
|
|
|
|
|
|
|
onMount(async () => { |
|
|
|
|
@ -24,9 +25,19 @@
@@ -24,9 +25,19 @@
|
|
|
|
|
|
|
|
|
|
async function loadStats() { |
|
|
|
|
loadingStats = true; |
|
|
|
|
const timeout = 30000; // 30 seconds |
|
|
|
|
|
|
|
|
|
try { |
|
|
|
|
const config = nostrClient.getConfig(); |
|
|
|
|
|
|
|
|
|
// Create a timeout promise |
|
|
|
|
const timeoutPromise = new Promise<never>((_, reject) => { |
|
|
|
|
setTimeout(() => reject(new Error('Stats loading timeout')), timeout); |
|
|
|
|
}); |
|
|
|
|
|
|
|
|
|
// Race between loading and timeout |
|
|
|
|
await Promise.race([ |
|
|
|
|
(async () => { |
|
|
|
|
// Load reactions (kind 7) |
|
|
|
|
const reactionRelays = relayManager.getThreadReadRelays(); |
|
|
|
|
const reactionEvents = await nostrClient.fetchEvents( |
|
|
|
|
@ -76,8 +87,34 @@
@@ -76,8 +87,34 @@
|
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
// Find latest response time (most recent comment, reaction, or zap) |
|
|
|
|
let latestTime = thread.created_at; |
|
|
|
|
if (commentEvents.length > 0) { |
|
|
|
|
const latestComment = commentEvents.sort((a, b) => b.created_at - a.created_at)[0]; |
|
|
|
|
latestTime = Math.max(latestTime, latestComment.created_at); |
|
|
|
|
} |
|
|
|
|
if (reactionEvents.length > 0) { |
|
|
|
|
const latestReaction = reactionEvents.sort((a, b) => b.created_at - a.created_at)[0]; |
|
|
|
|
latestTime = Math.max(latestTime, latestReaction.created_at); |
|
|
|
|
} |
|
|
|
|
if (zapReceipts.length > 0) { |
|
|
|
|
const latestZap = zapReceipts.sort((a, b) => b.created_at - a.created_at)[0]; |
|
|
|
|
latestTime = Math.max(latestTime, latestZap.created_at); |
|
|
|
|
} |
|
|
|
|
latestResponseTime = latestTime > thread.created_at ? latestTime : null; |
|
|
|
|
})(), |
|
|
|
|
timeoutPromise |
|
|
|
|
]); |
|
|
|
|
} catch (error) { |
|
|
|
|
console.error('Error loading thread stats:', error); |
|
|
|
|
// On timeout or error, show zero stats instead of loading forever |
|
|
|
|
upvotes = 0; |
|
|
|
|
downvotes = 0; |
|
|
|
|
commentCount = 0; |
|
|
|
|
zapTotal = 0; |
|
|
|
|
zapCount = 0; |
|
|
|
|
latestResponseTime = null; |
|
|
|
|
} finally { |
|
|
|
|
loadingStats = false; |
|
|
|
|
} |
|
|
|
|
@ -109,6 +146,20 @@
@@ -109,6 +146,20 @@
|
|
|
|
|
return 'just now'; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
function getLatestResponseTime(): string { |
|
|
|
|
if (!latestResponseTime) return ''; |
|
|
|
|
const now = Math.floor(Date.now() / 1000); |
|
|
|
|
const diff = now - latestResponseTime; |
|
|
|
|
const hours = Math.floor(diff / 3600); |
|
|
|
|
const days = Math.floor(diff / 86400); |
|
|
|
|
const minutes = Math.floor(diff / 60); |
|
|
|
|
|
|
|
|
|
if (days > 0) return `${days}d ago`; |
|
|
|
|
if (hours > 0) return `${hours}h ago`; |
|
|
|
|
if (minutes > 0) return `${minutes}m ago`; |
|
|
|
|
return 'just now'; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
function getClientName(): string | null { |
|
|
|
|
const clientTag = thread.tags.find((t) => t[0] === 'client'); |
|
|
|
|
return clientTag?.[1] || null; |
|
|
|
|
@ -140,18 +191,23 @@
@@ -140,18 +191,23 @@
|
|
|
|
|
</div> |
|
|
|
|
{/if} |
|
|
|
|
|
|
|
|
|
<div class="flex items-center justify-between text-xs text-fog-text-light dark:text-fog-dark-text-light"> |
|
|
|
|
<div class="flex items-center gap-4"> |
|
|
|
|
{#if !loadingStats} |
|
|
|
|
<span>↑ {upvotes}</span> |
|
|
|
|
<span>↓ {downvotes}</span> |
|
|
|
|
<span>{commentCount} {commentCount === 1 ? 'comment' : 'comments'}</span> |
|
|
|
|
<div class="flex items-center justify-between text-xs text-fog-text dark:text-fog-dark-text"> |
|
|
|
|
<div class="flex items-center gap-4 flex-wrap"> |
|
|
|
|
{#if loadingStats} |
|
|
|
|
<span class="text-fog-text-light dark:text-fog-dark-text-light">Loading stats...</span> |
|
|
|
|
{:else} |
|
|
|
|
<span class="font-medium">↑ {upvotes}</span> |
|
|
|
|
<span class="font-medium">↓ {downvotes}</span> |
|
|
|
|
<span class="font-medium">{commentCount} {commentCount === 1 ? 'comment' : 'comments'}</span> |
|
|
|
|
{#if latestResponseTime} |
|
|
|
|
<span class="text-fog-text-light dark:text-fog-dark-text-light">Last: {getLatestResponseTime()}</span> |
|
|
|
|
{/if} |
|
|
|
|
{#if zapCount > 0} |
|
|
|
|
<span>⚡ {zapTotal.toLocaleString()} sats ({zapCount})</span> |
|
|
|
|
<span class="font-medium">⚡ {zapTotal.toLocaleString()} sats ({zapCount})</span> |
|
|
|
|
{/if} |
|
|
|
|
{/if} |
|
|
|
|
</div> |
|
|
|
|
<a href="/thread/{thread.id}">View thread →</a> |
|
|
|
|
<a href="/thread/{thread.id}" class="ml-2 text-fog-accent dark:text-fog-dark-accent hover:underline">View thread →</a> |
|
|
|
|
</div> |
|
|
|
|
</article> |
|
|
|
|
|
|
|
|
|
|