import React, { useState, useEffect } from "react"
import { Tab, Image, Grid, Button, Message } from "semantic-ui-react"
import { Redirect, useHistory } from "react-router-dom"
import { useQuery } from "@apollo/react-hooks"
import { useParams } from "react-router-dom"
import { searchForUser } from "../../graphql/queries/searchForUser"
import { userLean } from "../../graphql/queries/userLean"
import PlayerXRankStats from "../xsearch/PlayerXRankStats"
import BuildTab from "./BuildTab"
import Loading from "../common/Loading"
import Error from "../common/Error"
import { useQueryParam, NumberParam } from "use-query-params"
import Settings from "./Settings"
import ProfileLists from "./ProfileLists"
const UserPage = () => {
const { id } = useParams()
const history = useHistory()
const [tab, setTab] = useQueryParam("tab", NumberParam)
const { data, error, loading } = useQuery(searchForUser, {
variables: isNaN(id) ? { custom_url: id } : { discord_id: id },
})
const userLeanQuery = useQuery(userLean)
const [activeIndex, setActiveIndex] = useState(tab ? tab : 0)
const [showSettings, setShowSettings] = useState(false)
const [successMsg, setSuccessMsg] = useState(null)
const [errorMsg, setErrorMsg] = useState(null)
const [imageError, setImageError] = useState(false)
const handleError = error => {
setErrorMsg(error.message)
setTimeout(() => {
setErrorMsg(null)
}, 10000)
}
const handleSuccess = () => {
setShowSettings(false)
setSuccessMsg("Profile successfully updated!")
setTimeout(() => {
setSuccessMsg(null)
}, 10000)
}
useEffect(() => {
if (loading || !data || !data.searchForUser) return
document.title = `${data.searchForUser.username} - sendou.ink`
if (data.searchForUser.custom_url)
history.replace(`/u/${data.searchForUser.custom_url}`)
return () => setImageError(false)
}, [loading, data, history])
if (loading || userLeanQuery.loading) return