From fcae0138b74d4d3e92e06c02c5666739271073b6 Mon Sep 17 00:00:00 2001 From: Lingfei Song <82314+madcat@users.noreply.github.com> Date: Fri, 12 Apr 2024 11:04:07 +0700 Subject: [PATCH] page data in utc yyyy-mm-dd, not local date --- client/Dockerfile.web | 4 +++- client/pb/pb_hooks/main.pb.js | 2 +- client/web/src/components/screen/insights.jsx | 6 +++--- client/web/src/lib/utils.js | 5 ++++- client/web/src/store.js | 15 +++++++++------ 5 files changed, 20 insertions(+), 12 deletions(-) diff --git a/client/Dockerfile.web b/client/Dockerfile.web index d4ef67d..df59a6b 100644 --- a/client/Dockerfile.web +++ b/client/Dockerfile.web @@ -12,7 +12,9 @@ FROM alpine:latest ARG PB_VERSION=0.21.1 -RUN apk add --no-cache unzip ca-certificates tzdata +RUN apk add --no-cache unzip ca-certificates tzdata && \ + ln -fs /usr/share/zoneinfo/Asia/Shanghai /etc/localtime + # download and unzip PocketBase ADD https://github.com/pocketbase/pocketbase/releases/download/v${PB_VERSION}/pocketbase_${PB_VERSION}_linux_amd64.zip /tmp/pb.zip diff --git a/client/pb/pb_hooks/main.pb.js b/client/pb/pb_hooks/main.pb.js index 44bf76a..7f585e8 100644 --- a/client/pb/pb_hooks/main.pb.js +++ b/client/pb/pb_hooks/main.pb.js @@ -43,7 +43,7 @@ routerAdd( }) ) - $app.dao().db().newQuery("SELECT DISTINCT DATE(created, 'localtime') as created FROM insights").all(result) + $app.dao().db().newQuery("SELECT DISTINCT DATE(created) as created FROM insights").all(result) return c.json( 200, diff --git a/client/web/src/components/screen/insights.jsx b/client/web/src/components/screen/insights.jsx index 90da40f..2cbbd8c 100644 --- a/client/web/src/components/screen/insights.jsx +++ b/client/web/src/components/screen/insights.jsx @@ -66,11 +66,11 @@ function InsightsScreen({}) { const [, navigate] = useLocation() const queryClient = useQueryClient() const mut = useMutation({ - mutationFn: (data) => { - if (data && selectInsight && data.find((insight) => insight.id == selectedInsight).expand.articles.length == 1) { + mutationFn: (params) => { + if (params && selectedInsight && data.find((insight) => insight.id == selectedInsight).expand.articles.length == 1) { throw new Error("不能删除最后一篇文章") } - return unlinkArticle(data) + return unlinkArticle(params) }, onSuccess: () => { queryClient.invalidateQueries({ queryKey: ["insights", currentDate] }) diff --git a/client/web/src/lib/utils.js b/client/web/src/lib/utils.js index d314d8c..eae5d61 100644 --- a/client/web/src/lib/utils.js +++ b/client/web/src/lib/utils.js @@ -1,12 +1,15 @@ import { clsx } from "clsx" import { twMerge } from "tailwind-merge" +export const LOCAL_TIME_OFFSITE = "+08:00" + export function cn(...inputs) { return twMerge(clsx(inputs)) } export function formatDate(date) { - var d = new Date(isNaN(date) ? date + "T00:00:00" : date) + // var d = new Date(isNaN(date) ? date + "T00:00:00" + LOCAL_TIME_OFFSITE : date) // local + var d = new Date(isNaN(date) ? date + "T00:00:00" : date) // utc var iso = d.toISOString() return iso.slice(0, 10) + " " + iso.slice(11, 23) + "Z" // return [d.getFullYear(), (d.getMonth() + 1).padLeft(), d.getDate().padLeft()].join("-") + " " + [d.getHours().padLeft(), d.getMinutes().padLeft(), d.getSeconds().padLeft()].join(":") + ".000Z" diff --git a/client/web/src/store.js b/client/web/src/store.js index be5fb65..e2c08cf 100644 --- a/client/web/src/store.js +++ b/client/web/src/store.js @@ -9,7 +9,7 @@ import { persist } from "zustand/middleware" import axios from "axios" import { nanoid } from "nanoid" -import { formatDate } from "./lib/utils" +import { formatDate, LOCAL_TIME_OFFSITE } from "./lib/utils" const DAYS_RANGE = [1, 14] @@ -211,7 +211,9 @@ export function getArticles(date) { if (!date) return [] const from = formatDate(date) - const to = formatDate(new Date(new Date(date).getTime() + 60 * 60 * 24 * 1000)) + //const to = formatDate(new Date(new Date(date + "T00:00:00" + LOCAL_TIME_OFFSITE).getTime() + 60 * 60 * 24 * 1000)) + const to = formatDate(new Date(new Date(date + "T00:00:00").getTime() + 60 * 60 * 24 * 1000)) + console.log("from/to", from, to) return pb.collection("articles").getFullList({ sort: "-created", expand: "translation_result", @@ -224,15 +226,15 @@ export function getInsight(id) { } export function getInsights(date) { - if (!date) return null + if (!date) return [] const from = formatDate(date) + //const to = formatDate(new Date(new Date(date + "T00:00:00" + LOCAL_TIME_OFFSITE).getTime() + 60 * 60 * 24 * 1000)) const to = formatDate(new Date(new Date(date + "T00:00:00").getTime() + 60 * 60 * 24 * 1000)) // console.log("from/to", from, to) const f = 'created >= "' + from + '" && created < "' + to + '"' - console.log(f) - + // console.log(f) return pb.collection("insights").getFullList({ sort: "-created", expand: "articles, articles.translation_result", @@ -250,7 +252,7 @@ export async function getInsightDates() { Authorization: "Bearer " + pb.authStore?.token, }, }) - // return data.map((d) => new Date(d + "T00:00:00Z").toLocaleDateString().split("/").join("-")) + //return data.map((d) => new Date(d + "T00:00:00" + LOCAL_TIME_OFFSITE).toISOString().slice(0, 10)) return data } @@ -263,6 +265,7 @@ export async function getArticleDates() { Authorization: "Bearer " + pb.authStore?.token, }, }) + //return data.map((d) => new Date(d + "T00:00:00" + LOCAL_TIME_OFFSITE).toISOString().slice(0, 10)) return data }