add pinned posts

This commit is contained in:
smolgrrr 2024-01-08 15:07:06 +11:00
parent 325edbee8b
commit 5661d9da0e
4 changed files with 43 additions and 52 deletions

View File

@ -1,7 +1,7 @@
import { useEffect, useState, useCallback } from "react";
import PostCard from "./Modals/NoteCard";
import { uniqBy } from "../utils/otherUtils"; // Assume getPow is a correct import now
import { subBoardFeed } from "../utils/subscriptions";
import { subBoardFeed, subProfile} from "../utils/subscriptions";
import { verifyPow } from "../utils/mine";
import { Event, nip19 } from "nostr-tools";
import NewNoteCard from "./Forms/PostFormCard";
@ -26,8 +26,9 @@ const Board = () => {
useEffect(() => {
const onEvent = (event: Event) => setEvents((prevEvents) => [...prevEvents, event]);
console.log(events[events.length])
console.log(events)
const unsubscribe = subBoardFeed(pubkey, onEvent);
subProfile(pubkey, onEvent)
return unsubscribe;
}, [pubkey]);
@ -58,6 +59,8 @@ const Board = () => {
sortByTime ? b.created_at - a.created_at : verifyPow(b) - verifyPow(a)
)
const pinnedEvents = uniqEvents.filter(event => event.pubkey === pubkey && !event.tags.some((tag) => tag[0] === "e"));
if (delayedSort) {
sortedEvents = sortedEvents.filter(
!setAnon ? (e) => !metadataEvents.some((metadataEvent) => metadataEvent.pubkey === e.pubkey) : () => true
@ -94,6 +97,15 @@ const Board = () => {
</div>
<OptionsBar sortByTime={sortByTime} setAnon={setAnon} toggleSort={toggleSort} toggleAnon={toggleAnon} />
<div className="grid grid-cols-1 md:grid-cols-3 lg:grid-cols-4 gap-4 p-4">
{pinnedEvents.map((event) => (
<div className="rounded-lg border border-red-900">
<PostCard
event={event}
metadata={metadataEvents.find((e) => e.pubkey === event.pubkey && e.kind === 0) || null}
replyCount={countReplies(event)}
/>
</div>
))}
{sortedEvents.map((event) => (
event.kind === 1 ?
<PostCard

View File

@ -1,11 +1,11 @@
import React, { useState } from 'react';
export const DefaultBoards = [
['Politically Incorrect', 'npub19znf32s8s7qpkpfrck0suyym3m3wtrwpnldj76u0qwjtms3dcftsqs6r87', 'pol'],
['Bitcoin', 'npub19nrn4l0s39kpwww7pgk9jddj8lzekqxmtrll8r2a57chtq3zx6sq00vetn', 'btc'],
['Vidya', 'npub19t2dt6deqaleq59fdaq576tnqdzwkyzwptxfa2tck0v66w29xagqe7yqll', 'v'],
['Technology', 'npub1mltf3r3tskdxfjlq6ltt2n73xs29wcza3sjfw75ggxz3p8fpcg4qe44h9v', 'g'],
['Television & Film', 'npub1cpeuaea3cymx42fmmx2ur82t5qnckqv85qy5q2nhzhxwzael5v4sksfe29', 'tv'],
['Technology', 'npub1qd7pdtkrdgd0239d7jtvjcdjtryy4vn98cnqhzl8pt9pcnt3u2eqll2sdz', 'g']
['Vidya', 'npub19t2dt6deqaleq59fdaq576tnqdzwkyzwptxfa2tck0v66w29xagqe7yqll', 'v'],
['Politically Incorrect', 'npub19znf32s8s7qpkpfrck0suyym3m3wtrwpnldj76u0qwjtms3dcftsqs6r87', 'pol']
];
const Boards = () => {

View File

@ -19,11 +19,6 @@ interface FormProps {
board?: string;
}
const tagMapping = {
'Reply': ['e', 'p'],
'Quote': ['q', 'p']
};
const NewNoteCard = ({
refEvent,
tagType,
@ -51,19 +46,18 @@ const NewNoteCard = ({
const [uploadingFile, setUploadingFile] = useState(false);
useEffect(() => {
if (refEvent && tagType && unsigned.tags.length === 1) {
if (refEvent && tagType) {
unsigned.tags = Array.from(new Set(unsigned.tags.concat(refEvent.tags)));
unsigned.tags.push(['p', refEvent.pubkey]);
if (tagType === 'Reply') {
unsigned.tags.push(['p', refEvent.pubkey]);
unsigned.tags.push(['e', refEvent.id, 'root']);
unsigned.tags.push(['e', refEvent.id, refEvent.tags.some(tag => tag[0] === 'e') ? 'root' : '']);
} else {
unsigned.tags = refEvent.tags
unsigned.tags.push(['p', refEvent.pubkey]);
if (tagType === 'Quote') {
setComment(comment + '\nnostr:' + nip19.noteEncode(refEvent.id));
unsigned.tags.push(['q', refEvent.id]);
unsigned.tags.push(['q', refEvent.id]);
} else {
unsigned.tags.push(['e', refEvent.id]);
unsigned.tags.push(['e', refEvent.id]);
}
}
}

View File

@ -213,40 +213,6 @@ export const subNotesOnce = (
}, 2000);
};
// /** quick subscribe to a note id (nip-19) */
// export const subNotifications = (
// pubkeys: string[],
// onEvent: SubCallback,
// ) => {
// const replyPubkeys = new Set<string>();
// sub({
// cb: (evt, relay) => {
// replyPubkeys.add(evt.pubkey);
// onEvent(evt, relay);
// },
// filter: {
// "#p": pubkeys,
// kinds: [1],
// limit: 50,
// },
// unsub: true,
// });
// setTimeout(() => {
// // get profile info
// sub({
// cb: onEvent,
// filter: {
// authors: Array.from(replyPubkeys),
// kinds: [0],
// limit: replyPubkeys.size,
// },
// unsub: true,
// });
// replyPubkeys.clear();
// }, 2000);
// };
const hasEventTag = (tag: string[]) => tag[0] === 'e';
const isReply = ([tag, , , marker]: string[]) => tag === 'e' && marker !== 'mention';
@ -342,3 +308,22 @@ export const subBoardFeed = (
},
});
};
export const subProfile = (
pubkey: string,
onEvent: SubCallback,
) => {
unsubAll();
sub({
cb: (evt, relay) => {
onEvent(evt, relay);
},
filter: {
authors: [pubkey],
kinds: [1, 7],
limit: 25,
},
unsub: true,
});
};