mirror of
https://github.com/RoboSats/robosats.git
synced 2025-07-18 16:53:16 +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 { clientVersion, page, settings, origin } = useContext(AppContext);
|
||||||
const { federation } = useContext<UseFederationStoreType>(FederationContext);
|
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 [averageRating, setAvergeRating] = useState<number>(0);
|
||||||
const [expanded, setExpanded] = useState<'summary' | 'stats' | 'policies' | undefined>(undefined);
|
const [expanded, setExpanded] = useState<'summary' | 'stats' | 'policies' | undefined>(undefined);
|
||||||
const [coordinator, setCoordinator] = useState<Coordinator>(
|
const [coordinator, setCoordinator] = useState<Coordinator>(
|
||||||
@ -374,7 +374,7 @@ const CoordinatorDialog = ({ open = false, onClose, shortAlias }: Props): React.
|
|||||||
|
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
setCoordinator(federation.getCoordinator(shortAlias ?? ''));
|
setCoordinator(federation.getCoordinator(shortAlias ?? ''));
|
||||||
setRating([0, 0]);
|
setRating({});
|
||||||
setAvergeRating(0);
|
setAvergeRating(0);
|
||||||
}, [shortAlias]);
|
}, [shortAlias]);
|
||||||
|
|
||||||
@ -391,10 +391,12 @@ const CoordinatorDialog = ({ open = false, onClose, shortAlias }: Props): React.
|
|||||||
const eventRating = event.tags.find((t) => t[0] === 'rating')?.[1];
|
const eventRating = event.tags.find((t) => t[0] === 'rating')?.[1];
|
||||||
if (eventRating) {
|
if (eventRating) {
|
||||||
setRating((prev) => {
|
setRating((prev) => {
|
||||||
const sum = prev[0];
|
prev[event.pubkey] = parseFloat(eventRating);
|
||||||
const count = prev[1] + 1;
|
const totalRatings = Object.values(prev);
|
||||||
prev = [sum + parseFloat(eventRating), count];
|
const sum: number = Object.values(prev).reduce((accumulator, currentValue) => {
|
||||||
setAvergeRating(sum / count);
|
return accumulator + currentValue;
|
||||||
|
}, 0);
|
||||||
|
setAvergeRating(sum / totalRatings.length);
|
||||||
return prev;
|
return prev;
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
@ -403,6 +405,7 @@ const CoordinatorDialog = ({ open = false, onClose, shortAlias }: Props): React.
|
|||||||
oneose: () => {},
|
oneose: () => {},
|
||||||
},
|
},
|
||||||
[coordinator.nostrHexPubkey],
|
[coordinator.nostrHexPubkey],
|
||||||
|
coordinator.shortAlias,
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
coordinator?.loadInfo();
|
coordinator?.loadInfo();
|
||||||
@ -442,7 +445,7 @@ const CoordinatorDialog = ({ open = false, onClose, shortAlias }: Props): React.
|
|||||||
disabled={settings.connection !== 'nostr'}
|
disabled={settings.connection !== 'nostr'}
|
||||||
/>
|
/>
|
||||||
<Typography variant='caption' color='text.secondary'>
|
<Typography variant='caption' color='text.secondary'>
|
||||||
{`(${rating[1]})`}
|
{`(${Object.keys(rating).length ?? 0})`}
|
||||||
</Typography>
|
</Typography>
|
||||||
</Grid>
|
</Grid>
|
||||||
</Grid>
|
</Grid>
|
||||||
|
@ -26,9 +26,10 @@ const FederationTable = ({
|
|||||||
const { setOpen, windowSize, settings } = useContext<UseAppStoreType>(AppContext);
|
const { setOpen, windowSize, settings } = useContext<UseAppStoreType>(AppContext);
|
||||||
const theme = useTheme();
|
const theme = useTheme();
|
||||||
const [pageSize, setPageSize] = useState<number>(0);
|
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) => {
|
federation.getCoordinators().reduce((acc, coord) => {
|
||||||
if (coord.nostrHexPubkey) acc[coord.nostrHexPubkey] = [0, 0];
|
if (coord.nostrHexPubkey) acc[coord.nostrHexPubkey] = {};
|
||||||
return acc;
|
return acc;
|
||||||
}, {}),
|
}, {}),
|
||||||
);
|
);
|
||||||
@ -57,7 +58,7 @@ const FederationTable = ({
|
|||||||
|
|
||||||
const loadRatings: () => void = () => {
|
const loadRatings: () => void = () => {
|
||||||
if (settings.connection !== 'nostr') return;
|
if (settings.connection !== 'nostr') return;
|
||||||
|
setLoading(true);
|
||||||
federation.roboPool.subscribeRatings({
|
federation.roboPool.subscribeRatings({
|
||||||
onevent: (event) => {
|
onevent: (event) => {
|
||||||
const verfied = verifyCoordinatorToken(event);
|
const verfied = verifyCoordinatorToken(event);
|
||||||
@ -66,15 +67,13 @@ const FederationTable = ({
|
|||||||
const rating = event.tags.find((t) => t[0] === 'rating')?.[1];
|
const rating = event.tags.find((t) => t[0] === 'rating')?.[1];
|
||||||
if (rating) {
|
if (rating) {
|
||||||
setRatings((prev) => {
|
setRatings((prev) => {
|
||||||
const sum = prev[coordinatorPubKey][0];
|
prev[coordinatorPubKey][event.pubkey] = parseFloat(rating);
|
||||||
const count = prev[coordinatorPubKey][1];
|
|
||||||
prev[coordinatorPubKey] = [sum + parseFloat(rating), count + 1];
|
|
||||||
return prev;
|
return prev;
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
oneose: () => {},
|
oneose: () => setLoading(false),
|
||||||
});
|
});
|
||||||
};
|
};
|
||||||
|
|
||||||
@ -146,10 +145,13 @@ const FederationTable = ({
|
|||||||
|
|
||||||
if (!coordinatorRating) return <></>;
|
if (!coordinatorRating) return <></>;
|
||||||
|
|
||||||
const average =
|
const totalRatings = Object.values(coordinatorRating);
|
||||||
coordinatorRating && coordinatorRating[1] > 0
|
const total = totalRatings.length;
|
||||||
? coordinatorRating[0] / coordinatorRating[1]
|
const sum: number = Object.values(totalRatings).reduce((accumulator, currentValue) => {
|
||||||
: 0;
|
return accumulator + currentValue;
|
||||||
|
}, 0);
|
||||||
|
const average = total < 1 ? 0 : sum / total;
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<>
|
<>
|
||||||
{mobile ? (
|
{mobile ? (
|
||||||
@ -170,7 +172,7 @@ const FederationTable = ({
|
|||||||
}}
|
}}
|
||||||
/>
|
/>
|
||||||
<Typography variant='caption' color='text.secondary'>
|
<Typography variant='caption' color='text.secondary'>
|
||||||
{`(${coordinatorRating[1]})`}
|
{`(${total})`}
|
||||||
</Typography>
|
</Typography>
|
||||||
</>
|
</>
|
||||||
)}
|
)}
|
||||||
@ -318,6 +320,7 @@ const FederationTable = ({
|
|||||||
}
|
}
|
||||||
>
|
>
|
||||||
<DataGrid
|
<DataGrid
|
||||||
|
loading={loading}
|
||||||
sx={headerStyleFix}
|
sx={headerStyleFix}
|
||||||
localeText={localeText}
|
localeText={localeText}
|
||||||
rowHeight={3.714 * theme.typography.fontSize}
|
rowHeight={3.714 * theme.typography.fontSize}
|
||||||
|
@ -132,15 +132,15 @@ class RoboPool {
|
|||||||
this.sendMessage(JSON.stringify(requestSuccess));
|
this.sendMessage(JSON.stringify(requestSuccess));
|
||||||
};
|
};
|
||||||
|
|
||||||
subscribeRatings = (events: RoboPoolEvents): void => {
|
subscribeRatings = (events: RoboPoolEvents, pubkeys?: string[], id?: string): void => {
|
||||||
const pubkeys = Object.values(defaultFederation)
|
const defaultPubkeys = Object.values(defaultFederation)
|
||||||
.map((f) => f.nostrHexPubkey)
|
.map((f) => f.nostrHexPubkey)
|
||||||
.filter((item) => item !== undefined);
|
.filter((item) => item !== undefined);
|
||||||
|
|
||||||
const requestRatings = [
|
const requestRatings = [
|
||||||
'REQ',
|
'REQ',
|
||||||
'subscribeRatings',
|
`subscribeRatings${id}`,
|
||||||
{ kinds: [31986], '#p': pubkeys, since: 1746316800 },
|
{ kinds: [31986], '#p': pubkeys ?? defaultPubkeys, since: 1746316800 },
|
||||||
];
|
];
|
||||||
|
|
||||||
this.messageHandlers.push((_url: string, messageEvent: MessageEvent) => {
|
this.messageHandlers.push((_url: string, messageEvent: MessageEvent) => {
|
||||||
|
Reference in New Issue
Block a user