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/routes/PageRepository.js

134 lines
4.2 KiB
JavaScript
Raw Normal View History

2021-05-19 00:05:20 +00:00
import React, { useMemo, useState } from "react"
2021-05-15 16:05:13 +00:00
import Style from "./PageRepository.module.css"
import classNames from "classnames"
2021-05-18 17:15:53 +00:00
import BoxRepositoryTweets from "../components/interactive/BoxRepositoryTweets"
import BoxWordcloud from "../components/interactive/BoxWordcloud"
2021-05-19 00:05:20 +00:00
import BoxHeader from "../components/base/BoxHeader"
import PickerVisualization from "../components/interactive/PickerVisualization"
import PickerFilter from "../components/interactive/PickerFilter"
import useBackendViewset from "../hooks/useBackendViewset"
import useBackendResource from "../hooks/useBackendResource"
import { faFolder, faFolderOpen, faTrash } from "@fortawesome/free-solid-svg-icons"
import {FontAwesomeIcon} from "@fortawesome/react-fontawesome"
import { useParams } from "react-router"
import Loading from "../components/base/Loading"
2021-05-15 16:05:13 +00:00
export default function PageRepository({ className, ...props }) {
const {id} = useParams()
2021-05-19 00:05:20 +00:00
const [visualizationTab, setVisualizationTab] = useState("wordcloud")
const [addFilterTab, setAddFilterTab] = useState("hashtag")
const repositoryBr = useBackendResource(
`/api/v1/repositories/${id}`,
{
retrieve: true,
edit: true,
destroy: true,
action: false,
}
)
const repository = repositoryBr.error ? null : repositoryBr.resource
const tweetsBv = useBackendViewset(
`/api/v1/repositories/${id}/tweets/`,
"snowflake",
{
list: true,
create: false,
retrieve: false,
edit: false,
destroy: false,
command: false,
action: false,
}
)
const tweets = tweetsBv.resources && tweetsBv.error ? [] : tweetsBv.resources
2021-05-19 00:05:20 +00:00
const words = useMemo(
() => {
let preprocessedWords = {}
for(const tweet of tweets) {
if(!tweet.content) {
continue
}
for(const word of tweet.content.toLowerCase().split(/\s+/)) {
if(!preprocessedWords.hasOwnProperty(word)) {
preprocessedWords[word] = 0
}
preprocessedWords[word] += 1
}
}
let processedWords = []
for(const word in preprocessedWords) {
if(!preprocessedWords.hasOwnProperty(word)) {
continue
}
processedWords.push({
text: word,
value: preprocessedWords[word]
})
}
2021-05-19 00:05:20 +00:00
return processedWords
},
[tweets]
)
let contents;
if(!repositoryBr.firstLoad || !tweetsBv.firstLoad) {
contents = <>
<BoxHeader className={Style.Header}>
<Loading/>
</BoxHeader>
</>
}
else if(repository === null) {
console.debug("repositoryBr: ", repositoryBr, ", tweetsBv: ", tweetsBv)
2021-05-19 00:05:20 +00:00
// TODO: Translate this!
contents = <>
<BoxHeader className={Style.Header}>
<FontAwesomeIcon icon={faTrash}/> <i>This repository was deleted.</i>
</BoxHeader>
</>
}
else {
contents = <>
2021-05-19 00:05:20 +00:00
<BoxHeader className={Style.Header}>
<FontAwesomeIcon icon={repository.is_active ? faFolderOpen : faFolder}/> {repository.name}
2021-05-19 00:05:20 +00:00
</BoxHeader>
<BoxRepositoryTweets
className={Style.Tweets}
tweets={tweets}
/>
<PickerVisualization
className={Style.VisualizationPicker}
currentTab={visualizationTab}
setTab={setVisualizationTab}
/>
{visualizationTab === "wordcloud" ?
<BoxWordcloud
className={Style.Wordcloud}
words={words}
/>
: null}
2021-05-19 00:05:20 +00:00
<PickerFilter
className={Style.FilterPicker}
currentTab={addFilterTab}
setTab={setAddFilterTab}
/>
</>
}
return (
<div className={classNames(Style.PageRepository, className)} {...props}>
{contents}
2021-05-15 16:05:13 +00:00
</div>
)
}