Two fixes: 1. post-interactions2.mjs: only call addCommentToPost when getCommentsVisible() is true 2. feed.html: call cards.setCommentsVisible(false) after initPostCards to disable inline comment rendering for the new simplified feed The old feed (feed-old.html) still has the Comments toggle and can re-enable them.
162 lines
4.4 KiB
JavaScript
162 lines
4.4 KiB
JavaScript
import { initInteractions } from './post-interactions.mjs';
|
|
|
|
const DEFAULT_MAX_SUBSCRIPTIONS = 25;
|
|
|
|
export function initPostCards(deps = {}) {
|
|
const {
|
|
subscribe,
|
|
publishEvent,
|
|
getPubkey,
|
|
ndkFetchEvents,
|
|
queryCache,
|
|
fetchCachedProfile,
|
|
storeProfile,
|
|
walletPayInvoice,
|
|
walletSendNutzap,
|
|
walletFetchMintList,
|
|
walletGetMints,
|
|
walletGetBalance,
|
|
getRelayData,
|
|
maxSubscriptions = DEFAULT_MAX_SUBSCRIPTIONS,
|
|
getUserSettings,
|
|
patchUserSettings,
|
|
onCommentIntent,
|
|
onQuoteIntent,
|
|
onMuteIntent
|
|
} = deps;
|
|
|
|
const interactions = initInteractions({
|
|
subscribe,
|
|
publishEvent,
|
|
getPubkey,
|
|
ndkFetchEvents,
|
|
queryCache,
|
|
fetchCachedProfile,
|
|
storeProfile,
|
|
walletPayInvoice,
|
|
walletSendNutzap,
|
|
walletFetchMintList,
|
|
walletGetMints,
|
|
walletGetBalance,
|
|
getRelayData,
|
|
getUserSettings,
|
|
patchUserSettings,
|
|
onCommentIntent,
|
|
onQuoteIntent,
|
|
onMuteIntent
|
|
});
|
|
|
|
const activeSubscriptions = new Map(); // key -> { subId, timestamp }
|
|
|
|
function makeSubKey(postIds = []) {
|
|
return Array.from(new Set(postIds.filter(Boolean))).sort().join(',');
|
|
}
|
|
|
|
function evictOldSubscriptions() {
|
|
if (activeSubscriptions.size <= maxSubscriptions) return;
|
|
const overflow = activeSubscriptions.size - maxSubscriptions;
|
|
const oldestKeys = Array.from(activeSubscriptions.entries())
|
|
.sort((a, b) => (a[1].timestamp || 0) - (b[1].timestamp || 0))
|
|
.slice(0, overflow)
|
|
.map(([key]) => key);
|
|
|
|
oldestKeys.forEach((key) => {
|
|
const record = activeSubscriptions.get(key);
|
|
if (record?.subId) interactions.unsubscribeFromInteractions(record.subId);
|
|
activeSubscriptions.delete(key);
|
|
});
|
|
}
|
|
|
|
function handleUpdate(postId, state, event, currentPubkey) {
|
|
interactions.updateInteractionBar(postId, state);
|
|
// Only render inline comments if comments are visible (the new feed
|
|
// sets commentsVisible=false to avoid inline reply rendering).
|
|
if (event && event.kind === 1 && interactions.isCommentEvent?.(event, postId) && interactions.getCommentsVisible?.()) {
|
|
interactions.addCommentToPost(postId, {
|
|
id: event.id,
|
|
pubkey: event.pubkey,
|
|
content: event.content,
|
|
created_at: event.created_at,
|
|
tags: event.tags
|
|
}, currentPubkey);
|
|
}
|
|
}
|
|
|
|
function wireInteractions(postIds = [], options = {}) {
|
|
const { currentPubkey = null } = options;
|
|
const normalized = Array.from(new Set(postIds.filter(Boolean)));
|
|
if (normalized.length === 0) return null;
|
|
|
|
interactions.fetchExistingInteractions(normalized, {
|
|
ndkFetchEvents,
|
|
queryCache,
|
|
currentPubkey,
|
|
onUpdate: (postId, state, event) => handleUpdate(postId, state, event, currentPubkey)
|
|
});
|
|
|
|
const key = makeSubKey(normalized);
|
|
const existing = activeSubscriptions.get(key);
|
|
if (existing?.subId) {
|
|
existing.timestamp = Date.now();
|
|
return existing.subId;
|
|
}
|
|
|
|
const subId = interactions.subscribeToInteractions(normalized, {
|
|
currentPubkey,
|
|
subscribeFn: subscribe,
|
|
onUpdate: (postId, state, event) => handleUpdate(postId, state, event, currentPubkey)
|
|
});
|
|
|
|
if (subId) {
|
|
activeSubscriptions.set(key, { subId, timestamp: Date.now() });
|
|
evictOldSubscriptions();
|
|
}
|
|
|
|
return subId;
|
|
}
|
|
|
|
function createCard(eventData, options = {}) {
|
|
const {
|
|
currentPubkey = null,
|
|
showHeader = true,
|
|
isCompact = false,
|
|
autoWire = true,
|
|
autoplayVideo = false
|
|
} = options;
|
|
|
|
const cardEl = interactions.renderPostItem(eventData, {
|
|
currentPubkey,
|
|
showHeader,
|
|
isCompact,
|
|
autoplayVideo
|
|
});
|
|
|
|
if (autoWire && eventData?.id) {
|
|
wireInteractions([eventData.id], { currentPubkey });
|
|
}
|
|
|
|
return cardEl;
|
|
}
|
|
|
|
function destroy() {
|
|
for (const { subId } of activeSubscriptions.values()) {
|
|
if (subId) interactions.unsubscribeFromInteractions(subId);
|
|
}
|
|
activeSubscriptions.clear();
|
|
}
|
|
|
|
return {
|
|
createCard,
|
|
createCompactCard: (eventData, options = {}) => createCard(eventData, { ...options, isCompact: true }),
|
|
wireInteractions,
|
|
destroy,
|
|
getCommentsVisible: interactions.getCommentsVisible,
|
|
setCommentsVisible: interactions.setCommentsVisible,
|
|
onCommentsVisibilityChange: interactions.onCommentsVisibilityChange,
|
|
prewarmProfileCache: interactions.prewarmProfileCache,
|
|
seedProfileCache: interactions.seedProfileCache
|
|
};
|
|
}
|
|
|
|
export default initPostCards;
|