import { configureAbly, useChannel } from "@ably-labs/react-hooks"; import { env } from "~/env.mjs"; import { api } from "~/utils/api"; const Stats = () => { configureAbly({ key: env.NEXT_PUBLIC_ABLY_PUBLIC_KEY, recover: (_, cb) => { cb(true); }, }); const [] = useChannel( `${env.NEXT_PUBLIC_APP_ENV}-stats`, () => void refetchData() ); const { data: usersCount, isLoading: usersCountLoading, isFetching: usersCountFetching, refetch: refetchUsersCount, } = api.rest.userCount.useQuery(); const { data: roomsCount, isLoading: roomsCountLoading, isFetching: roomsCountFetching, refetch: refetchRoomsCount, } = api.rest.roomCount.useQuery(); const { data: votesCount, isLoading: votesCountLoading, isFetching: votesCountFetching, refetch: refetchVotesCount, } = api.rest.voteCount.useQuery(); const refetchData = async () => { await Promise.all([ refetchUsersCount(), refetchRoomsCount(), refetchVotesCount(), ]); }; return (
Users
{usersCountLoading || usersCountFetching ? ( ) : ( <>{usersCount ? usersCount : "0"} )}
Rooms
{roomsCountLoading || roomsCountFetching ? ( ) : ( <>{roomsCount ? roomsCount : "0"} )}
Votes
{votesCountLoading || votesCountFetching ? ( ) : ( <>{votesCount ? votesCount : "0"} )}
); }; export default Stats;