import { useEffect, useRef, useState } from "react"; import { memoService } from "../services"; import toImage from "../labs/html2image"; import useToggle from "../hooks/useToggle"; import useLoading from "../hooks/useLoading"; import { DAILY_TIMESTAMP } from "../helpers/consts"; import utils from "../helpers/utils"; import { showDialog } from "./Dialog"; import showPreviewImageDialog from "./PreviewImageDialog"; import DailyMemo from "./DailyMemo"; import DatePicker from "./common/DatePicker"; import "../less/daily-memo-diary-dialog.less"; interface Props extends DialogProps { currentDateStamp: DateStamp; } const monthChineseStrArray = ["一月", "二月", "三月", "四月", "五月", "六月", "七月", "八月", "九月", "十月", "十一月", "十二月"]; const weekdayChineseStrArray = ["周日", "周一", "周二", "周三", "周四", "周五", "周六"]; const DailyMemoDiaryDialog: React.FC = (props: Props) => { const loadingState = useLoading(); const [memos, setMemos] = useState([]); const [currentDateStamp, setCurrentDateStamp] = useState(utils.getDateStampByDate(utils.getDateString(props.currentDateStamp))); const [showDatePicker, toggleShowDatePicker] = useToggle(false); const memosElRef = useRef(null); const currentDate = new Date(currentDateStamp); useEffect(() => { const setDailyMemos = () => { const dailyMemos = memoService .getState() .memos.filter( (a) => utils.getTimeStampByDate(a.createdTs) >= currentDateStamp && utils.getTimeStampByDate(a.createdTs) < currentDateStamp + DAILY_TIMESTAMP ) .sort((a, b) => utils.getTimeStampByDate(a.createdTs) - utils.getTimeStampByDate(b.createdTs)); setMemos(dailyMemos); loadingState.setFinish(); }; setDailyMemos(); }, [currentDateStamp]); const handleShareBtnClick = () => { toggleShowDatePicker(false); setTimeout(() => { if (!memosElRef.current) { return; } toImage(memosElRef.current, { backgroundColor: "#ffffff", pixelRatio: window.devicePixelRatio * 2, }) .then((url) => { showPreviewImageDialog(url); }) .catch(() => { // do nth }); }, 0); }; const handleDataPickerChange = (datestamp: DateStamp): void => { setCurrentDateStamp(datestamp); toggleShowDatePicker(false); }; return ( <>

Daily Memos

setCurrentDateStamp(currentDateStamp - DAILY_TIMESTAMP)}> setCurrentDateStamp(currentDateStamp + DAILY_TIMESTAMP)}> props.destroy()}>
toggleShowDatePicker()}>
{currentDate.getFullYear()}
{monthChineseStrArray[currentDate.getMonth()]}
{currentDate.getDate()}
{weekdayChineseStrArray[currentDate.getDay()]}
{loadingState.isLoading ? (

Loading...

) : memos.length === 0 ? (

Oops, there is nothing.

) : (
{memos.map((memo) => ( ))}
)}
); }; export default function showDailyMemoDiaryDialog(datestamp: DateStamp = Date.now()): void { showDialog( { className: "daily-memo-diary-dialog", }, DailyMemoDiaryDialog, { currentDateStamp: datestamp } ); }