mirror of
https://github.com/smolgrrr/TAO.git
synced 2024-09-20 01:11:25 +00:00
fix boards
This commit is contained in:
parent
89c58d209c
commit
eef394a388
@ -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
|
||||||
|
@ -268,46 +268,90 @@ 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>();
|
||||||
const notes = new Set<string>();
|
const notes = new Set<string>();
|
||||||
const prefix = Math.floor(16 / 4); // 4 bits in each '0' character
|
const prefix = Math.floor(16 / 4); // 4 bits in each '0' character
|
||||||
sub({ // get past events
|
sub({ // get past events
|
||||||
cb: (evt, relay) => {
|
cb: (evt, relay) => {
|
||||||
pubkeys.add(evt.pubkey);
|
pubkeys.add(evt.pubkey);
|
||||||
notes.add(evt.id);
|
notes.add(evt.id);
|
||||||
onEvent(evt, relay);
|
onEvent(evt, relay);
|
||||||
},
|
},
|
||||||
filter: {
|
filter: {
|
||||||
...(prefix && { ids: ['0'.repeat(prefix)] }),
|
...(prefix && { ids: ['0'.repeat(prefix)] }),
|
||||||
"#d": [board],
|
"#d": [board],
|
||||||
kinds: [1, 6],
|
kinds: [1, 6],
|
||||||
since: Math.floor((Date.now() * 0.001) - (age * 60 * 60)),
|
since: Math.floor((Date.now() * 0.001) - (age * 60 * 60)),
|
||||||
limit: 500,
|
limit: 500,
|
||||||
},
|
},
|
||||||
unsub: true
|
unsub: true
|
||||||
});
|
});
|
||||||
|
sub({
|
||||||
// subscribe to future notes, reactions and profile updates
|
cb: (evt, relay) => {
|
||||||
sub({
|
onEvent(evt, relay);
|
||||||
cb: (evt, relay) => {
|
},
|
||||||
onEvent(evt, relay);
|
filter: {
|
||||||
if (
|
authors: [board],
|
||||||
evt.kind !== 1
|
kinds: [1, 7],
|
||||||
|| pubkeys.has(evt.pubkey)
|
limit: 25,
|
||||||
) {
|
},
|
||||||
return;
|
unsub: true,
|
||||||
}
|
});
|
||||||
},
|
|
||||||
filter: {
|
setTimeout(() => {
|
||||||
...(prefix && { ids: ['0'.repeat(prefix)] }),
|
// get profile info
|
||||||
"#d": [board],
|
sub({
|
||||||
kinds: [1],
|
cb: onEvent,
|
||||||
since: now,
|
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
|
||||||
|
sub({
|
||||||
|
cb: (evt, relay) => {
|
||||||
|
onEvent(evt, relay);
|
||||||
|
if (
|
||||||
|
evt.kind !== 1
|
||||||
|
|| pubkeys.has(evt.pubkey)
|
||||||
|
) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
subOnce({ // get profile data
|
||||||
|
relay,
|
||||||
|
cb: onEvent,
|
||||||
|
filter: {
|
||||||
|
authors: [evt.pubkey],
|
||||||
|
kinds: [0],
|
||||||
|
limit: 1,
|
||||||
|
}
|
||||||
|
});
|
||||||
|
},
|
||||||
|
filter: {
|
||||||
|
...(prefix && { ids: ['0'.repeat(prefix)] }),
|
||||||
|
kinds: [1],
|
||||||
|
since: now,
|
||||||
|
},
|
||||||
|
});
|
||||||
};
|
};
|
||||||
|
|
||||||
export const subProfile = (
|
export const subProfile = (
|
||||||
|
Loading…
Reference in New Issue
Block a user