diff --git a/frontend/src/components/Dialogs/Coordinator.tsx b/frontend/src/components/Dialogs/Coordinator.tsx index 9bf0dd46..99d9d82c 100644 --- a/frontend/src/components/Dialogs/Coordinator.tsx +++ b/frontend/src/components/Dialogs/Coordinator.tsx @@ -360,7 +360,7 @@ const CoordinatorDialog = ({ open = false, onClose, shortAlias }: Props): React. const { clientVersion, page, settings, origin } = useContext(AppContext); const { federation } = useContext(FederationContext); - const [rating, setRating] = useState([0, 0]); + const [rating, setRating] = useState>({}); const [averageRating, setAvergeRating] = useState(0); const [expanded, setExpanded] = useState<'summary' | 'stats' | 'policies' | undefined>(undefined); const [coordinator, setCoordinator] = useState( @@ -374,7 +374,7 @@ const CoordinatorDialog = ({ open = false, onClose, shortAlias }: Props): React. useEffect(() => { setCoordinator(federation.getCoordinator(shortAlias ?? '')); - setRating([0, 0]); + setRating({}); setAvergeRating(0); }, [shortAlias]); @@ -391,10 +391,12 @@ const CoordinatorDialog = ({ open = false, onClose, shortAlias }: Props): React. const eventRating = event.tags.find((t) => t[0] === 'rating')?.[1]; if (eventRating) { setRating((prev) => { - const sum = prev[0]; - const count = prev[1] + 1; - prev = [sum + parseFloat(eventRating), count]; - setAvergeRating(sum / count); + prev[event.pubkey] = parseFloat(eventRating); + const totalRatings = Object.values(prev); + const sum: number = Object.values(prev).reduce((accumulator, currentValue) => { + return accumulator + currentValue; + }, 0); + setAvergeRating(sum / totalRatings.length); return prev; }); } @@ -403,6 +405,7 @@ const CoordinatorDialog = ({ open = false, onClose, shortAlias }: Props): React. oneose: () => {}, }, [coordinator.nostrHexPubkey], + coordinator.shortAlias, ); } coordinator?.loadInfo(); @@ -442,7 +445,7 @@ const CoordinatorDialog = ({ open = false, onClose, shortAlias }: Props): React. disabled={settings.connection !== 'nostr'} /> - {`(${rating[1]})`} + {`(${Object.keys(rating).length ?? 0})`} diff --git a/frontend/src/components/FederationTable/index.tsx b/frontend/src/components/FederationTable/index.tsx index 5d9b5b64..93c7aa1f 100644 --- a/frontend/src/components/FederationTable/index.tsx +++ b/frontend/src/components/FederationTable/index.tsx @@ -26,9 +26,10 @@ const FederationTable = ({ const { setOpen, windowSize, settings } = useContext(AppContext); const theme = useTheme(); const [pageSize, setPageSize] = useState(0); - const [ratings, setRatings] = useState>( + const [loading, setLoading] = useState(true); + const [ratings, setRatings] = useState>>( federation.getCoordinators().reduce((acc, coord) => { - if (coord.nostrHexPubkey) acc[coord.nostrHexPubkey] = [0, 0]; + if (coord.nostrHexPubkey) acc[coord.nostrHexPubkey] = {}; return acc; }, {}), ); @@ -57,7 +58,7 @@ const FederationTable = ({ const loadRatings: () => void = () => { if (settings.connection !== 'nostr') return; - + setLoading(true); federation.roboPool.subscribeRatings({ onevent: (event) => { const verfied = verifyCoordinatorToken(event); @@ -66,15 +67,13 @@ const FederationTable = ({ const rating = event.tags.find((t) => t[0] === 'rating')?.[1]; if (rating) { setRatings((prev) => { - const sum = prev[coordinatorPubKey][0]; - const count = prev[coordinatorPubKey][1]; - prev[coordinatorPubKey] = [sum + parseFloat(rating), count + 1]; + prev[coordinatorPubKey][event.pubkey] = parseFloat(rating); return prev; }); } } }, - oneose: () => {}, + oneose: () => setLoading(false), }); }; @@ -146,10 +145,13 @@ const FederationTable = ({ if (!coordinatorRating) return <>; - const average = - coordinatorRating && coordinatorRating[1] > 0 - ? coordinatorRating[0] / coordinatorRating[1] - : 0; + const totalRatings = Object.values(coordinatorRating); + const total = totalRatings.length; + const sum: number = Object.values(totalRatings).reduce((accumulator, currentValue) => { + return accumulator + currentValue; + }, 0); + const average = total < 1 ? 0 : sum / total; + return ( <> {mobile ? ( @@ -170,7 +172,7 @@ const FederationTable = ({ }} /> - {`(${coordinatorRating[1]})`} + {`(${total})`} )} @@ -318,6 +320,7 @@ const FederationTable = ({ } > { - const pubkeys = Object.values(defaultFederation) + subscribeRatings = (events: RoboPoolEvents, pubkeys?: string[], id?: string): void => { + const defaultPubkeys = Object.values(defaultFederation) .map((f) => f.nostrHexPubkey) .filter((item) => item !== undefined); const requestRatings = [ 'REQ', - 'subscribeRatings', - { kinds: [31986], '#p': pubkeys, since: 1746316800 }, + `subscribeRatings${id}`, + { kinds: [31986], '#p': pubkeys ?? defaultPubkeys, since: 1746316800 }, ]; this.messageHandlers.push((_url: string, messageEvent: MessageEvent) => {