timetable-app

Unnamed repository; edit this file 'description' to name the repository.
Log | Files | Refs | README | LICENSE

commit 63e3cf85bbc4ba16afb2da9ac807ada0b18eb8c2
parent dc51623816bff8375c204a58f0a62f6194d01c87
Author: Yuukin256 <Yuukin256@gmail.com>
Date:   Sun, 21 Aug 2022 20:38:33 +0900

デフォルトで表示する日付を仕様変更

Diffstat:
Msrc/hooks/useDailyTimetable.ts | 34+++++++++++++++++++---------------
Msrc/pages/[class].tsx | 6+++---
2 files changed, 22 insertions(+), 18 deletions(-)

diff --git a/src/hooks/useDailyTimetable.ts b/src/hooks/useDailyTimetable.ts @@ -1,5 +1,5 @@ import { Temporal } from '@js-temporal/polyfill'; -import closestIndexTo from 'date-fns/closestIndexTo'; +import { isFuture } from 'date-fns'; import { useMemo, useState } from 'react'; import type { Dispatch, SetStateAction } from 'react'; @@ -24,23 +24,27 @@ const useDailyTimetable = (calendar: Calendar): Return => { const calendarDateSeq = useMemo(() => calendar.keySeq(), [calendar]); const dates = useMemo(() => calendarDateSeq.toArray().map((v) => Temporal.PlainDate.from(v)), [calendarDateSeq]); - // 初期設定の日付: 16時以前なら当日; 16時以降なら翌日 - const now = Temporal.Now.zonedDateTimeISO('Asia/Tokyo'); - const duration = now.since(now.with({ hour: 16, minute: 0, second: 0 })); - const initialDate = duration.sign === -1 ? Temporal.Now.plainDateISO() : Temporal.Now.plainDateISO().add({ days: 1 }); - + // 初期設定の日付: 16時以前なら当日; 16時以降なら翌日; 無ければ一番最初 const [index, setIndex] = useState<number>(() => { - const i = calendarDateSeq.indexOf(initialDate.toJSON()); - if (i !== -1) { - return i; + { + const now = Temporal.Now.zonedDateTimeISO('Asia/Tokyo'); + const duration = now.since(now.with({ hour: 16, minute: 0, second: 0 })); + const initialDate = + duration.sign === -1 ? Temporal.Now.plainDateISO() : Temporal.Now.plainDateISO().add({ days: 1 }); + const i = calendarDateSeq.indexOf(initialDate.toJSON()); + if (i !== -1) { + return i; + } + } + + { + const i = calendarDateSeq.findIndex((v) => isFuture(new Date(v))); + if (i !== -1) { + return i; + } } - return ( - closestIndexTo( - new Date(initialDate.toJSON()), - calendarDateSeq.toArray().map((v) => new Date(v)) - ) ?? calendar.size - 1 - ); + return calendar.size - 1; }); const dateString = calendarDateSeq.get(index); diff --git a/src/pages/[class].tsx b/src/pages/[class].tsx @@ -53,7 +53,7 @@ const Page: NextPage<PageProps> = (props) => { 6: '体育', 7: '家庭科', }) - .set(new Temporal.PlainDate(2022, 8, 10).toJSON(), { + .set(new Temporal.PlainDate(2022, 8, 23).toJSON(), { 1: '数学II', 2: '英語構文', 3: '倫理政経', @@ -62,7 +62,7 @@ const Page: NextPage<PageProps> = (props) => { 6: '体育', 7: '家庭科', }) - .set(new Temporal.PlainDate(2022, 8, 9).toJSON(), { + .set(new Temporal.PlainDate(2022, 8, 24).toJSON(), { 1: '数学II', 2: '英語構文', 3: '倫理政経', @@ -71,7 +71,7 @@ const Page: NextPage<PageProps> = (props) => { 6: '体育', 7: '家庭科', }) - .set(new Temporal.PlainDate(2022, 8, 14).toJSON(), { + .set(new Temporal.PlainDate(2022, 8, 21).toJSON(), { 1: null, 2: null, 3: null,