diff --git a/web/src/components/MemoEditor/MemoEditorDialog.tsx b/web/src/components/MemoEditor/MemoEditorDialog.tsx index dc26fb0d..3adb7fca 100644 --- a/web/src/components/MemoEditor/MemoEditorDialog.tsx +++ b/web/src/components/MemoEditor/MemoEditorDialog.tsx @@ -1,7 +1,8 @@ import { IconButton } from "@mui/joy"; import clsx from "clsx"; -import { useEffect, useRef, useState } from "react"; +import { useEffect, useRef } from "react"; import useCurrentUser from "@/hooks/useCurrentUser"; +import useDateTime from "@/hooks/useDateTime"; import { useMemoStore, useTagStore } from "@/store/v1"; import { Memo } from "@/types/proto/api/v1/memo_service"; import MemoEditor, { Props as MemoEditorProps } from "."; @@ -21,7 +22,7 @@ const MemoEditorDialog: React.FC = ({ }: Props) => { const tagStore = useTagStore(); const memoStore = useMemoStore(); - const [displayTime, setDisplayTime] = useState(memoStore.getMemoByName(memoName || "")?.displayTime?.toISOString()); + const { setDateTime, displayDateTime, datePickerDateTime } = useDateTime(memoStore.getMemoByName(memoName || "")?.displayTime); const memoPatchRef = useRef>({ displayTime: memoStore.getMemoByName(memoName || "")?.displayTime, }); @@ -32,7 +33,7 @@ const MemoEditorDialog: React.FC = ({ }, []); const updateDisplayTime = (displayTime: string) => { - setDisplayTime(displayTime); + setDateTime(displayTime); memoPatchRef.current.displayTime = new Date(displayTime); }; @@ -50,14 +51,14 @@ const MemoEditorDialog: React.FC = ({ return ( <>
-
- {displayTime ? ( +
+ {displayDateTime ? (
- {new Date(displayTime).toLocaleString()} + {displayDateTime} e.target.showPicker()} onChange={(e) => updateDisplayTime(e.target.value)} /> diff --git a/web/src/hooks/index.ts b/web/src/hooks/index.ts index 881f9458..b8af6852 100644 --- a/web/src/hooks/index.ts +++ b/web/src/hooks/index.ts @@ -4,3 +4,4 @@ export * from "./useNavigateTo"; export * from "./useAsyncEffect"; export * from "./useFilterWithUrlParams"; export * from "./useResponsiveWidth"; +export * from "./useDateTime"; diff --git a/web/src/hooks/useDateTime.ts b/web/src/hooks/useDateTime.ts new file mode 100644 index 00000000..0c5aaec9 --- /dev/null +++ b/web/src/hooks/useDateTime.ts @@ -0,0 +1,13 @@ +import { useState } from "react"; + +const useDateTime = (initalState?: Date) => { + const [dateTime, setDateTimeInternal] = useState(initalState && new Date(initalState)); + + return { + setDateTime: (dateTimeString: string) => setDateTimeInternal(new Date(dateTimeString)), + displayDateTime: dateTime && dateTime.toLocaleString(), + datePickerDateTime: dateTime && new Date(dateTime.getTime() - dateTime.getTimezoneOffset() * 60000).toISOString().split(".")[0], + }; +}; + +export default useDateTime;