From 486f6451fefaf1efcd576534b19f95ae8179395e Mon Sep 17 00:00:00 2001 From: EGOIST Date: Mon, 23 May 2022 17:34:44 +0800 Subject: [PATCH] fix: saving time with timezone, closes #81 --- src/lib/date.ts | 12 +++++++++++- src/pages/dashboard/[subdomain]/editor.tsx | 18 ++++++++++-------- 2 files changed, 21 insertions(+), 9 deletions(-) diff --git a/src/lib/date.ts b/src/lib/date.ts index b5f59f57..33816361 100644 --- a/src/lib/date.ts +++ b/src/lib/date.ts @@ -1,8 +1,18 @@ import dayjs from "dayjs" +import utc from "dayjs/plugin/utc" +import tz from "dayjs/plugin/timezone" + +dayjs.extend(utc) +dayjs.extend(tz) + export const formatDate = ( date: string | Date, - format: "MMM D" | "MMM D, YYYY" | "YYYY" = "MMM D, YYYY" + format: "MMM D" | "MMM D, YYYY" | "YYYY" = "MMM D, YYYY", ) => { return dayjs(date).format(format) } + +export const inLocalTimezone = (date: string | Date) => { + return dayjs(date).tz().toDate() +} diff --git a/src/pages/dashboard/[subdomain]/editor.tsx b/src/pages/dashboard/[subdomain]/editor.tsx index 93c8b567..d4148dbc 100644 --- a/src/pages/dashboard/[subdomain]/editor.tsx +++ b/src/pages/dashboard/[subdomain]/editor.tsx @@ -15,6 +15,7 @@ import { DashboardLayout } from "~/components/dashboard/DashboardLayout" import { UniLink } from "~/components/ui/UniLink" import { useUploadFile } from "~/hooks/useUploadFile" import { PublishButton } from "~/components/dashboard/PublishButton" +import { inLocalTimezone } from "~/lib/date" const getInputDatetimeValue = (date: Date | string) => { const str = dayjs(date).format() @@ -36,7 +37,7 @@ export default function SubdomainEditor() { { enabled: !!pageId, refetchOnWindowFocus: false, - } + }, ) const published = page?.published ?? false const visibility = getPageVisibility({ @@ -70,7 +71,7 @@ export default function SubdomainEditor() { [key]: value, }) }, - [setValues, values] + [setValues, values], ) const savePage = (published: boolean) => { @@ -98,8 +99,8 @@ export default function SubdomainEditor() { }) view.dispatch( view.state.replaceSelection( - `\n\n![${file.name.replace(/\.\w+$/, "")}](${key})\n\n` - ) + `\n\n![${file.name.replace(/\.\w+$/, "")}](${key})\n\n`, + ), ) } catch (error) { if (error instanceof Error) { @@ -107,7 +108,7 @@ export default function SubdomainEditor() { } } }, - [uploadFile] + [uploadFile], ) const handleEditorChange = (newValue: string) => { @@ -128,7 +129,7 @@ export default function SubdomainEditor() { router.replace( `/dashboard/${subdomain}/editor?id=${ createOrUpdatePageResult.id - }&type=${isPost ? "post" : "page"}` + }&type=${isPost ? "post" : "page"}`, ) } }, [ @@ -175,7 +176,7 @@ export default function SubdomainEditor() { {visibility === PageVisibilityEnum.Published @@ -226,7 +227,8 @@ export default function SubdomainEditor() { id="publishAt" value={getInputDatetimeValue(values.publishedAt)} onChange={(e: ChangeEvent) => { - updateValue("publishedAt", e.target.value) + const value = inLocalTimezone(e.target.value).toISOString() + updateValue("publishedAt", value) }} help={`This ${ isPost ? "post" : "page"