fix boards

This commit is contained in:
smolgrrr 2024-01-31 22:26:09 +11:00
parent 89c58d209c
commit eef394a388
2 changed files with 102 additions and 55 deletions

View File

@ -11,25 +11,14 @@ import { useParams } from "react-router-dom";
const DEFAULT_DIFFICULTY = 20; const DEFAULT_DIFFICULTY = 20;
const useUniqEvents = (pubkey: string) => {
const Board = () => {
const { id } = useParams();
const filterDifficulty = localStorage.getItem("filterDifficulty") || DEFAULT_DIFFICULTY;
const age = Number(localStorage.getItem("age")) || 24;
const [sortByTime, setSortByTime] = useState<boolean>(localStorage.getItem('sortBy') !== 'false');
const [setAnon, setSetAnon] = useState<boolean>(localStorage.getItem('anonMode') !== 'false');
let decodeResult = nip19.decode(id as string);
let pubkey = decodeResult.data as string;
const [delayedSort, setDelayedSort] = useState(false)
const [events, setEvents] = useState<Event[]>([]); const [events, setEvents] = useState<Event[]>([]);
const age = Number(localStorage.getItem("age")) || 24;
useEffect(() => { useEffect(() => {
const onEvent = (event: Event) => setEvents((prevEvents) => [...prevEvents, event]); const onEvent = (event: Event) => setEvents((prevEvents) => [...prevEvents, event]);
console.log(events) console.log(events)
const unsubscribe = subBoardFeed(pubkey, onEvent, age); const unsubscribe = subBoardFeed(pubkey, onEvent, age);
subProfile(pubkey, onEvent)
return unsubscribe; return unsubscribe;
}, [pubkey]); }, [pubkey]);
@ -38,6 +27,22 @@ const Board = () => {
const noteEvents = uniqEvents.filter(event => event.kind === 1 || event.kind === 6); const noteEvents = uniqEvents.filter(event => event.kind === 1 || event.kind === 6);
const metadataEvents = uniqEvents.filter(event => event.kind === 0); const metadataEvents = uniqEvents.filter(event => event.kind === 0);
const pinnedEvents = uniqEvents.filter(event => event.pubkey === pubkey && !event.tags.some((tag) => tag[0] === "e"));
return { pinnedEvents, noteEvents, metadataEvents };
};
const Board = () => {
const { id } = useParams();
const filterDifficulty = localStorage.getItem("filterDifficulty") || DEFAULT_DIFFICULTY;
const [sortByTime, setSortByTime] = useState<boolean>(localStorage.getItem('sortBy') !== 'false');
const [setAnon, setSetAnon] = useState<boolean>(localStorage.getItem('anonMode') !== 'false');
let decodeResult = nip19.decode(id as string);
let pubkey = decodeResult.data as string;
const {pinnedEvents, noteEvents, metadataEvents } = useUniqEvents(pubkey);
const [delayedSort, setDelayedSort] = useState(false)
const postEvents: Event[] = noteEvents const postEvents: Event[] = noteEvents
.filter((event) => .filter((event) =>
@ -60,8 +65,6 @@ const Board = () => {
sortByTime ? b.created_at - a.created_at : verifyPow(b) - verifyPow(a) 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) { if (delayedSort) {
sortedEvents = sortedEvents.filter( sortedEvents = sortedEvents.filter(
!setAnon ? (e) => !metadataEvents.some((metadataEvent) => metadataEvent.pubkey === e.pubkey) : () => true !setAnon ? (e) => !metadataEvents.some((metadataEvent) => metadataEvent.pubkey === e.pubkey) : () => true

View File

@ -268,7 +268,7 @@ export const subBoardFeed = (
onEvent: SubCallback, onEvent: SubCallback,
age: number age: number
) => { ) => {
console.info('subscribe to board'); console.info('subscribe to board feed');
unsubAll(); unsubAll();
const now = Math.floor(Date.now() * 0.001); const now = Math.floor(Date.now() * 0.001);
const pubkeys = new Set<string>(); const pubkeys = new Set<string>();
@ -289,6 +289,42 @@ export const subBoardFeed = (
}, },
unsub: true unsub: true
}); });
sub({
cb: (evt, relay) => {
onEvent(evt, relay);
},
filter: {
authors: [board],
kinds: [1, 7],
limit: 25,
},
unsub: true,
});
setTimeout(() => {
// get profile info
sub({
cb: onEvent,
filter: {
authors: Array.from(pubkeys),
kinds: [0],
limit: pubkeys.size,
},
unsub: true,
});
pubkeys.clear();
sub({
cb: onEvent,
filter: {
'#e': Array.from(notes),
kinds: [1],
},
unsub: true,
});
notes.clear();
}, 2000);
// subscribe to future notes, reactions and profile updates // subscribe to future notes, reactions and profile updates
sub({ sub({
@ -300,10 +336,18 @@ export const subBoardFeed = (
) { ) {
return; return;
} }
subOnce({ // get profile data
relay,
cb: onEvent,
filter: {
authors: [evt.pubkey],
kinds: [0],
limit: 1,
}
});
}, },
filter: { filter: {
...(prefix && { ids: ['0'.repeat(prefix)] }), ...(prefix && { ids: ['0'.repeat(prefix)] }),
"#d": [board],
kinds: [1], kinds: [1],
since: now, since: now,
}, },