()
| 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); |
| 89 | } catch { |
| 90 | return null; |
| 91 | } |
| 92 | }), |
| 93 | ); |
| 94 | |
| 95 | const descendantEventsPromise = |
| 96 | selectedChannelId && threadRootId |
| 97 | ? relayClient |
| 98 | .fetchEvents({ |
| 99 | "#e": [threadRootId], |
| 100 | "#h": [selectedChannelId], |
| 101 | kinds: [...HOME_MENTION_EVENT_KINDS], |
| 102 | limit: THREAD_CONTEXT_LIMIT, |
| 103 | }) |
| 104 | .catch(() => []) |
| 105 | : Promise.resolve([]); |
| 106 | const [ancestorEvents, descendantEvents] = await Promise.all([ |
| 107 | ancestorEventsPromise, |
| 108 | descendantEventsPromise, |
| 109 | ]); |
| 110 | |
| 111 | if (isCancelled) { |
| 112 | return; |
| 113 | } |
| 114 | |
| 115 | setFetchedEvents( |
| 116 | dedupeEvents( |
| 117 | [...ancestorEvents, ...descendantEvents].filter( |
| 118 | (event): event is RelayEvent => |
| 119 | event !== null && isInboxThreadContextEvent(event, selection), |
no test coverage detected