mirror of
https://github.com/RoboSats/robosats.git
synced 2025-07-18 08:43:14 +00:00
Fix ratign counter
This commit is contained in:
@ -360,7 +360,7 @@ const CoordinatorDialog = ({ open = false, onClose, shortAlias }: Props): React.
|
||||
const { clientVersion, page, settings, origin } = useContext(AppContext);
|
||||
const { federation } = useContext<UseFederationStoreType>(FederationContext);
|
||||
|
||||
const [rating, setRating] = useState<number[]>([0, 0]);
|
||||
const [rating, setRating] = useState<Record<string, number>>({});
|
||||
const [averageRating, setAvergeRating] = useState<number>(0);
|
||||
const [expanded, setExpanded] = useState<'summary' | 'stats' | 'policies' | undefined>(undefined);
|
||||
const [coordinator, setCoordinator] = useState<Coordinator>(
|
||||
@ -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'}
|
||||
/>
|
||||
<Typography variant='caption' color='text.secondary'>
|
||||
{`(${rating[1]})`}
|
||||
{`(${Object.keys(rating).length ?? 0})`}
|
||||
</Typography>
|
||||
</Grid>
|
||||
</Grid>
|
||||
|
@ -26,9 +26,10 @@ const FederationTable = ({
|
||||
const { setOpen, windowSize, settings } = useContext<UseAppStoreType>(AppContext);
|
||||
const theme = useTheme();
|
||||
const [pageSize, setPageSize] = useState<number>(0);
|
||||
const [ratings, setRatings] = useState<Record<string, number[]>>(
|
||||
const [loading, setLoading] = useState<boolean>(true);
|
||||
const [ratings, setRatings] = useState<Record<string, Record<string, number>>>(
|
||||
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 = ({
|
||||
}}
|
||||
/>
|
||||
<Typography variant='caption' color='text.secondary'>
|
||||
{`(${coordinatorRating[1]})`}
|
||||
{`(${total})`}
|
||||
</Typography>
|
||||
</>
|
||||
)}
|
||||
@ -318,6 +320,7 @@ const FederationTable = ({
|
||||
}
|
||||
>
|
||||
<DataGrid
|
||||
loading={loading}
|
||||
sx={headerStyleFix}
|
||||
localeText={localeText}
|
||||
rowHeight={3.714 * theme.typography.fontSize}
|
||||
|
@ -132,15 +132,15 @@ class RoboPool {
|
||||
this.sendMessage(JSON.stringify(requestSuccess));
|
||||
};
|
||||
|
||||
subscribeRatings = (events: RoboPoolEvents): void => {
|
||||
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) => {
|
||||
|
Reference in New Issue
Block a user