import React, { useContext, useMemo, useState } from "react" import Style from "./RepositoryViewer.module.css" import ContextLanguage from "../../contexts/ContextLanguage" import useBackendResource from "../../hooks/useBackendResource" import useBackendViewset from "../../hooks/useBackendViewset" import objectToWordcloudFormat from "../../utils/objectToWordcloudFormat" import countTweetWords from "../../utils/countTweetWords" import BoxHeader from "../base/BoxHeader" import Loading from "../base/Loading" import { FontAwesomeIcon } from "@fortawesome/react-fontawesome" import { faFolder, faFolderOpen } from "@fortawesome/free-solid-svg-icons" import BoxRepositoryTweets from "../interactive/BoxRepositoryTweets" import PickerVisualization from "../interactive/PickerVisualization" import BoxVisualizationWordcloud from "../interactive/BoxVisualizationWordcloud" import BoxVisualizationChart from "../interactive/BoxVisualizationChart" import BoxVisualizationMap from "../interactive/BoxVisualizationMap" import BoxVisualizationStats from "../interactive/BoxVisualizationStats" import PickerFilter from "../interactive/PickerFilter" import useArrayState from "../../hooks/useArrayState" import BoxFilters from "../interactive/BoxFilters" import ContextRepositoryViewer from "../../contexts/ContextRepositoryViewer" import BoxFilterContains from "../interactive/BoxFilterContains" import BoxFilterUser from "../interactive/BoxFilterUser" import BoxFilterHashtag from "../interactive/BoxFilterHashtag" import BoxFilterLocation from "../interactive/BoxFilterLocation" import useMapAreaState from "../../hooks/useMapAreaState" import BoxFilterDatetime from "../interactive/BoxFilterDatetime" import BoxFilterHasPlace from "../interactive/BoxFilterHasPlace" import BoxFilterHasImage from "../interactive/BoxFilterHasImage" import BoxFilterIsNotRetweet from "../interactive/BoxFilterIsNotRetweet" import PageWithHeader from "../base/layout/PageWithHeader" import AlertError from "../interactive/AlertError" export default function RepositoryViewer({ id, className, ...props }) { const { strings } = useContext(ContextLanguage) // State const [visualizationTab, setVisualizationTab] = useState("stats") const [filterTab, setFilterTab] = useState("contains") const { value: filters, setValue: setFilters, appendValue: appendFilter, spliceValue: spliceFilter, removeValue: removeFilter, } = useArrayState([]) // FIXME: this has a severe performance impact, investigate const mapViewHook = useMapAreaState() // Repository const { resource: repository, error: repositoryError, firstLoad: repositoryFirstLoad, } = useBackendResource( `/api/v1/repositories/${id}`, { retrieve: true, edit: true, destroy: true, action: false, }, ) // Tweets const { resources: tweets, error: tweetsError, firstLoad: tweetsFirstLoad, } = useBackendViewset( `/api/v1/repositories/${id}/tweets/`, "snowflake", { list: true, create: false, retrieve: false, edit: false, destroy: false, command: false, action: false, }, ) // Filtering const filteredTweets = useMemo( () => { let tempTweets = tweets for(const filter of filters) { tempTweets = tempTweets.filter(tweet => filter.exec(tweet)) } return tempTweets }, [tweets, filters] ) // Words const words = useMemo( () => objectToWordcloudFormat(countTweetWords(filteredTweets)), [filteredTweets], ) // Page header const header = useMemo( () => ( {repository?.name} ), [repository?.is_active, repository?.name] ) // Page contents const contents = useMemo( () => { if(!repositoryFirstLoad || !tweetsFirstLoad) { return } else if(repositoryError) { return } else if(tweetsError) { return } else { return (
{visualizationTab === "wordcloud" ? : null} {visualizationTab === "chart" ? : null} {visualizationTab === "map" ? : null} {visualizationTab === "stats" ? : null} {filterTab === "contains" ? : null} {filterTab === "hashtag" ? : null} {filterTab === "user" ? : null} {filterTab === "retweet" ? : null} {filterTab === "image" ? : null} {filterTab === "time" ? : null} {filterTab === "place" ? : null} {filterTab === "location" ? : null}
) } }, [repositoryFirstLoad, tweetsFirstLoad, visualizationTab, filterTab] ) return ( {contents} ) }