feat: dayjs i18n
This commit is contained in:
@@ -1,4 +1,3 @@
|
||||
{
|
||||
"intlDateTime": "{{val, datetime}}",
|
||||
"intlRelativeTime": "{{val, relativetime}}"
|
||||
}
|
||||
@@ -5,7 +5,6 @@
|
||||
"Followings": "正在关注",
|
||||
"Follow": "关注",
|
||||
"Unfollow": "取消关注",
|
||||
"intlDateTime": "{{val, datetime}}",
|
||||
"intlRelativeTime": "{{val, relativetime}}",
|
||||
"post": "文章",
|
||||
"posts": "文章",
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
{
|
||||
"Blog Free": "自由创作",
|
||||
"description": "xLog,给所有人的 <1>链上</1> <3>开源</3> 博客平台。",
|
||||
"description": "xLog,给所有人的 <2>链上</2> <6>开源</6> 博客平台。",
|
||||
"Get my xLog in 5 minutes": "5 分钟拥有自己的 xLog",
|
||||
"Features": "特点",
|
||||
"Showcase": "展示",
|
||||
|
||||
@@ -1,11 +1,11 @@
|
||||
import { useEffect, useState } from "react"
|
||||
import { Avatar } from "~/components/ui/Avatar"
|
||||
import dayjs, { formatToISO } from "~/lib/date"
|
||||
import { FollowingButton } from "~/components/common/FollowingButton"
|
||||
import { FollowingCount } from "~/components/common/FollowingCount"
|
||||
import * as siteModel from "~/models/site.model"
|
||||
import type { Profile } from "~/lib/types"
|
||||
import clsx from "clsx"
|
||||
import { useDate } from "~/hooks/useDate"
|
||||
|
||||
export const CharacterCard: React.FC<{
|
||||
siteId?: string
|
||||
@@ -17,6 +17,7 @@ export const CharacterCard: React.FC<{
|
||||
}> = ({ siteId, address, open, setButtonLoading, hideFollowButton, style }) => {
|
||||
const [firstOpen, setFirstOpen] = useState("")
|
||||
const [site, setSite] = useState<Profile>()
|
||||
const date = useDate()
|
||||
|
||||
useEffect(() => {
|
||||
if (open && (firstOpen !== (siteId || address) || !site)) {
|
||||
@@ -72,10 +73,10 @@ export const CharacterCard: React.FC<{
|
||||
{site?.date_created && (
|
||||
<span className="block text-gray-500">
|
||||
Joined{" "}
|
||||
<time dateTime={formatToISO(site.date_created)}>
|
||||
{dayjs
|
||||
<time dateTime={date.formatToISO(site.date_created)}>
|
||||
{date.dayjs
|
||||
.duration(
|
||||
dayjs(site.date_created).diff(dayjs(), "minute"),
|
||||
date.dayjs(site.date_created).diff(date.dayjs(), "minute"),
|
||||
"minute",
|
||||
)
|
||||
.humanize()}{" "}
|
||||
|
||||
@@ -1,5 +1,4 @@
|
||||
import { Avatar } from "~/components/ui/Avatar"
|
||||
import dayjs, { formatToISO } from "~/lib/date"
|
||||
import { UniLink } from "~/components/ui/UniLink"
|
||||
import { BlockchainIcon } from "~/components/icons/BlockchainIcon"
|
||||
import { CSB_SCAN } from "~/lib/env"
|
||||
@@ -12,6 +11,7 @@ import { useState } from "react"
|
||||
import { CommentInput } from "~/components/common/CommentInput"
|
||||
import { CharacterFloatCard } from "~/components/common/CharacterFloatCard"
|
||||
import { useTranslation } from "next-i18next"
|
||||
import { useDate } from "~/hooks/useDate"
|
||||
|
||||
export const CommentItem: React.FC<{
|
||||
comment: NoteEntity & {
|
||||
@@ -22,6 +22,7 @@ export const CommentItem: React.FC<{
|
||||
}> = ({ comment, originalId, depth }) => {
|
||||
const [replyOpen, setReplyOpen] = useState(false)
|
||||
const { t } = useTranslation("common")
|
||||
const date = useDate()
|
||||
|
||||
return (
|
||||
<div
|
||||
@@ -64,10 +65,10 @@ export const CommentItem: React.FC<{
|
||||
{comment?.character?.metadata?.content?.name}
|
||||
</UniLink>{" "}
|
||||
·{" "}
|
||||
<time dateTime={formatToISO(comment?.createdAt)}>
|
||||
{dayjs
|
||||
<time dateTime={date.formatToISO(comment?.createdAt)}>
|
||||
{date.dayjs
|
||||
.duration(
|
||||
dayjs(comment?.createdAt).diff(dayjs(), "minute"),
|
||||
date.dayjs(comment?.createdAt).diff(date.dayjs(), "minute"),
|
||||
"minute",
|
||||
)
|
||||
.humanize()}{" "}
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
import { Fragment, useEffect, useMemo, useState } from "react"
|
||||
import { getPageVisibility } from "~/lib/page-helpers"
|
||||
import { formatDate } from "~/lib/date"
|
||||
import { useDate } from "~/hooks/useDate"
|
||||
import { TabItem, Tabs } from "../ui/Tabs"
|
||||
import { Menu } from "@headlessui/react"
|
||||
import clsx from "clsx"
|
||||
@@ -46,6 +46,7 @@ export const PagesManager: React.FC<{
|
||||
const [convertToastId, setConvertToastId] = useState("")
|
||||
const [deleteToastId, setDeleteToastId] = useState("")
|
||||
const { t } = useTranslation(["site", "common"])
|
||||
const date = useDate()
|
||||
|
||||
useEffect(() => {
|
||||
if (deletePage.isSuccess) {
|
||||
@@ -403,8 +404,8 @@ export const PagesManager: React.FC<{
|
||||
<span className="mx-2">·</span>
|
||||
<span>
|
||||
{getPageVisibility(page) === PageVisibilityEnum.Draft
|
||||
? formatDate(page.date_updated)
|
||||
: formatDate(page.date_published)}
|
||||
? date.formatDate(page.date_updated)
|
||||
: date.formatDate(page.date_published)}
|
||||
</span>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
@@ -1,4 +1,3 @@
|
||||
import { formatToISO } from "~/lib/date"
|
||||
import { BlockchainIcon } from "~/components/icons/BlockchainIcon"
|
||||
import { UniLink } from "~/components/ui/UniLink"
|
||||
import { Note, Profile } from "~/lib/types"
|
||||
@@ -10,6 +9,7 @@ import { Avatar } from "~/components/ui/Avatar"
|
||||
import { EyeIcon, PencilSquareIcon } from "@heroicons/react/24/outline"
|
||||
import { useUserRole } from "~/hooks/useUserRole"
|
||||
import { useTranslation } from "next-i18next"
|
||||
import { useDate } from "~/hooks/useDate"
|
||||
|
||||
export const PostMeta: React.FC<{
|
||||
page: Note
|
||||
@@ -17,6 +17,12 @@ export const PostMeta: React.FC<{
|
||||
author?: Profile | null
|
||||
}> = ({ page, site, author }) => {
|
||||
const { t } = useTranslation("common")
|
||||
const date = useDate()
|
||||
const [isMounted, setIsMounted] = useState(false)
|
||||
|
||||
useEffect(() => {
|
||||
setIsMounted(true)
|
||||
}, [])
|
||||
|
||||
const [showEdit, setShowEdit] = useState(false)
|
||||
const userRole = useUserRole(site?.username)
|
||||
@@ -29,19 +35,14 @@ export const PostMeta: React.FC<{
|
||||
return (
|
||||
<div className="text-zinc-400 mt-4 xlog-post-meta space-x-5 flex items-center">
|
||||
<time
|
||||
dateTime={formatToISO(page.date_published)}
|
||||
dateTime={date.formatToISO(page.date_published)}
|
||||
className="xlog-post-date whitespace-nowrap"
|
||||
>
|
||||
{t("intlDateTime", {
|
||||
val: new Date(page.date_published),
|
||||
formatParams: {
|
||||
val: {
|
||||
year: "numeric",
|
||||
month: "short",
|
||||
day: "numeric",
|
||||
},
|
||||
},
|
||||
})}
|
||||
{date.formatDate(
|
||||
page.date_published,
|
||||
undefined,
|
||||
isMounted ? undefined : "America/Los_Angeles",
|
||||
)}
|
||||
</time>
|
||||
{page.tags?.filter((tag) => tag !== "post" && tag !== "page").length ? (
|
||||
<>
|
||||
|
||||
@@ -1,10 +1,10 @@
|
||||
import Link from "next/link"
|
||||
import { useMemo } from "react"
|
||||
import { formatDate } from "~/lib/date"
|
||||
import { EmptyState } from "../ui/EmptyState"
|
||||
import { Notes, Note } from "~/lib/types"
|
||||
import { UniLink } from "../ui/UniLink"
|
||||
import { Button } from "~/components/ui/Button"
|
||||
import { useDate } from "~/hooks/useDate"
|
||||
|
||||
export const SiteArchives: React.FC<{
|
||||
title?: string
|
||||
@@ -22,6 +22,7 @@ export const SiteArchives: React.FC<{
|
||||
isFetchingNextPage,
|
||||
}) => {
|
||||
let currentLength = 0
|
||||
const date = useDate()
|
||||
|
||||
const groupedByYear = useMemo<Map<string, Note[]>>(() => {
|
||||
const map = new Map()
|
||||
@@ -29,7 +30,7 @@ export const SiteArchives: React.FC<{
|
||||
if (postPages?.length) {
|
||||
for (const posts of postPages) {
|
||||
for (const post of posts.list) {
|
||||
const year = formatDate(post.date_published, "YYYY")
|
||||
const year = date.formatDate(post.date_published, "YYYY")
|
||||
const items = map.get(year) || []
|
||||
items.push(post)
|
||||
map.set(year, items)
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
import Link from "next/link"
|
||||
import { formatToISO } from "~/lib/date"
|
||||
import { useDate } from "~/hooks/useDate"
|
||||
import { Notes } from "~/lib/types"
|
||||
import { EmptyState } from "../ui/EmptyState"
|
||||
import { useRouter } from "next/router"
|
||||
@@ -7,6 +7,7 @@ import { Image } from "~/components/ui/Image"
|
||||
import { Button } from "~/components/ui/Button"
|
||||
import { EyeIcon } from "@heroicons/react/24/outline"
|
||||
import { useTranslation } from "next-i18next"
|
||||
import { useEffect, useState } from "react"
|
||||
|
||||
export const SiteHome: React.FC<{
|
||||
postPages?: Notes[]
|
||||
@@ -17,6 +18,13 @@ export const SiteHome: React.FC<{
|
||||
const router = useRouter()
|
||||
const { t } = useTranslation("common")
|
||||
const { t: siteT } = useTranslation("site")
|
||||
const date = useDate()
|
||||
|
||||
const [isMounted, setIsMounted] = useState(false)
|
||||
|
||||
useEffect(() => {
|
||||
setIsMounted(true)
|
||||
}, [])
|
||||
|
||||
if (!postPages?.length) return null
|
||||
|
||||
@@ -42,19 +50,14 @@ export const SiteHome: React.FC<{
|
||||
</h3>
|
||||
<div className="xlog-post-meta text-sm text-zinc-400 mt-1 space-x-4 flex items-center mr-8">
|
||||
<time
|
||||
dateTime={formatToISO(post.date_published)}
|
||||
dateTime={date.formatToISO(post.date_published)}
|
||||
className="xlog-post-date whitespace-nowrap"
|
||||
>
|
||||
{t("intlDateTime", {
|
||||
val: new Date(post.date_published),
|
||||
formatParams: {
|
||||
val: {
|
||||
year: "numeric",
|
||||
month: "short",
|
||||
day: "numeric",
|
||||
},
|
||||
},
|
||||
})}
|
||||
{date.formatDate(
|
||||
post.date_published,
|
||||
undefined,
|
||||
isMounted ? undefined : "America/Los_Angeles",
|
||||
)}
|
||||
</time>
|
||||
{!!post.tags?.filter(
|
||||
(tag) => tag !== "post" && tag !== "page",
|
||||
|
||||
@@ -0,0 +1,34 @@
|
||||
import dayjs from "dayjs"
|
||||
|
||||
import utc from "dayjs/plugin/utc"
|
||||
import tz from "dayjs/plugin/timezone"
|
||||
import localizedFormat from "dayjs/plugin/localizedFormat"
|
||||
import duration from "dayjs/plugin/duration"
|
||||
import relativeTime from "dayjs/plugin/relativeTime"
|
||||
import { useTranslation } from "next-i18next"
|
||||
import "dayjs/locale/zh"
|
||||
import "dayjs/locale/en"
|
||||
|
||||
dayjs.extend(localizedFormat)
|
||||
dayjs.extend(utc)
|
||||
dayjs.extend(tz)
|
||||
dayjs.extend(duration)
|
||||
dayjs.extend(relativeTime)
|
||||
|
||||
export function useDate() {
|
||||
const { i18n } = useTranslation()
|
||||
dayjs.locale(i18n.resolvedLanguage)
|
||||
|
||||
return {
|
||||
dayjs,
|
||||
formatDate: (date: string | Date, format = "ll", timezone?: string) => {
|
||||
return dayjs(date).tz(timezone).format(format)
|
||||
},
|
||||
formatToISO: (date: string | Date) => {
|
||||
return dayjs(date).toISOString()
|
||||
},
|
||||
inLocalTimezone: (date: string | Date) => {
|
||||
return dayjs(date).tz().toDate()
|
||||
},
|
||||
}
|
||||
}
|
||||
@@ -1,31 +0,0 @@
|
||||
import dayjs from "dayjs"
|
||||
|
||||
import utc from "dayjs/plugin/utc"
|
||||
import tz from "dayjs/plugin/timezone"
|
||||
import localizedFormat from "dayjs/plugin/localizedFormat"
|
||||
import duration from "dayjs/plugin/duration"
|
||||
import relativeTime from "dayjs/plugin/relativeTime"
|
||||
|
||||
dayjs.extend(localizedFormat)
|
||||
dayjs.extend(utc)
|
||||
dayjs.extend(tz)
|
||||
dayjs.extend(duration)
|
||||
dayjs.extend(relativeTime)
|
||||
|
||||
export const formatDate = (
|
||||
date: string | Date,
|
||||
format: "MMM D" | "MMM D, YYYY" | "YYYY" = "MMM D, YYYY",
|
||||
timezone?: string,
|
||||
) => {
|
||||
return dayjs(date).tz(timezone).format(format)
|
||||
}
|
||||
|
||||
export const formatToISO = (date: string | Date) => {
|
||||
return dayjs(date).toISOString()
|
||||
}
|
||||
|
||||
export const inLocalTimezone = (date: string | Date) => {
|
||||
return dayjs(date).tz().toDate()
|
||||
}
|
||||
|
||||
export default dayjs
|
||||
@@ -1,4 +1,4 @@
|
||||
import dayjs from "~/lib/date"
|
||||
import dayjs from "dayjs"
|
||||
import { PageVisibilityEnum } from "./types"
|
||||
|
||||
export const getPageVisibility = ({
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
import clsx from "clsx"
|
||||
import dayjs from "~/lib/date"
|
||||
import { useDate } from "~/hooks/useDate"
|
||||
import { useRouter } from "next/router"
|
||||
import {
|
||||
ChangeEvent,
|
||||
@@ -18,7 +18,6 @@ import { EditorToolbar } from "~/components/ui/EditorToolbar"
|
||||
import { Input } from "~/components/ui/Input"
|
||||
import { UniLink } from "~/components/ui/UniLink"
|
||||
import { useUploadFile } from "~/hooks/useUploadFile"
|
||||
import { inLocalTimezone } from "~/lib/date"
|
||||
import { getSiteLink } from "~/lib/helpers"
|
||||
import { getPageVisibility } from "~/lib/page-helpers"
|
||||
import { PageVisibilityEnum } from "~/lib/types"
|
||||
@@ -40,7 +39,7 @@ import { CSB_SCAN } from "~/lib/env"
|
||||
import { showConfetti } from "~/lib/confetti"
|
||||
import type { ReactElement } from "react"
|
||||
|
||||
const getInputDatetimeValue = (date: Date | string) => {
|
||||
const getInputDatetimeValue = (date: Date | string, dayjs: any) => {
|
||||
const str = dayjs(date).format()
|
||||
return str.substring(0, ((str.indexOf("T") | 0) + 6) | 0)
|
||||
}
|
||||
@@ -48,6 +47,7 @@ const getInputDatetimeValue = (date: Date | string) => {
|
||||
export default function SubdomainEditor() {
|
||||
const router = useRouter()
|
||||
const queryClient = useQueryClient()
|
||||
const date = useDate()
|
||||
|
||||
let pageId = router.query.id as string | undefined
|
||||
const subdomain = router.query.subdomain as string
|
||||
@@ -481,11 +481,14 @@ export default function SubdomainEditor() {
|
||||
isBlock
|
||||
name="publishAt"
|
||||
id="publishAt"
|
||||
value={getInputDatetimeValue(values.publishedAt)}
|
||||
value={getInputDatetimeValue(
|
||||
values.publishedAt,
|
||||
date.dayjs,
|
||||
)}
|
||||
onChange={(e: ChangeEvent<HTMLInputElement>) => {
|
||||
const value = inLocalTimezone(
|
||||
e.target.value,
|
||||
).toISOString()
|
||||
const value = date
|
||||
.inLocalTimezone(e.target.value)
|
||||
.toISOString()
|
||||
updateValue("publishedAt", value)
|
||||
}}
|
||||
help={`This ${
|
||||
|
||||
@@ -7,7 +7,7 @@ import { DISCORD_LINK, TWITTER_LINK, GITHUB_LINK, APP_NAME } from "~/lib/env"
|
||||
import { getSiteLink } from "~/lib/helpers"
|
||||
import type { ReactElement } from "react"
|
||||
import { useGetSite, useGetStat } from "~/queries/site"
|
||||
import dayjs from "~/lib/date"
|
||||
import { useDate } from "~/hooks/useDate"
|
||||
|
||||
export default function SubdomainIndex() {
|
||||
const router = useRouter()
|
||||
@@ -17,6 +17,7 @@ export default function SubdomainIndex() {
|
||||
const stat = useGetStat({
|
||||
characterId,
|
||||
})
|
||||
const date = useDate()
|
||||
const statMap = [
|
||||
{
|
||||
name: "Total Posts",
|
||||
@@ -36,7 +37,8 @@ export default function SubdomainIndex() {
|
||||
},
|
||||
{
|
||||
name: "Site Duration",
|
||||
value: dayjs().diff(dayjs(stat.data?.createdAt), "day") + " days",
|
||||
value:
|
||||
date.dayjs().diff(date.dayjs(stat.data?.createdAt), "day") + " days",
|
||||
},
|
||||
]
|
||||
|
||||
|
||||
Reference in New Issue
Block a user