From 71f15c0cdc705131e1a05a25398106f63c2fb7a0 Mon Sep 17 00:00:00 2001 From: Priyanshu Verma Date: Sun, 3 Nov 2024 17:20:07 +0000 Subject: [PATCH] feat: Analytics information for chatbot and images --- client/src/pages/MyChatbots.tsx | 48 +++++++++++++++++++++++++++++++++ client/src/pages/MyImages.tsx | 47 ++++++++++++++++++++++++++++++++ 2 files changed, 95 insertions(+) diff --git a/client/src/pages/MyChatbots.tsx b/client/src/pages/MyChatbots.tsx index e0c3516..b11af04 100644 --- a/client/src/pages/MyChatbots.tsx +++ b/client/src/pages/MyChatbots.tsx @@ -47,12 +47,60 @@ export default function MyChatbotsPage() { ); } + // Calculate analytics + const totalLikes = + botsData?.my_bots?.reduce((sum, bot) => sum + bot.likes, 0) || 0; + const totalReports = + botsData?.my_bots?.reduce((sum, bot) => sum + bot.reports, 0) || 0; + const topRankedBot = (botsData?.my_bots || []).reduce( + (topBot, currentBot) => { + return currentBot.likes > (topBot?.likes || 0) ? currentBot : topBot; + }, + botsData?.my_bots?.[0] + ); return (
+ + +

Analytics Summary

+
+ +
+ + Total Likes: + {totalLikes} +
+
+ + Total Reports: + {totalReports} +
+ {topRankedBot && ( +
+ + + + {topRankedBot.latest_version.name.slice(0, 2).toUpperCase()} + + +

Top Ranked Bot

+

+ {topRankedBot.latest_version.name} +

+

+ Likes: {topRankedBot.likes} +

+
+ )} +
+

My Images

{botsLoading ? ( diff --git a/client/src/pages/MyImages.tsx b/client/src/pages/MyImages.tsx index f4fe546..efc5da7 100644 --- a/client/src/pages/MyImages.tsx +++ b/client/src/pages/MyImages.tsx @@ -13,6 +13,7 @@ import { imageSrc } from "@/lib/utils"; import { useDeleteChatbotModal } from "@/stores/modal-store"; import { useMutation, useQuery, useQueryClient } from "@tanstack/react-query"; import { Flag, Heart, Send, Trash2 } from "lucide-react"; +import { useMemo } from "react"; export default function MyImagesPage() { const { loading, user } = useAuth(); @@ -33,6 +34,24 @@ export default function MyImagesPage() { mutationFn: publishObj, onSuccess: () => qc.invalidateQueries({ queryKey: ["my_images"] }), }); + // Calculate analytics data + const { totalLikes, totalReports, topRankedImage } = useMemo(() => { + if (!imagesData?.my_images?.length) { + return { totalLikes: 0, totalReports: 0, topRankedImage: null }; + } + const totalLikes = imagesData.my_images.reduce( + (sum, image) => sum + image.likes, + 0 + ); + const totalReports = imagesData.my_images.reduce( + (sum, image) => sum + image.reports, + 0 + ); + const topRankedImage = imagesData.my_images.reduce((topImage, image) => + image.likes > (topImage?.likes || 0) ? image : topImage + ); + return { totalLikes, totalReports, topRankedImage }; + }, [imagesData]); if (user == null || loading) { return ( @@ -48,6 +67,34 @@ export default function MyImagesPage() {
+ + +

Image Analytics

+
+ +
+ + Total Likes: + {totalLikes} +
+
+ + Total Reports: + {totalReports} +
+ {topRankedImage && ( +
+ Top Image: + {topRankedImage.prompt} + {topRankedImage.likes} Likes +
+ )} +
+

My Images

{imagesLoading ? (