diff --git a/web/src/components/kit/DatePicker.tsx b/web/src/components/kit/DatePicker.tsx index fa6a6063..eea14d0a 100644 --- a/web/src/components/kit/DatePicker.tsx +++ b/web/src/components/kit/DatePicker.tsx @@ -3,20 +3,22 @@ import { useEffect, useState } from "react"; import { useTranslate } from "@/utils/i18n"; import { DAILY_TIMESTAMP } from "@/helpers/consts"; import { getMemoStats } from "@/helpers/api"; -import { getDateStampByDate } from "@/helpers/datetime"; +import { getDateStampByDate, isFutureDate } from "@/helpers/datetime"; import { useUserStore } from "@/store/module"; +import classNames from "classnames"; import Icon from "../Icon"; import "@/less/common/date-picker.less"; interface DatePickerProps { className?: string; + isFutureDateDisabled?: boolean; datestamp: DateStamp; handleDateStampChange: (datestamp: DateStamp) => void; } const DatePicker: React.FC = (props: DatePickerProps) => { const t = useTranslate(); - const { className, datestamp, handleDateStampChange } = props; + const { className, isFutureDateDisabled, datestamp, handleDateStampChange } = props; const [currentDateStamp, setCurrentDateStamp] = useState(getMonthFirstDayDateStamp(datestamp)); const [countByDate, setCountByDate] = useState(new Map()); const currentUserId = useUserStore().getCurrentUserId(); @@ -96,6 +98,7 @@ const DatePicker: React.FC = (props: DatePickerProps) => { {dayList.map((d) => { + const isDisabled = isFutureDateDisabled && isFutureDate(d.datestamp); if (d.date === 0) { return ( @@ -106,8 +109,8 @@ const DatePicker: React.FC = (props: DatePickerProps) => { return ( handleDateItemClick(d.datestamp)} + className={classNames(`day-item relative ${d.datestamp === datestamp ? "current" : ""}`, isDisabled && "disabled")} + onClick={() => (isDisabled ? null : handleDateItemClick(d.datestamp))} > {countByDate.has(d.datestamp) ? {d.date} : d.date} diff --git a/web/src/helpers/datetime.ts b/web/src/helpers/datetime.ts index 53f388a0..99d90658 100644 --- a/web/src/helpers/datetime.ts +++ b/web/src/helpers/datetime.ts @@ -216,3 +216,16 @@ export function getUnixTime(t?: Date | number | string): number { const date = new Date(t ? t : Date.now()); return Math.floor(date.getTime() / 1000); } + +/** + * Checks if the provided date or timestamp is in the future. + * + * If no date is provided, the current date is used. + * + * @param t - Date or timestamp to check. + * @returns `true` if the date is in the future, `false` otherwise. + */ +export function isFutureDate(t?: Date | number | string): boolean { + const timestamp = getTimeStampByDate(t ? t : Date.now()); + return timestamp > Date.now(); +} diff --git a/web/src/less/common/date-picker.less b/web/src/less/common/date-picker.less index 91399a2a..f8bf5861 100644 --- a/web/src/less/common/date-picker.less +++ b/web/src/less/common/date-picker.less @@ -37,6 +37,10 @@ @apply text-blue-600 !bg-blue-100 text-base font-medium; } + &.disabled { + @apply cursor-not-allowed; + } + &.null { background-color: unset; cursor: unset; diff --git a/web/src/pages/DailyReview.tsx b/web/src/pages/DailyReview.tsx index 99900aa1..af5b329f 100644 --- a/web/src/pages/DailyReview.tsx +++ b/web/src/pages/DailyReview.tsx @@ -13,8 +13,9 @@ import DatePicker from "@/components/kit/DatePicker"; import DailyMemo from "@/components/DailyMemo"; import i18n from "@/i18n"; import { findNearestLanguageMatch } from "@/utils/i18n"; -import { convertToMillis, getDateStampByDate, getNormalizedDateString, getTimeStampByDate } from "@/helpers/datetime"; +import { convertToMillis, getDateStampByDate, getNormalizedDateString, getTimeStampByDate, isFutureDate } from "@/helpers/datetime"; import Empty from "@/components/Empty"; +import classNames from "classnames"; const DailyReview = () => { const t = useTranslate(); @@ -85,6 +86,7 @@ const DailyReview = () => { 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 (
@@ -105,8 +107,12 @@ const DailyReview = () => { @@ -123,6 +129,7 @@ const DailyReview = () => { }`} datestamp={currentDateStamp} handleDateStampChange={handleDataPickerChange} + isFutureDateDisabled />