From 78f0001f081fabc8f201762995a1c962db3b3acf Mon Sep 17 00:00:00 2001 From: DIYgod Date: Mon, 31 Jul 2023 18:29:50 +0100 Subject: [PATCH] feat: replace `isPost` with the note type --- .../[subdomain]/editor/page-component.tsx | 44 +++--- src/app/dashboard/[subdomain]/pages/page.tsx | 2 +- src/app/dashboard/[subdomain]/posts/page.tsx | 2 +- .../dashboard/DeleteConfirmationModal.tsx | 6 +- src/components/dashboard/OptionsButton.tsx | 8 +- src/components/dashboard/PagesManager.tsx | 139 +++++++++--------- .../PagesManagerBatchSelectActionTab.tsx | 18 +-- src/components/dashboard/PagesManagerMenu.tsx | 22 ++- src/components/dashboard/PublishButton.tsx | 7 +- src/components/site/EditButton.tsx | 9 +- src/components/site/PostMeta.tsx | 4 +- src/components/ui/EmptyState.tsx | 2 +- src/lib/i18n/locales/zh/dashboard.json | 6 + src/lib/types.ts | 2 + src/models/page.model.ts | 18 ++- src/queries/page.ts | 11 +- 16 files changed, 155 insertions(+), 145 deletions(-) diff --git a/src/app/dashboard/[subdomain]/editor/page-component.tsx b/src/app/dashboard/[subdomain]/editor/page-component.tsx index 6bcf6ad8..9ffe99df 100644 --- a/src/app/dashboard/[subdomain]/editor/page-component.tsx +++ b/src/app/dashboard/[subdomain]/editor/page-component.tsx @@ -52,7 +52,7 @@ import { getSiteLink, getTwitterShareUrl } from "~/lib/helpers" import { useTranslation } from "~/lib/i18n/client" import { getPageVisibility } from "~/lib/page-helpers" import { delStorage, setStorage } from "~/lib/storage" -import { ExpandedNote, PageVisibilityEnum } from "~/lib/types" +import { ExpandedNote, NoteType, PageVisibilityEnum } from "~/lib/types" import { cn, pick } from "~/lib/utils" import { Rendered, renderPageContent } from "~/markdown" import { checkPageSlug } from "~/models/page.model" @@ -98,7 +98,7 @@ export default function SubdomainEditor() { const searchParams = useSearchParams() let pageId = searchParams?.get("id") as string | undefined - const isPost = searchParams?.get("type") === "post" + const type = (searchParams?.get("type") || "post") as NoteType const defaultTag = searchParams?.get("tag") const site = useGetSite(subdomain) @@ -206,7 +206,7 @@ export default function SubdomainEditor() { setStorage(draftKey, { date: +new Date(), values: newValues, - isPost: isPost, + type, }) queryClient.invalidateQueries([ "getPagesBySite", @@ -215,7 +215,7 @@ export default function SubdomainEditor() { } useEditorState.setState(newValues) }, - [isPost, queryClient, subdomain, visibility], + [type, queryClient, subdomain, visibility], ) const createPage = useCreatePage() @@ -246,7 +246,7 @@ export default function SubdomainEditor() { if (visibility === PageVisibilityEnum.Draft) { createPage.mutate({ ...baseValues, - isPost: isPost, + type, }) } else { updatePage.mutate({ @@ -545,12 +545,12 @@ export default function SubdomainEditor() { ) }, [draftKey, subdomain, site.data?.characterId]) - const presentAdvancedModal = useEditorAdvancedModal({ isPost }) + const presentAdvancedModal = useEditorAdvancedModal({ type }) const extraProperties = ( @@ -654,7 +654,7 @@ export default function SubdomainEditor() { visibility !== PageVisibilityEnum.Modified && visibility !== PageVisibilityEnum.Draft } - isPost={isPost} + type={type} isModified={visibility === PageVisibilityEnum.Modified} discardChanges={discardChanges} /> @@ -736,7 +736,7 @@ export default function SubdomainEditor() { (key: K, value: Values[K]) => void - isPost: boolean + type: NoteType subdomain: string defaultSlug: string @@ -823,7 +823,9 @@ const EditorExtraProperties = memo( name="slug" value={values.slug} placeholder={defaultSlug} - label={t(`${isPost ? "Post" : "Page"} slug`) || ""} + label={ + t(`${type.charAt(0).toUpperCase() + type.slice(1)} slug`) || "" + } id="slug" isBlock onChange={(e: ChangeEvent) => @@ -833,9 +835,7 @@ const EditorExtraProperties = memo( <> {(values.slug || defaultSlug) && ( <> - {t( - `This ${isPost ? "post" : "page"} will be accessible at`, - )}{" "} + {t(`This ${type} will be accessible at`)}{" "} { +const useEditorAdvancedModal = ({ type }: { type: NoteType }) => { const { t } = useTranslation("dashboard") const { present } = useModalStack() @@ -900,7 +900,7 @@ const useEditorAdvancedModal = ({ isPost }: { isPost: boolean }) => { return () => { present({ title: t("Advanced Settings"), - content: () => , + content: () => , modalProps: { withConfirm: true, }, @@ -909,8 +909,8 @@ const useEditorAdvancedModal = ({ isPost }: { isPost: boolean }) => { } const EditorAdvancedModal: FC<{ - isPost: boolean -}> = ({ isPost }) => { + type: NoteType +}> = ({ type }) => { const { t } = useTranslation("dashboard") const values = useEditorState( @@ -971,9 +971,7 @@ const EditorAdvancedModal: FC<{ />
{t( - `This ${ - isPost ? "post" : "page" - } will be accessible from this time. Leave blank to use the current time.`, + `This ${type} will be accessible from this time. Leave blank to use the current time.`, )}
{values.publishedAt > new Date().toISOString() && ( diff --git a/src/app/dashboard/[subdomain]/pages/page.tsx b/src/app/dashboard/[subdomain]/pages/page.tsx index a0c6d31b..6306b84f 100644 --- a/src/app/dashboard/[subdomain]/pages/page.tsx +++ b/src/app/dashboard/[subdomain]/pages/page.tsx @@ -1,5 +1,5 @@ import { PagesManager } from "~/components/dashboard/PagesManager" export default function SubdomainPages() { - return + return } diff --git a/src/app/dashboard/[subdomain]/posts/page.tsx b/src/app/dashboard/[subdomain]/posts/page.tsx index 51cc1b40..14770fe1 100644 --- a/src/app/dashboard/[subdomain]/posts/page.tsx +++ b/src/app/dashboard/[subdomain]/posts/page.tsx @@ -1,5 +1,5 @@ import { PagesManager } from "~/components/dashboard/PagesManager" export default function SubdomainPosts() { - return + return } diff --git a/src/components/dashboard/DeleteConfirmationModal.tsx b/src/components/dashboard/DeleteConfirmationModal.tsx index 4ac34339..eec62edf 100644 --- a/src/components/dashboard/DeleteConfirmationModal.tsx +++ b/src/components/dashboard/DeleteConfirmationModal.tsx @@ -7,19 +7,19 @@ export const DeleteConfirmationModal = ({ open, setOpen, onConfirm, - isPost, + type, }: { open: boolean setOpen: (open: boolean) => void onConfirm: () => void - isPost?: boolean + type?: string }) => { const { t } = useTranslation("dashboard") return (
-

{t("delete_confirmation", { context: isPost ? "post" : "page" })}

+

{t("delete_confirmation", { context: type })}

{ {batchSelected.length > 0 ? ( {
{pages.isLoading &&

{t("Loading")}...

} {!pages.isLoading && !pages.data?.pages?.[0].count && ( - + )} {pages.data?.pages.map( @@ -381,7 +386,7 @@ export const PagesManager = ({ isPost }: { isPost: boolean }) => { @@ -403,12 +408,10 @@ export const PagesManager = ({ isPost }: { isPost: boolean }) => { > {siteT("load more", { name: t( - isPost - ? "post" - : "page" + - ((pages.data?.pages?.[0].count || 0) - currentLength > 1 - ? "s" - : ""), + type + + ((pages.data?.pages?.[0].count || 0) - currentLength > 1 + ? "s" + : ""), ), count: (pages.data?.pages?.[0].count || 0) - currentLength, })} diff --git a/src/components/dashboard/PagesManagerBatchSelectActionTab.tsx b/src/components/dashboard/PagesManagerBatchSelectActionTab.tsx index 7d3d07e7..f841c54d 100644 --- a/src/components/dashboard/PagesManagerBatchSelectActionTab.tsx +++ b/src/components/dashboard/PagesManagerBatchSelectActionTab.tsx @@ -1,4 +1,3 @@ -import { useParams } from "next/navigation" import React, { useState } from "react" import toast from "react-hot-toast" @@ -8,7 +7,7 @@ import { useQueryClient } from "@tanstack/react-query" import { type TabItem, Tabs } from "~/components/ui/Tabs" import { useTranslation } from "~/lib/i18n/client" import { delStorage, getStorage, setStorage } from "~/lib/storage" -import { ExpandedNote } from "~/lib/types" +import { ExpandedNote, NoteType } from "~/lib/types" import { useDeletePage, useUpdatePage } from "~/queries/page" import { DeleteConfirmationModal } from "./DeleteConfirmationModal" @@ -18,12 +17,12 @@ function getPageId(page: ExpandedNote) { } export const PagesManagerBatchSelectActionTab = ({ - isPost, + type, pages, batchSelected, setBatchSelected, }: { - isPost: boolean + type: NoteType pages?: InfiniteData<{ list: ExpandedNote[] }> @@ -32,9 +31,6 @@ export const PagesManagerBatchSelectActionTab = ({ }) => { const { t } = useTranslation("dashboard") - const params = useParams() - const subdomain = params?.subdomain as string - const queryClient = useQueryClient() // Convert or Delete page @@ -63,7 +59,7 @@ export const PagesManagerBatchSelectActionTab = ({ }, }, { - text: "Convert to " + (isPost ? "Page" : "Post"), + text: "Convert to " + (type === "post" ? "Page" : "Post"), onClick: async () => { // Start message const toastId = toast.loading("Converting...") @@ -87,14 +83,14 @@ export const PagesManagerBatchSelectActionTab = ({ // Is draft const key = `draft-${page?.characterId}-${page.draftKey}` const data = getStorage(key) - data.isPost = !isPost + data.type = data.type === "post" ? "page" : "post" setStorage(key, data) } else { // IsNote return updatePage.mutate({ characterId: page.characterId, noteId: page.noteId, - isPost: !isPost, // Change type + type: type === "post" ? "page" : "post", }) } }), @@ -195,7 +191,7 @@ export const PagesManagerBatchSelectActionTab = ({ open={deleteConfirmModalOpen} setOpen={setDeleteConfirmModalOpen} onConfirm={onDelete} - isPost={isPost} + type={type} /> ) diff --git a/src/components/dashboard/PagesManagerMenu.tsx b/src/components/dashboard/PagesManagerMenu.tsx index eda61e04..7ae389e4 100644 --- a/src/components/dashboard/PagesManagerMenu.tsx +++ b/src/components/dashboard/PagesManagerMenu.tsx @@ -9,19 +9,17 @@ import { useGetState } from "~/hooks/useGetState" import { getNoteSlugFromNote, getTwitterShareUrl } from "~/lib/helpers" import { useTranslation } from "~/lib/i18n/client" import { delStorage, getStorage, setStorage } from "~/lib/storage" -import { ExpandedNote } from "~/lib/types" +import { ExpandedNote, NoteType } from "~/lib/types" import { useDeletePage, usePinPage, useUpdatePage } from "~/queries/page" import { useGetSite } from "~/queries/site" import { DeleteConfirmationModal } from "./DeleteConfirmationModal" -const usePageEditLink = (page: ExpandedNote, isPost: boolean) => { +const usePageEditLink = (page: ExpandedNote, type: NoteType) => { const params = useParams() const subdomain = params?.subdomain as string - return `/dashboard/${subdomain}/editor?id=${page.noteId}&type=${ - isPost ? "post" : "page" - }` + return `/dashboard/${subdomain}/editor?id=${page.noteId}&type=${type}` } interface Item { @@ -30,11 +28,11 @@ interface Item { onClick: () => void } export const PagesManagerMenu = ({ - isPost, + type, page, onClick: onClose, }: { - isPost: boolean + type: NoteType page: ExpandedNote onClick: () => void }) => { @@ -45,7 +43,7 @@ export const PagesManagerMenu = ({ const subdomain = params?.subdomain as string const updatePage = useUpdatePage() - const editLink = usePageEditLink(page, isPost) + const editLink = usePageEditLink(page, type) const queryClient = useQueryClient() const deletePage = useDeletePage() const pinPage = usePinPage(page) @@ -105,7 +103,7 @@ export const PagesManagerMenu = ({ onClick: pinPage.togglePin, }, { - text: "Convert to " + (isPost ? "Page" : "Post"), + text: "Convert to " + (type === "post" ? "Page" : "Post"), icon: ( ), @@ -116,7 +114,7 @@ export const PagesManagerMenu = ({ const data = getStorage( `draft-${site.data?.characterId}-${page.draftKey}`, ) - data.isPost = !isPost + data.type = data.type === "post" ? "page" : "post" setStorage(`draft-${site.data?.characterId}-${page.draftKey}`, data) queryClient.invalidateQueries([ "getPagesBySite", @@ -129,7 +127,7 @@ export const PagesManagerMenu = ({ } else { setConvertToastId(toastId) updatePage.mutate({ - isPost: !isPost, + type, characterId: page.characterId, noteId: page.noteId, }) @@ -231,7 +229,7 @@ export const PagesManagerMenu = ({ open={deleteConfirmModalOpen} setOpen={setDeleteConfirmModalOpen} onConfirm={onDelete} - isPost={isPost} + type={type} /> ) diff --git a/src/components/dashboard/PublishButton.tsx b/src/components/dashboard/PublishButton.tsx index 474b914d..e0a32447 100644 --- a/src/components/dashboard/PublishButton.tsx +++ b/src/components/dashboard/PublishButton.tsx @@ -2,6 +2,7 @@ import { useEffect, useRef, useState } from "react" import useOnClickOutside from "use-onclickoutside" import { useTranslation } from "~/lib/i18n/client" +import { NoteType } from "~/lib/types" import { Button, ButtonGroup } from "../ui/Button" import { DeleteConfirmationModal } from "./DeleteConfirmationModal" @@ -15,7 +16,7 @@ export const PublishButton = ({ isDisabled, isModified, twitterShareUrl, - isPost, + type, }: { savePage: () => void deletePage: () => void @@ -23,7 +24,7 @@ export const PublishButton = ({ isSaving: boolean isDisabled: boolean twitterShareUrl?: string - isPost: boolean + type: NoteType isModified: boolean discardChanges: () => void }) => { @@ -114,7 +115,7 @@ export const PublishButton = ({ onConfirm={() => { deletePage() }} - isPost={isPost} + type={type} />
) diff --git a/src/components/site/EditButton.tsx b/src/components/site/EditButton.tsx index cbe20482..88992adb 100644 --- a/src/components/site/EditButton.tsx +++ b/src/components/site/EditButton.tsx @@ -6,15 +6,16 @@ import { UniLink } from "~/components/ui/UniLink" import { useUserRole } from "~/hooks/useUserRole" import { SITE_URL } from "~/lib/env" import { useTranslation } from "~/lib/i18n/client" +import { NoteType } from "~/lib/types" export const EditButton = ({ handle, noteId, - isPost, + type, }: { handle?: string noteId?: number - isPost?: boolean + type?: NoteType }) => { const { t } = useTranslation("common") const [showEdit, setShowEdit] = useState(false) @@ -30,9 +31,7 @@ export const EditButton = ({ {showEdit && ( {t("Edit")} diff --git a/src/components/site/PostMeta.tsx b/src/components/site/PostMeta.tsx index bfb6b339..d798a9a8 100644 --- a/src/components/site/PostMeta.tsx +++ b/src/components/site/PostMeta.tsx @@ -4,7 +4,7 @@ import { EditButton } from "~/components/site/EditButton" import PostTag from "~/components/site/PostTag" import { UniLink } from "~/components/ui/UniLink" import { CSB_SCAN } from "~/lib/env" -import { ExpandedCharacter, ExpandedNote } from "~/lib/types" +import { ExpandedCharacter, ExpandedNote, NoteType } from "~/lib/types" export default function PostMeta({ page, @@ -47,7 +47,7 @@ export default function PostMeta({
{summary && ( diff --git a/src/components/ui/EmptyState.tsx b/src/components/ui/EmptyState.tsx index ba285287..f4875565 100644 --- a/src/components/ui/EmptyState.tsx +++ b/src/components/ui/EmptyState.tsx @@ -4,7 +4,7 @@ export const EmptyState = ({ resource }: { resource?: string }) => {
{" "} - No {resource || "posts"} Yet. + No {resource || "post"} Yet.
) diff --git a/src/lib/i18n/locales/zh/dashboard.json b/src/lib/i18n/locales/zh/dashboard.json index a5892996..0f1a2c3c 100644 --- a/src/lib/i18n/locales/zh/dashboard.json +++ b/src/lib/i18n/locales/zh/dashboard.json @@ -1,7 +1,9 @@ { "Dashboard": "仪表盘", + "Publishing": "发布", "Posts": "文章", "Pages": "页面", + "Portfolios": "作品集", "Notifications": "通知", "Unread notifications": "未读通知", "Settings": "设置", @@ -20,6 +22,7 @@ "Failed to convert.": "转换失败。", "All Posts": "所有文章", "All Pages": "所有页面", + "All Portfolios": "所有作品集", "Published": "已发布", "published": "已发布", "Draft": "草稿", @@ -33,6 +36,7 @@ "pages add": "创建页面后,你可以将其<2>添加到你网站的导航菜单中,以便访问者可以找到它。", "New Post": "新建文章", "New Page": "新建页面", + "New Portfolio": "新建作品", "Import": "导入", "Import markdown file with front matter supported": "导入 Markdown 文件(支持 Front Matter)", "View Site": "查看站点", @@ -69,6 +73,7 @@ "Publish at": "发布时间", "This post will be accessible from this time. Leave blank to use the current time.": "此文章将从此时间开始可访问。留空则使用当前时间。", "This page will be accessible from this time. Leave blank to use the current time.": "此页面将从此时间开始可访问。留空则使用当前时间。", + "This portfolio will be accessible from this time. Leave blank to use the current time.": "此作品将从此时间开始可访问。留空则使用当前时间。", "The post is currently not public as its publication date has been scheduled for a future time.": "此文章当前没有公开,因为它的发布日期已被定为将来的某个时间。", "Post slug": "文章短链接", "Page slug": "页面短链接", @@ -198,6 +203,7 @@ "Confirm Deletion?": "确认删除?", "delete_confirmation_post": "你确定要删除这篇文章吗?", "delete_confirmation_page": "你确定要删除这个页面吗?", + "delete_confirmation_portfolio": "你确定要删除这个作品吗?", "Confirm": "确认", "Cancel": "取消", "Published a new post on my blockchain blog: {{title}}. Check it out now!": "在我的区块链博客发布了一篇新文章《{{title}}》,快来看看吧!", diff --git a/src/lib/types.ts b/src/lib/types.ts index 7e9d1992..1b812f3f 100644 --- a/src/lib/types.ts +++ b/src/lib/types.ts @@ -113,3 +113,5 @@ export type ExpandedCharacter = CharacterEntity & { } export type ColorScheme = "dark" | "light" + +export type NoteType = "post" | "page" | "portfolio" diff --git a/src/models/page.model.ts b/src/models/page.model.ts index 2b3e8598..db9e465f 100644 --- a/src/models/page.model.ts +++ b/src/models/page.model.ts @@ -18,7 +18,7 @@ import { filterCommentCharacter } from "~/lib/filter-character" import { getNoteSlug } from "~/lib/helpers" import { checkSlugReservedWords } from "~/lib/slug-reserved-words" import { getKeys, getStorage } from "~/lib/storage" -import { ExpandedNote, PageVisibilityEnum } from "~/lib/types" +import { ExpandedNote, NoteType, PageVisibilityEnum } from "~/lib/types" import { client } from "~/queries/graphql" export const PINNED_PAGE_KEY = "xlog_pinned_page" @@ -65,14 +65,19 @@ export async function postNotes( const getLocalPages = (input: { characterId: number - isPost?: boolean + isPost?: boolean // In order to be compatible with old drafts + type?: NoteType handle?: string }) => { const pages: ExpandedNote[] = [] getKeys([`draft-${input.characterId}-`, `draft-${input.handle}-`]).forEach( (key) => { const page = getStorage(key) - if (input.isPost === undefined || page.isPost === input.isPost) { + if ( + page.isPost === input.isPost || + page.type === input.type || + input.type === undefined + ) { const note: ExpandedNote = { characterId: input.characterId, noteId: 0, @@ -113,7 +118,7 @@ const getLocalPages = (input: { date_published: page.values?.publishedAt, summary: page.values?.excerpt, tags: [ - page.isPost ? "post" : "page", + input.type, ...(page.values?.tags ?.split(",") .map((tag: string) => tag.trim()) @@ -135,7 +140,7 @@ const getLocalPages = (input: { export async function getPagesBySite(input: { characterId?: number - type: "post" | "page" + type: NoteType visibility?: PageVisibilityEnum limit?: number cursor?: string @@ -211,7 +216,8 @@ export async function getPagesBySite(input: { const local = getLocalPages({ characterId: input.characterId, - isPost: input.type === "post", + isPost: input.type === "post", // In order to be compatible with old drafts + type: input.type, handle: input.handle, }) diff --git a/src/queries/page.ts b/src/queries/page.ts index 26c6a54d..ef47f4bd 100644 --- a/src/queries/page.ts +++ b/src/queries/page.ts @@ -25,6 +25,7 @@ import { useQueryClient, } from "@tanstack/react-query" +import { NoteType } from "~/lib/types" import * as pageModel from "~/models/page.model" export const useGetPagesBySiteLite = ( @@ -220,7 +221,7 @@ export function useCreatePage() { content?: string publishedAt?: string excerpt?: string - isPost?: boolean + type?: NoteType cover?: { address?: string mime_type?: string @@ -240,7 +241,7 @@ export function useCreatePage() { date_published: input.publishedAt || new Date().toISOString(), summary: input.excerpt, tags: [ - input.isPost ? "post" : "page", + input.type, ...(input.tags ?.split(",") .map((tag) => tag.trim()) @@ -306,7 +307,7 @@ export function useUpdatePage() { content?: string publishedAt?: string excerpt?: string - isPost?: boolean + type?: NoteType cover?: { address?: string mime_type?: string @@ -341,8 +342,8 @@ export function useUpdatePage() { if (!metadataDraft.tags) { metadataDraft.tags = [] } - if (input.isPost !== undefined) { - metadataDraft.tags[0] = input.isPost ? "post" : "page" + if (input.type !== undefined) { + metadataDraft.tags[0] = input.type } if (input.tags !== undefined) { metadataDraft.tags = [