import { useCallback, useEffect, useState } from "react"; import { useTranslation } from "react-i18next"; import { useNavigate } from "react-router-dom"; import * as utils from "../helpers/utils"; import userService from "../services/userService"; import { locationService } from "../services"; import { useAppSelector } from "../store"; import Icon from "./Icon"; import Dropdown from "./common/Dropdown"; import showResourcesDialog from "./ResourcesDialog"; import showArchivedMemoDialog from "./ArchivedMemoDialog"; import showAboutSiteDialog from "./AboutSiteDialog"; import "../less/user-banner.less"; const UserBanner = () => { const { t } = useTranslation(); const navigate = useNavigate(); const { user, owner } = useAppSelector((state) => state.user); const { memos, tags } = useAppSelector((state) => state.memo); const [username, setUsername] = useState("Memos"); const [createdDays, setCreatedDays] = useState(0); const isVisitorMode = userService.isVisitorMode(); useEffect(() => { if (isVisitorMode) { if (!owner) { return; } setUsername(owner.name); setCreatedDays(Math.ceil((Date.now() - utils.getTimeStampByDate(owner.createdTs)) / 1000 / 3600 / 24)); } else if (user) { setUsername(user.name); setCreatedDays(Math.ceil((Date.now() - utils.getTimeStampByDate(user.createdTs)) / 1000 / 3600 / 24)); } }, [isVisitorMode, user, owner]); const handleUsernameClick = useCallback(() => { locationService.clearQuery(); }, []); const handleResourcesBtnClick = () => { showResourcesDialog(); }; const handleArchivedBtnClick = () => { showArchivedMemoDialog(); }; const handleAboutBtnClick = () => { showAboutSiteDialog(); }; const handleSignOutBtnClick = async () => { userService .doSignOut() .then(() => { navigate("/auth"); }) .catch(() => { // do nth }); }; return ( <>