1
Fork 0
mirror of https://github.com/pds-nest/nest.git synced 2024-11-26 06:54:18 +00:00
pds-2021-g2-nest/nest_frontend/components/interactive/BoxVisualizationStats.js

154 lines
4.6 KiB
JavaScript
Raw Normal View History

2021-05-20 09:39:40 +00:00
import React, { useMemo } from "react"
import FormLabelled from "../base/FormLabelled"
import FormLabel from "../base/formparts/FormLabel"
import BoxFullScrollable from "../base/BoxFullScrollable"
2021-05-20 10:15:13 +00:00
export default function BoxVisualizationStats({ tweets, words, totalTweetCount, ...props }) {
const tweetCount = useMemo(
() => tweets.length,
2021-05-20 10:16:01 +00:00
[tweets],
2021-05-20 09:39:40 +00:00
)
2021-05-20 10:15:13 +00:00
const tweetPct = useMemo(
() => tweetCount / totalTweetCount * 100,
2021-05-20 10:16:01 +00:00
[tweetCount, totalTweetCount],
2021-05-20 10:15:13 +00:00
)
const tweetLocationCount = useMemo(
() => tweets.filter(tweet => tweet.location).length,
2021-05-20 10:16:01 +00:00
[tweets],
2021-05-20 10:15:13 +00:00
)
const tweetLocationPct = useMemo(
() => tweetLocationCount / tweetCount * 100,
2021-05-20 10:16:01 +00:00
[tweetLocationCount, tweetCount],
2021-05-20 10:15:13 +00:00
)
const tweetContent = useMemo(
() => tweets.filter(tweet => tweet.content),
2021-05-20 10:16:01 +00:00
[tweets],
2021-05-20 10:15:13 +00:00
)
const tweetContentCount = useMemo(
() => tweetContent.length,
[tweetContent],
)
const tweetContentPct = useMemo(
() => tweetContentCount / tweetCount * 100,
[tweetContentCount, tweetCount],
)
const wordCount = useMemo(
2021-05-20 13:34:05 +00:00
() => {
if(words.length === 0) return 0
return words.map(word => word.value).reduce((a, b) => a + b)
},
[words]
2021-05-20 10:15:13 +00:00
)
const mostPopularWord = useMemo(
() => {
2021-05-20 13:34:05 +00:00
if(words.length === 0) return "❌"
2021-05-20 10:15:13 +00:00
return words.sort((wa, wb) => {
2021-05-20 10:16:01 +00:00
if(wa.value > wb.value) {
return -1
}
if(wa.value < wb.value) {
return 1
}
2021-05-20 10:15:13 +00:00
return 0
})[0].text
},
2021-05-20 10:16:01 +00:00
[words],
2021-05-20 10:15:13 +00:00
)
const users = useMemo(
() => tweets.map(tweet => tweet.poster),
2021-05-20 10:16:01 +00:00
[tweets],
2021-05-20 10:15:13 +00:00
)
const uniqueUsers = useMemo(
() => [...new Set(users)],
2021-05-20 10:16:01 +00:00
[users],
2021-05-20 10:15:13 +00:00
)
const uniqueUsersCount = useMemo(
() => uniqueUsers.length,
2021-05-20 10:16:01 +00:00
[uniqueUsers],
2021-05-20 10:15:13 +00:00
)
const mostActiveUser = useMemo(
() => {
2021-05-20 10:16:01 +00:00
if(uniqueUsers.length === 0) {
return null
}
2021-05-20 10:15:13 +00:00
return uniqueUsers.map(user => {
return {
user: user,
2021-05-20 10:16:01 +00:00
count: tweets.filter(tweet => tweet.poster === user).length,
2021-05-20 10:15:13 +00:00
}
}).sort((a, b) => {
2021-05-20 10:16:01 +00:00
if(a.count > b.count) {
return -1
}
if(a.count < b.count) {
return 1
}
2021-05-20 10:15:13 +00:00
return 0
})[0]
},
2021-05-20 10:16:01 +00:00
[uniqueUsers, tweets],
2021-05-20 10:15:13 +00:00
)
2021-05-20 09:39:40 +00:00
2021-05-20 10:18:56 +00:00
// TODO: missing stats
2021-05-20 09:39:40 +00:00
// TODO: translate this
return (
<BoxFullScrollable header={"Stats"} {...props}>
<FormLabelled>
<FormLabel text={"Total tweets"}>
<b>{totalTweetCount}</b>
</FormLabel>
<FormLabel text={"Displayed tweets"}>
<b>{tweetCount}</b>
</FormLabel>
<FormLabel text={"% of displayed tweets"}>
<b>{tweetPct.toFixed(2)}%</b>
</FormLabel>
<FormLabel text={"Tweets with location"}>
<b>{tweetLocationCount}</b>
</FormLabel>
<FormLabel text={"% of tweets with location"}>
<b>{tweetLocationPct.toFixed(2)}%</b>
</FormLabel>
<FormLabel text={"Tweets with content"}>
<b>{tweetContentCount}</b>
</FormLabel>
<FormLabel text={"% of tweets with content"}>
<b>{tweetContentPct.toFixed(2)}%</b>
</FormLabel>
<FormLabel text={"Word count"}>
<b>{wordCount}</b>
</FormLabel>
<FormLabel text={"Most popular word"}>
<b>{mostPopularWord}</b>
</FormLabel>
<FormLabel text={"Tweets with image"}>
<b>🚧</b>
</FormLabel>
<FormLabel text={"% of tweets with image"}>
<b>🚧</b>
</FormLabel>
2021-05-20 10:15:13 +00:00
<FormLabel text={"Unique posters"}>
<b>{uniqueUsersCount}</b>
</FormLabel>
2021-05-20 10:18:56 +00:00
<FormLabel text={"Most active poster"}>
2021-05-20 13:34:05 +00:00
<b>{mostActiveUser ? `${mostActiveUser.user} (${mostActiveUser.count} tweet${mostActiveUser.count === 1 ? "" : "s"})` : "❌"}</b>
2021-05-20 09:39:40 +00:00
</FormLabel>
</FormLabelled>
</BoxFullScrollable>
)
}