nostr-hooks
Version:
React hooks for developing Nostr clients
35 lines • 1.49 kB
JavaScript
import { useEffect, useMemo } from 'react';
import { useSubscription } from '../../../hooks';
import { useNip29Store } from '../../../nip29/store';
const updateGroupRoles = useNip29Store.getState().updateGroupRoles;
export const useGroupRoles = (relay, groupId) => {
const subId = relay && groupId ? `${relay}-${groupId}-roles` : undefined;
const roles = useNip29Store((state) => subId && groupId ? state.groups[subId]?.[groupId]?.roles : undefined);
const { events, eose, createSubscription } = useSubscription(subId);
useEffect(() => {
if (!relay || !groupId || !subId)
return;
const filters = [{ kinds: [39003], '#d': [groupId], limit: 1 }];
const relayUrls = [relay];
const onEvent = (event) => {
const roles = event
.getMatchingTags('role')
.filter((t) => t.length > 1)
.map((t) => {
let r = { name: t[1] || '' };
if (t.length > 2)
r.description = t[2] || '';
return r;
});
updateGroupRoles(subId, groupId, roles);
};
createSubscription({ filters, relayUrls, onEvent });
}, [subId, relay, groupId, createSubscription]);
const isLoading = useMemo(() => (!events || events.length == 0) && !eose, [events, eose]);
return {
roles,
isLoadingRoles: isLoading,
rolesEvents: events,
};
};
//# sourceMappingURL=index.js.map