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

166 lines
5 KiB
JavaScript
Raw Normal View History

2021-05-20 16:28:08 +00:00
import React, { useContext, useMemo } from "react"
2021-05-20 09:39:40 +00:00
import FormLabelled from "../base/FormLabelled"
import FormLabel from "../base/formparts/FormLabel"
import BoxFullScrollable from "../base/BoxFullScrollable"
2021-05-21 14:37:30 +00:00
import ContextRepositoryViewer from "../../contexts/ContextRepositoryViewer"
import useStrings from "../../hooks/useStrings"
2021-05-20 09:39:40 +00:00
2021-05-21 14:37:30 +00:00
export default function BoxVisualizationStats({ ...props }) {
const strings = useStrings()
const { tweets, words, rawTweets } = useContext(ContextRepositoryViewer)
2021-05-21 14:37:30 +00:00
2021-05-20 10:15:13 +00:00
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(
2021-05-21 14:37:30 +00:00
() => tweetCount / rawTweets.length * 100,
[tweetCount, rawTweets],
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
}
2021-05-20 13:34:05 +00:00
return words.map(word => word.value).reduce((a, b) => a + b)
},
[words],
2021-05-20 10:15:13 +00:00
)
const mostPopularWord = useMemo(
() => {
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
return (
2021-05-20 16:28:08 +00:00
<BoxFullScrollable header={strings.stats} {...props}>
2021-05-20 09:39:40 +00:00
<FormLabelled>
2021-05-20 16:28:08 +00:00
<FormLabel text={strings.totTweets}>
2021-05-21 14:37:30 +00:00
<b>{rawTweets.length}</b>
2021-05-20 09:39:40 +00:00
</FormLabel>
2021-05-20 16:28:08 +00:00
<FormLabel text={strings.dispTweets}>
2021-05-20 09:39:40 +00:00
<b>{tweetCount}</b>
</FormLabel>
2021-05-20 16:28:08 +00:00
<FormLabel text={strings.dispTweetsPerc}>
2021-05-20 09:39:40 +00:00
<b>{tweetPct.toFixed(2)}%</b>
</FormLabel>
2021-05-20 16:28:08 +00:00
<FormLabel text={strings.locTweets}>
2021-05-20 09:39:40 +00:00
<b>{tweetLocationCount}</b>
</FormLabel>
2021-05-20 16:28:08 +00:00
<FormLabel text={strings.locTweetsPerc}>
2021-05-20 09:39:40 +00:00
<b>{tweetLocationPct.toFixed(2)}%</b>
</FormLabel>
2021-05-20 16:28:08 +00:00
<FormLabel text={strings.contTweets}>
2021-05-20 09:39:40 +00:00
<b>{tweetContentCount}</b>
</FormLabel>
2021-05-20 16:31:01 +00:00
<FormLabel text={strings.contTweetsPerc}>
2021-05-20 09:39:40 +00:00
<b>{tweetContentPct.toFixed(2)}%</b>
</FormLabel>
2021-05-20 16:28:08 +00:00
<FormLabel text={strings.wordCount}>
2021-05-20 09:39:40 +00:00
<b>{wordCount}</b>
</FormLabel>
2021-05-20 16:28:08 +00:00
<FormLabel text={strings.wordPop}>
2021-05-20 09:39:40 +00:00
<b>{mostPopularWord}</b>
</FormLabel>
2021-05-20 16:28:08 +00:00
<FormLabel text={strings.imgTweets}>
2021-05-20 09:39:40 +00:00
<b>🚧</b>
</FormLabel>
2021-05-20 16:28:08 +00:00
<FormLabel text={strings.imgTweetsPerc}>
2021-05-20 09:39:40 +00:00
<b>🚧</b>
</FormLabel>
2021-05-20 16:28:08 +00:00
<FormLabel text={strings.postUniq}>
2021-05-20 10:15:13 +00:00
<b>{uniqueUsersCount}</b>
</FormLabel>
2021-05-20 16:28:08 +00:00
<FormLabel text={strings.postPop}>
<b>{mostActiveUser
? `${mostActiveUser.user} (${mostActiveUser.count} tweet${mostActiveUser.count === 1
? ""
: "s"})`
: "❌"}</b>
2021-05-20 09:39:40 +00:00
</FormLabel>
</FormLabelled>
</BoxFullScrollable>
)
}