Bump to v0.5.0 — note sharing, reply counts

This commit is contained in:
Jure
2026-03-15 21:49:52 +01:00
parent 5b4f6381da
commit 8ce1d43d2d
10 changed files with 85 additions and 9 deletions
+33 -2
View File
@@ -2,12 +2,14 @@ import { useState, useRef, useEffect } from "react";
import { NDKEvent } from "@nostr-dev-kit/ndk";
import { useProfile } from "../../hooks/useProfile";
import { useReactionCount } from "../../hooks/useReactionCount";
import { useReplyCount } from "../../hooks/useReplyCount";
import { useZapCount } from "../../hooks/useZapCount";
import { useUserStore } from "../../stores/user";
import { useMuteStore } from "../../stores/mute";
import { useBookmarkStore } from "../../stores/bookmark";
import { useUIStore } from "../../stores/ui";
import { timeAgo, shortenPubkey } from "../../lib/utils";
import { nip19 } from "@nostr-dev-kit/ndk";
import { publishReaction, publishReply, publishRepost, getNDK, fetchNoteById } from "../../lib/nostr";
import { NoteContent } from "./NoteContent";
import { ZapModal } from "../zap/ZapModal";
@@ -61,6 +63,8 @@ export function NoteCard({ event, focused }: NoteCardProps) {
const [liked, setLiked] = useState(() => getLiked().has(event.id));
const [liking, setLiking] = useState(false);
const [reactionCount, adjustReactionCount] = useReactionCount(event.id);
const [replyCount, adjustReplyCount] = useReplyCount(event.id);
const [copied, setCopied] = useState(false);
const zapData = useZapCount(event.id);
const [showReply, setShowReply] = useState(false);
const [replyText, setReplyText] = useState("");
@@ -102,6 +106,7 @@ export function NoteCard({ event, focused }: NoteCardProps) {
await publishReply(replyText.trim(), { id: event.id, pubkey: event.pubkey });
setReplyText("");
setReplySent(true);
adjustReplyCount(1);
setTimeout(() => { setShowReply(false); setReplySent(false); }, 1500);
} catch (err) {
setReplyError(`Failed: ${err}`);
@@ -115,6 +120,13 @@ export function NoteCard({ event, focused }: NoteCardProps) {
if (e.key === "Escape") setShowReply(false);
};
const handleShare = async () => {
const nevent = nip19.neventEncode({ id: event.id!, author: event.pubkey });
await navigator.clipboard.writeText("nostr:" + nevent);
setCopied(true);
setTimeout(() => setCopied(false), 2000);
};
const handleRepost = async () => {
if (reposting || reposted) return;
setReposting(true);
@@ -235,7 +247,7 @@ export function NoteCard({ event, focused }: NoteCardProps) {
showReply ? "text-accent" : "text-text-dim hover:text-text"
}`}
>
reply
reply{replyCount !== null && replyCount > 0 ? ` ${replyCount}` : ""}
</button>
<button
onClick={handleLike}
@@ -279,18 +291,37 @@ export function NoteCard({ event, focused }: NoteCardProps) {
>
{isBookmarked ? "▪ saved" : "▫ save"}
</button>
<button
onClick={handleShare}
className={`text-[11px] transition-colors ${
copied ? "text-accent" : "text-text-dim hover:text-text"
}`}
>
{copied ? "copied ✓" : "share"}
</button>
</div>
)}
{/* Stats visible when logged out */}
{!loggedIn && (reactionCount !== null && reactionCount > 0 || zapData !== null && zapData.totalSats > 0) && (
{!loggedIn && (
<div className="flex items-center gap-3 mt-1.5">
{replyCount !== null && replyCount > 0 && (
<span className="text-text-dim text-[11px]"> {replyCount}</span>
)}
{reactionCount !== null && reactionCount > 0 && (
<span className="text-text-dim text-[11px]"> {reactionCount}</span>
)}
{zapData !== null && zapData.totalSats > 0 && (
<span className="text-zap text-[11px]"> {zapData.totalSats.toLocaleString()} sats</span>
)}
<button
onClick={handleShare}
className={`text-[11px] transition-colors ${
copied ? "text-accent" : "text-text-dim hover:text-text"
}`}
>
{copied ? "copied ✓" : "share"}
</button>
</div>
)}
+29
View File
@@ -0,0 +1,29 @@
import { useEffect, useState } from "react";
import { fetchReplyCount } from "../lib/nostr";
const cache = new Map<string, number>();
export function useReplyCount(eventId: string): [number | null, (delta: number) => void] {
const [count, setCount] = useState<number | null>(() => cache.get(eventId) ?? null);
useEffect(() => {
if (cache.has(eventId)) {
setCount(cache.get(eventId)!);
return;
}
fetchReplyCount(eventId).then((n) => {
cache.set(eventId, n);
setCount(n);
});
}, [eventId]);
const adjust = (delta: number) => {
setCount((prev) => {
const next = (prev ?? 0) + delta;
cache.set(eventId, next);
return next;
});
};
return [count, adjust];
}
+12
View File
@@ -313,6 +313,18 @@ export async function fetchZapCount(eventId: string): Promise<{ count: number; t
return { count: events.size, totalSats };
}
export async function fetchReplyCount(eventId: string): Promise<number> {
const instance = getNDK();
const filter: NDKFilter = {
kinds: [NDKKind.Text],
"#e": [eventId],
};
const events = await instance.fetchEvents(filter, {
cacheUsage: NDKSubscriptionCacheUsage.ONLY_RELAY,
});
return events.size;
}
export async function fetchReactionCount(eventId: string): Promise<number> {
const instance = getNDK();
const filter: NDKFilter = {
+1 -1
View File
@@ -1,2 +1,2 @@
export { getNDK, connectToRelays, fetchGlobalFeed, fetchFollowFeed, fetchReplies, publishNote, publishArticle, publishProfile, publishReaction, publishRepost, publishQuote, publishReply, publishContactList, fetchReactionCount, fetchZapCount, fetchNoteById, fetchUserNotes, fetchProfile, fetchArticle, fetchAuthorArticles, fetchZapsReceived, fetchZapsSent, fetchDMConversations, fetchDMThread, sendDM, decryptDM, fetchBookmarkList, publishBookmarkList, fetchMuteList, publishMuteList, getStoredRelayUrls, addRelay, removeRelay, searchNotes, searchUsers, fetchUserRelayList, publishRelayList, fetchUserNotesNIP65, fetchFollowSuggestions, fetchMentions } from "./client";
export { getNDK, connectToRelays, fetchGlobalFeed, fetchFollowFeed, fetchReplies, publishNote, publishArticle, publishProfile, publishReaction, publishRepost, publishQuote, publishReply, publishContactList, fetchReactionCount, fetchReplyCount, fetchZapCount, fetchNoteById, fetchUserNotes, fetchProfile, fetchArticle, fetchAuthorArticles, fetchZapsReceived, fetchZapsSent, fetchDMConversations, fetchDMThread, sendDM, decryptDM, fetchBookmarkList, publishBookmarkList, fetchMuteList, publishMuteList, getStoredRelayUrls, addRelay, removeRelay, searchNotes, searchUsers, fetchUserRelayList, publishRelayList, fetchUserNotesNIP65, fetchFollowSuggestions, fetchMentions } from "./client";
export type { UserRelayList } from "./client";