import classNames from "classnames"; import { last } from "lodash-es"; import { useEffect, useRef } from "react"; import toast from "react-hot-toast"; import useSessionStorage from "react-use/lib/useSessionStorage"; import useToggle from "react-use/lib/useToggle"; import DailyMemo from "@/components/DailyMemo"; import Empty from "@/components/Empty"; import Icon from "@/components/Icon"; import MobileHeader from "@/components/MobileHeader"; import showPreviewImageDialog from "@/components/PreviewImageDialog"; import DatePicker from "@/components/kit/DatePicker"; import { DAILY_TIMESTAMP, DEFAULT_MEMO_LIMIT } from "@/helpers/consts"; import { convertToMillis, getDateStampByDate, getNormalizedDateString, getTimeStampByDate, isFutureDate } from "@/helpers/datetime"; import useCurrentUser from "@/hooks/useCurrentUser"; import i18n from "@/i18n"; import toImage from "@/labs/html2image"; import { useMemoStore, useUserStore } from "@/store/module"; import { findNearestLanguageMatch, useTranslate } from "@/utils/i18n"; const DailyReview = () => { const t = useTranslate(); const memoStore = useMemoStore(); const userStore = useUserStore(); const user = useCurrentUser(); const { localSetting } = userStore.state.user as User; const [currentDateStampRaw, setCurrentDateStamp] = useSessionStorage( "daily-review-datestamp", getDateStampByDate(getNormalizedDateString()) ); const currentDateStamp = currentDateStampRaw as number; const [showDatePicker, toggleShowDatePicker] = useToggle(false); const memosElRef = useRef(null); const currentDate = new Date(currentDateStamp); const dailyMemos = memoStore.state.memos .filter((m) => { const displayTimestamp = getTimeStampByDate(m.displayTs); const currentDateStampWithOffset = currentDateStamp + convertToMillis(localSetting); return ( m.rowStatus === "NORMAL" && m.creatorUsername === user.username && displayTimestamp >= currentDateStampWithOffset && displayTimestamp < currentDateStampWithOffset + DAILY_TIMESTAMP ); }) .sort((a, b) => getTimeStampByDate(a.displayTs) - getTimeStampByDate(b.displayTs)); useEffect(() => { let offset = 0; const fetchMoreMemos = async () => { try { const fetchedMemos = await memoStore.fetchMemos("", DEFAULT_MEMO_LIMIT, offset); offset += fetchedMemos.length; if (fetchedMemos.length === DEFAULT_MEMO_LIMIT) { const lastMemo = last(fetchedMemos); if (lastMemo && lastMemo.displayTs > currentDateStamp) { await fetchMoreMemos(); } } } catch (error: any) { console.error(error); toast.error(error.response.data.message); } }; fetchMoreMemos(); }, [currentDateStamp]); const handleShareBtnClick = () => { if (!memosElRef.current) { return; } toggleShowDatePicker(false); toImage(memosElRef.current, { pixelRatio: window.devicePixelRatio * 2, }) .then((url) => { showPreviewImageDialog(url); }) .catch(() => { // do nth }); }; const handleDataPickerChange = (datestamp: number): void => { setCurrentDateStamp(datestamp); toggleShowDatePicker(false); }; const locale = findNearestLanguageMatch(i18n.language); const currentMonth = currentDate.toLocaleDateString(locale, { month: "short" }); const currentDayOfWeek = currentDate.toLocaleDateString(locale, { weekday: "short" }); const isFutureDateDisabled = isFutureDate(currentDateStamp + DAILY_TIMESTAMP); return (

toggleShowDatePicker()} > {t("daily-review.title")}

{currentDate.getFullYear()}
{currentMonth[0].toUpperCase() + currentMonth.substring(1)}
{currentDate.getDate()}
{currentDayOfWeek[0].toUpperCase() + currentDayOfWeek.substring(1)}
{dailyMemos.length === 0 ? (

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

) : (
{dailyMemos.map((memo) => ( ))}
)}
); }; export default DailyReview;