MCPcopy Create free account
hub / github.com/block/buzz / useInboxThreadContext

Function useInboxThreadContext

desktop/src/features/home/useInboxThreadContext.ts:31–183  ·  view source on GitHub ↗
(
  item: FeedItem | null,
  channelMessages: RelayEvent[] | undefined,
)

Source from the content-addressed store, hash-verified

29}
30
31export function useInboxThreadContext(
32 item: FeedItem | null,
33 channelMessages: RelayEvent[] | undefined,
34): InboxThreadContextResult {
35 const [fetchedEvents, setFetchedEvents] = React.useState<RelayEvent[]>([]);
36 const [isLoading, setIsLoading] = React.useState(false);
37
38 const selectedEvent = React.useMemo(
39 () => (item ? relayEventFromFeedItem(item) : null),
40 [item],
41 );
42
43 const selectedThreadRootId = selectedEvent
44 ? getThreadRootId(selectedEvent)
45 : null;
46 const selectedParentId = selectedEvent
47 ? getThreadReference(selectedEvent.tags).parentId
48 : null;
49 const selectedChannelId = item?.channelId ?? null;
50
51 React.useEffect(() => {
52 let isCancelled = false;
53
54 if (!selectedEvent || !selectedThreadRootId) {
55 setFetchedEvents([]);
56 setIsLoading(false);
57 return () => {
58 isCancelled = true;
59 };
60 }
61
62 async function loadContext() {
63 const targetEvent = selectedEvent;
64 const threadRootId = selectedThreadRootId;
65 if (!targetEvent || !threadRootId) {
66 return;
67 }
68
69 setIsLoading(true);
70
71 try {
72 const selection = {
73 selectedChannelId,
74 selectedEventId: targetEvent.id,
75 selectedParentId,
76 selectedThreadRootId: threadRootId,
77 };
78 const eventIds = new Set<string>([threadRootId]);
79 if (selectedParentId) {
80 eventIds.add(selectedParentId);
81 }
82
83 const ancestorEventsPromise = Promise.all(
84 [...eventIds]
85 .filter((eventId) => eventId !== targetEvent.id)
86 .map(async (eventId) => {
87 try {
88 return await getEventById(eventId);

Callers 1

HomeViewFunction · 0.90

Calls 6

relayEventFromFeedItemFunction · 0.90
getThreadReferenceFunction · 0.90
getThreadRootIdFunction · 0.85
loadContextFunction · 0.85
dedupeEventsFunction · 0.85

Tested by

no test coverage detected