import dayjs from "dayjs"; import { useEffect, useState } from "react"; import { useTranslation } from "react-i18next"; import { Link } from "react-router-dom"; import { useGlobalStore, useLocationStore, useMemoStore, useUserStore } from "../store/module"; import { DEFAULT_MEMO_LIMIT } from "../helpers/consts"; import useLoading from "../hooks/useLoading"; import toastHelper from "../components/Toast"; import MemoContent from "../components/MemoContent"; import MemoResources from "../components/MemoResources"; import "../less/explore.less"; interface State { memos: Memo[]; } const Explore = () => { const { t, i18n } = useTranslation(); const globalStore = useGlobalStore(); const locationStore = useLocationStore(); const userStore = useUserStore(); const memoStore = useMemoStore(); const [state, setState] = useState({ memos: [], }); const [isComplete, setIsComplete] = useState(false); const loadingState = useLoading(); const customizedProfile = globalStore.state.systemStatus.customizedProfile; const user = userStore.state.user; const location = locationStore.state; useEffect(() => { memoStore.fetchAllMemos(DEFAULT_MEMO_LIMIT, state.memos.length).then((memos) => { if (memos.length < DEFAULT_MEMO_LIMIT) { setIsComplete(true); } setState({ memos, }); loadingState.setFinish(); }); }, [location]); const handleFetchMoreClick = async () => { try { const fetchedMemos = await memoStore.fetchAllMemos(DEFAULT_MEMO_LIMIT, state.memos.length); if (fetchedMemos.length < DEFAULT_MEMO_LIMIT) { setIsComplete(true); } else { setIsComplete(false); } setState({ memos: state.memos.concat(fetchedMemos), }); } catch (error: any) { console.error(error); toastHelper.error(error.response.data.message); } }; return (
{customizedProfile.name}
{!loadingState.isLoading && user ? ( 🏠 {t("common.back-to-home")} ) : ( 👉 {t("common.sign-in")} )}
{!loadingState.isLoading && (
{state.memos.map((memo) => { const createdAtStr = dayjs(memo.displayTs).locale(i18n.language).format("YYYY/MM/DD HH:mm:ss"); return ( ); })} {isComplete ? ( state.memos.length === 0 ? (

{t("message.no-memos")}

) : null ) : (

{t("memo-list.fetch-more")}

)}
)}
); }; export default Explore;