From 59290dafc2ff8408222a1d0eb8e3dbf9f220f7ac Mon Sep 17 00:00:00 2001 From: DIYgod Date: Wed, 24 Jan 2024 16:42:35 +0800 Subject: [PATCH] feat: dayjs lib --- .../[locale]/site/[site]/sitemap.xml/route.ts | 2 +- src/components/common/Heatmap.tsx | 5 +-- src/hooks/useDate.ts | 18 +---------- src/lib/dayjs.ts | 20 ++++++++++++ src/lib/page-helpers.ts | 2 +- src/models/home.model.ts | 32 +++++++++++-------- src/models/page.model.ts | 7 +--- 7 files changed, 44 insertions(+), 42 deletions(-) create mode 100644 src/lib/dayjs.ts diff --git a/src/app/[locale]/site/[site]/sitemap.xml/route.ts b/src/app/[locale]/site/[site]/sitemap.xml/route.ts index 8351cc2e..c6427b14 100644 --- a/src/app/[locale]/site/[site]/sitemap.xml/route.ts +++ b/src/app/[locale]/site/[site]/sitemap.xml/route.ts @@ -1,9 +1,9 @@ -import dayjs from "dayjs" import { NextRequest } from "next/server" import { QueryClient } from "@tanstack/react-query" import { locales } from "~/i18n" +import dayjs from "~/lib/dayjs" import { getSiteLink } from "~/lib/helpers" import { NextServerResponse } from "~/lib/server-helper" import { ExpandedNote, PageVisibilityEnum } from "~/lib/types" diff --git a/src/components/common/Heatmap.tsx b/src/components/common/Heatmap.tsx index d2d5a960..d47d98a0 100644 --- a/src/components/common/Heatmap.tsx +++ b/src/components/common/Heatmap.tsx @@ -1,14 +1,11 @@ "use client" -import dayjs from "dayjs" -import advancedFormat from "dayjs/plugin/advancedFormat" import { useTranslations } from "next-intl" import { Tooltip } from "~/components/ui/Tooltip" +import dayjs from "~/lib/dayjs" import { useCalendar } from "~/queries/page" -dayjs.extend(advancedFormat) - export default function CalHeatmap({ characterId }: { characterId?: number }) { const calendar = useCalendar(characterId) const t = useTranslations() diff --git a/src/hooks/useDate.ts b/src/hooks/useDate.ts index 6e2c340f..edc9a3f1 100644 --- a/src/hooks/useDate.ts +++ b/src/hooks/useDate.ts @@ -1,23 +1,7 @@ -import dayjs from "dayjs" - -import "dayjs/locale/en" -import "dayjs/locale/ja" -import "dayjs/locale/zh" -import "dayjs/locale/zh-tw" - -import duration from "dayjs/plugin/duration" -import localizedFormat from "dayjs/plugin/localizedFormat" -import relativeTime from "dayjs/plugin/relativeTime" -import tz from "dayjs/plugin/timezone" -import utc from "dayjs/plugin/utc" import { useLocale } from "next-intl" import { useMemo } from "react" -dayjs.extend(localizedFormat) -dayjs.extend(utc) -dayjs.extend(tz) -dayjs.extend(duration) -dayjs.extend(relativeTime) +import dayjs from "~/lib/dayjs" export function useDate() { const locale = useLocale() diff --git a/src/lib/dayjs.ts b/src/lib/dayjs.ts new file mode 100644 index 00000000..ffa511fc --- /dev/null +++ b/src/lib/dayjs.ts @@ -0,0 +1,20 @@ +import dayjs from "dayjs" + +import "dayjs/locale/en" +import "dayjs/locale/ja" +import "dayjs/locale/zh" +import "dayjs/locale/zh-tw" + +import duration from "dayjs/plugin/duration" +import localizedFormat from "dayjs/plugin/localizedFormat" +import relativeTime from "dayjs/plugin/relativeTime" +import tz from "dayjs/plugin/timezone" +import utc from "dayjs/plugin/utc" + +dayjs.extend(localizedFormat) +dayjs.extend(utc) +dayjs.extend(tz) +dayjs.extend(duration) +dayjs.extend(relativeTime) + +export default dayjs diff --git a/src/lib/page-helpers.ts b/src/lib/page-helpers.ts index d3349235..15f2372b 100644 --- a/src/lib/page-helpers.ts +++ b/src/lib/page-helpers.ts @@ -1,4 +1,4 @@ -import dayjs from "dayjs" +import dayjs from "~/lib/dayjs" import { ExpandedNote, PageVisibilityEnum } from "./types" diff --git a/src/models/home.model.ts b/src/models/home.model.ts index 19f0d49c..c567dc0d 100644 --- a/src/models/home.model.ts +++ b/src/models/home.model.ts @@ -1,10 +1,10 @@ import type { NoteEntity } from "crossbell" -import dayjs from "dayjs" import { indexer } from "@crossbell/indexer" import { gql } from "@urql/core" import countCharacters from "~/lib/character-count" +import dayjs from "~/lib/dayjs" import { expandCrossbellNote } from "~/lib/expand-unit" import { ExpandedNote, Language } from "~/lib/types" import { client } from "~/queries/graphql" @@ -154,8 +154,9 @@ export async function getFeed({ resultAll = { list, cursor: list?.length - ? `${list[list.length - 1]?.characterId}_${list[list.length - 1] - ?.noteId}` + ? `${list[list.length - 1]?.characterId}_${ + list[list.length - 1]?.noteId + }` : undefined, count: list?.length || 0, } @@ -271,8 +272,9 @@ export async function getFeed({ resultAll = { list, cursor: list?.length - ? `${list[list.length - 1]?.characterId}_${list[list.length - 1] - ?.noteId}` + ? `${list[list.length - 1]?.characterId}_${ + list[list.length - 1]?.noteId + }` : undefined, count: list?.length || 0, } @@ -340,8 +342,9 @@ export async function getFeed({ resultAll = { list, cursor: list?.length - ? `${list[list.length - 1]?.characterId}_${list[list.length - 1] - ?.noteId}` + ? `${list[list.length - 1]?.characterId}_${ + list[list.length - 1]?.noteId + }` : undefined, count: list?.length || 0, } @@ -488,8 +491,9 @@ export async function getFeed({ resultAll = { list: list, cursor: list?.length - ? `${list[list.length - 1]?.characterId}_${list[list.length - 1] - ?.noteId}` + ? `${list[list.length - 1]?.characterId}_${ + list[list.length - 1]?.noteId + }` : undefined, count: list?.length || 0, } @@ -565,8 +569,9 @@ export async function getFeed({ resultAll = { list: list, cursor: list?.length - ? `${list[list.length - 1]?.characterId}_${list[list.length - 1] - ?.noteId}` + ? `${list[list.length - 1]?.characterId}_${ + list[list.length - 1]?.noteId + }` : undefined, count: list?.length || 0, } @@ -798,8 +803,9 @@ export async function getFeed({ ) const cursor = list?.length - ? `${list[list.length - 1]?.characterId}_${list[list.length - 1] - ?.noteId}` + ? `${list[list.length - 1]?.characterId}_${ + list[list.length - 1]?.noteId + }` : undefined list = list.sort((a, b) => { diff --git a/src/models/page.model.ts b/src/models/page.model.ts index be2cb34c..d6e17ad7 100644 --- a/src/models/page.model.ts +++ b/src/models/page.model.ts @@ -6,9 +6,6 @@ import type { NoteEntity, NoteMetadata, } from "crossbell" -import dayjs from "dayjs" -import advancedFormat from "dayjs/plugin/advancedFormat" -import weekOfYear from "dayjs/plugin/weekOfYear" import { type Address } from "viem" import { GeneralAccount } from "@crossbell/connect-kit" @@ -17,6 +14,7 @@ import { extractCharacterAttribute } from "@crossbell/util-metadata" import { gql } from "@urql/core" import { RESERVED_TAGS } from "~/lib/constants" +import dayjs from "~/lib/dayjs" import { editor2Crossbell } from "~/lib/editor-converter" import { expandCrossbellNote } from "~/lib/expand-unit" import { filterComment } from "~/lib/filter" @@ -32,9 +30,6 @@ import { } from "~/lib/types" import { client } from "~/queries/graphql" -dayjs.extend(weekOfYear) -dayjs.extend(advancedFormat) - export const PINNED_PAGE_KEY = "xlog_pinned_page" export async function checkPageSlug(input: {