import React, { useMemo } from "react" import FormLabelled from "../base/FormLabelled" import FormLabel from "../base/formparts/FormLabel" import BoxFullScrollable from "../base/BoxFullScrollable" import tokenizeTweetWords from "../../utils/countTweetWords" export default function BoxVisualizationStats({ tweets, words, totalTweetCount, ...props }) { const tweetCount = useMemo( () => tweets.length, [tweets] ) const tweetPct = useMemo( () => tweetCount / totalTweetCount * 100, [tweetCount, totalTweetCount] ) 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], ) console.debug(words) const wordCount = useMemo( () => words.map(word => word.value).reduce((a, b) => a+b), [words] ) const mostPopularWord = useMemo( () => { 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: tweets with picture count // TODO: tweets with picture pct // TODO: translate this return ( {totalTweetCount} {tweetCount} {tweetPct.toFixed(2)}% {tweetLocationCount} {tweetLocationPct.toFixed(2)}% {tweetContentCount} {tweetContentPct.toFixed(2)}% {wordCount} {mostPopularWord} 🚧 🚧 {uniqueUsersCount} {mostActiveUser.user} ({mostActiveUser.count} tweets) ) }