import React, { useContext, useMemo } from "react" import FormLabelled from "../base/FormLabelled" import FormLabel from "../base/formparts/FormLabel" import BoxFullScrollable from "../base/BoxFullScrollable" import ContextRepositoryViewer from "../../contexts/ContextRepositoryViewer" import useStrings from "../../hooks/useStrings" export default function BoxVisualizationStats({ ...props }) { const strings = useStrings() const { tweets, words, rawTweets } = useContext(ContextRepositoryViewer) const tweetCount = useMemo( () => tweets.length, [tweets], ) const tweetPct = useMemo( () => tweetCount / rawTweets.length * 100, [tweetCount, rawTweets], ) const tweetLocationCount = useMemo( () => tweets.filter(tweet => tweet.location).length, [tweets], ) const tweetLocationPct = useMemo( () => tweetLocationCount / tweetCount * 100, [tweetLocationCount, tweetCount], ) const tweetContent = useMemo( () => tweets.filter(tweet => tweet.content), [tweets], ) const tweetContentCount = useMemo( () => tweetContent.length, [tweetContent], ) const tweetContentPct = useMemo( () => tweetContentCount / tweetCount * 100, [tweetContentCount, tweetCount], ) const wordCount = useMemo( () => { if(words.length === 0) { return 0 } return words.map(word => word.value).reduce((a, b) => a + b) }, [words], ) const mostPopularWord = useMemo( () => { if(words.length === 0) { return "❌" } return words.sort((wa, wb) => { if(wa.value > wb.value) { return -1 } if(wa.value < wb.value) { return 1 } return 0 })[0].text }, [words], ) const users = useMemo( () => tweets.map(tweet => tweet.poster), [tweets], ) const uniqueUsers = useMemo( () => [...new Set(users)], [users], ) const uniqueUsersCount = useMemo( () => uniqueUsers.length, [uniqueUsers], ) const mostActiveUser = useMemo( () => { if(uniqueUsers.length === 0) { return null } return uniqueUsers.map(user => { return { user: user, count: tweets.filter(tweet => tweet.poster === user).length, } }).sort((a, b) => { if(a.count > b.count) { return -1 } if(a.count < b.count) { return 1 } return 0 })[0] }, [uniqueUsers, tweets], ) // TODO: missing stats return ( {rawTweets.length} {tweetCount} {tweetPct.toFixed(2)}% {tweetLocationCount} {tweetLocationPct.toFixed(2)}% {tweetContentCount} {tweetContentPct.toFixed(2)}% {wordCount} {mostPopularWord} 🚧 🚧 {uniqueUsersCount} {mostActiveUser ? `${mostActiveUser.user} (${mostActiveUser.count} tweet${mostActiveUser.count === 1 ? "" : "s"})` : "❌"} ) }