diff --git a/src/app/dashboard/[subdomain]/editor/portfolio-editor.tsx b/src/app/dashboard/[subdomain]/editor/portfolio-editor.tsx new file mode 100644 index 00000000..dbb680de --- /dev/null +++ b/src/app/dashboard/[subdomain]/editor/portfolio-editor.tsx @@ -0,0 +1,428 @@ +"use client" + +import { nanoid } from "nanoid" +import { useParams, useRouter, useSearchParams } from "next/navigation" +import { ChangeEvent, memo, useCallback, useEffect, useState } from "react" +import toast from "react-hot-toast" + +import { useQueryClient } from "@tanstack/react-query" + +import { DashboardMain } from "~/components/dashboard/DashboardMain" +import { PublishButton } from "~/components/dashboard/PublishButton" +import { Button } from "~/components/ui/Button" +import { FieldLabel } from "~/components/ui/FieldLabel" +import { ImageUploader } from "~/components/ui/ImageUploader" +import { Input } from "~/components/ui/Input" +import { ModalContentProps, useModalStack } from "~/components/ui/ModalStack" +import { UniLink } from "~/components/ui/UniLink" +import { + Values, + initialEditorState, + useEditorState, +} from "~/hooks/useEditorState" +import { useGetState } from "~/hooks/useGetState" +import { useBeforeMounted } from "~/hooks/useSyncOnce" +import { showConfetti } from "~/lib/confetti" +import { CSB_SCAN } from "~/lib/env" +import { getTwitterShareUrl } from "~/lib/helpers" +import { useTranslation } from "~/lib/i18n/client" +import { getPageVisibility } from "~/lib/page-helpers" +import { delStorage, setStorage } from "~/lib/storage" +import { PageVisibilityEnum } from "~/lib/types" +import { cn } from "~/lib/utils" +import { + useCreatePage, + useDeletePage, + useGetPage, + useUpdatePage, +} from "~/queries/page" +import { useGetSite } from "~/queries/site" + +export default function PortfolioEditor() { + const router = useRouter() + const queryClient = useQueryClient() + const { t } = useTranslation("dashboard") + const params = useParams() + const subdomain = params?.subdomain as string + const searchParams = useSearchParams() + + let pageId = searchParams?.get("id") as string | undefined + const type = "portfolio" + + const site = useGetSite(subdomain) + + const [draftKey, setDraftKey] = useState("") + useEffect(() => { + if (subdomain) { + let key + if (!pageId) { + const randomId = nanoid() + key = `draft-${site.data?.characterId}-!local-${randomId}` + setDraftKey(key) + queryClient.invalidateQueries([ + "getPagesBySite", + site.data?.characterId, + ]) + router.replace( + `/dashboard/${subdomain}/editor?id=!local-${randomId}&type=${type}`, + ) + } else { + key = `draft-${site.data?.characterId}-${pageId}` + } + setDraftKey(key) + } + }, [ + subdomain, + pageId, + queryClient, + router, + site.data?.characterId, + searchParams, + ]) + + const page = useGetPage({ + characterId: site.data?.characterId, + noteId: pageId && /\d+/.test(pageId) ? +pageId : undefined, + slug: pageId || draftKey.replace(`draft-${site.data?.characterId}-`, ""), + handle: subdomain, + }) + + const [visibility, setVisibility] = useState() + + useEffect(() => { + if (page.isSuccess) { + setVisibility(getPageVisibility(page.data || undefined)) + } + }, [page.isSuccess, page.data]) + + // reset editor state when page changes + useBeforeMounted(() => { + useEditorState.setState({ + ...initialEditorState, + }) + }) + + const values = useEditorState() + + const getValues = useGetState(values) + const getDraftKey = useGetState(draftKey) + + const updateValue = useCallback( + (key: K, value: Values[K]) => { + if (visibility !== PageVisibilityEnum.Draft) { + setVisibility(PageVisibilityEnum.Modified) + } + + const values = getValues() + const draftKey = getDraftKey() + const newValues = { ...values, [key]: value } + if (draftKey) { + setStorage(draftKey, { + date: +new Date(), + values: newValues, + type, + }) + queryClient.invalidateQueries([ + "getPagesBySite", + site.data?.characterId, + ]) + } + useEditorState.setState(newValues) + }, + [type, queryClient, subdomain, visibility], + ) + + const createPage = useCreatePage() + const updatePage = useUpdatePage() + + const savePage = async () => { + const baseValues = { + ...values, + characterId: site.data?.characterId, + cover: values.cover, + } + if (visibility === PageVisibilityEnum.Draft) { + createPage.mutate({ + ...baseValues, + type, + }) + } else { + updatePage.mutate({ + ...baseValues, + noteId: page?.data?.noteId, + }) + } + } + + const deleteP = useDeletePage() + const deletePage = async () => { + if (page.data) { + if (!page.data?.noteId) { + // Is draft + delStorage(`draft-${page.data.characterId}-${page.data.draftKey}`) + } else { + // Is Note + return deleteP.mutate({ + noteId: page.data.noteId, + characterId: page.data.characterId, + }) + } + } + } + + useEffect(() => { + if (deleteP.isSuccess) { + toast.success(t("Deleted!")) + deleteP.reset() + router.push(`/dashboard/${subdomain}/${type}s`) + } + }, [deleteP.isSuccess]) + + const { present } = useModalStack() + + useEffect(() => { + if (createPage.isSuccess || updatePage.isSuccess) { + if (draftKey) { + delStorage(draftKey) + queryClient.invalidateQueries([ + "getPagesBySite", + site.data?.characterId, + ]) + queryClient.invalidateQueries([ + "getPage", + draftKey.replace(`draft-${site.data?.characterId}-`, ""), + ]) + } else { + queryClient.invalidateQueries(["getPage", pageId]) + } + + if (createPage.data?.noteId) { + router.replace( + `/dashboard/${subdomain}/editor?id=${createPage.data?.noteId}&type=${type}`, + ) + } + + const transactionUrl = `${CSB_SCAN}/tx/${ + page.data?.updatedTransactionHash || page.data?.transactionHash // TODO + }` + + const modalId = "publish-modal" + present({ + title: `🎉 ${t("Published!")}`, + id: modalId, + content: (props) => ( + + ), + }) + + showConfetti() + + createPage.reset() + updatePage.reset() + } + }, [createPage.isSuccess, updatePage.isSuccess]) + + useEffect(() => { + if (createPage.isError || updatePage.isError) { + toast.error("Error: " + (createPage.error || updatePage.error)) + createPage.reset() + updatePage.reset() + } + }, [createPage.isError, updatePage.isSuccess]) + + useEffect(() => { + if (!page.data?.metadata || !draftKey) return + useEditorState.setState({ + published: !!page.data.noteId, + title: page.data.metadata?.content?.title || "", + excerpt: page.data.metadata?.content?.summary || "", + cover: page.data.metadata?.content?.attachments?.find( + (attachment) => attachment.name === "cover", + ) || { + address: "", + mime_type: "", + }, + externalUrl: page.data.metadata?.content?.external_urls?.[0] || "", + }) + }, [page.data, subdomain, draftKey, site.data?.characterId]) + + const discardChanges = useCallback(() => { + if (draftKey) { + delStorage(draftKey) + queryClient.invalidateQueries(["getPagesBySite", site.data?.characterId]) + page.remove() + page.refetch() + } + }, [draftKey, site.data?.characterId]) + + return ( + <> + +
+ {page.isLoading ? ( +
+ {t("Loading")}... +
+ ) : ( + <> +
+ +
+
+
+ + + {t(visibility as string)} + +
+
+ + )} +
+
+ + ) +} + +const EditorExtraProperties = memo( + ({ + updateValue, + }: { + updateValue: (key: K, value: Values[K]) => void + }) => { + const values = useEditorState() + const { t } = useTranslation("dashboard") + + return ( +
+
+ ) => { + updateValue("externalUrl", e.target.value) + }} + /> +
+
+ + { + const { address, mime_type } = key as { + address?: string + mime_type?: string + } + updateValue("cover", { + address, + mime_type, + }) + }} + accept="image/*" + /> +
+
+ ) => { + updateValue("title", e.target.value) + }} + /> +
+
+ ) => { + updateValue("excerpt", e.target.value) + }} + /> +
+
+ ) + }, +) + +EditorExtraProperties.displayName = "EditorExtraProperties" + +const PublishedModal = ({ + dismiss, + transactionUrl, +}: ModalContentProps<{ + transactionUrl: string +}>) => { + const { t } = useTranslation("dashboard") + return ( + <> +
+ {t( + "Your post has been securely stored on the blockchain. Now you may want to", + )} +
    +
  • + + {t("View the transaction")} + +
  • +
+
+
+ +
+ + ) +} diff --git a/src/app/dashboard/[subdomain]/editor/post-editor.tsx b/src/app/dashboard/[subdomain]/editor/post-editor.tsx index e9d8853f..d2eb5dd5 100644 --- a/src/app/dashboard/[subdomain]/editor/post-editor.tsx +++ b/src/app/dashboard/[subdomain]/editor/post-editor.tsx @@ -46,6 +46,7 @@ import { useIsMobileLayout } from "~/hooks/useMobileLayout" import { useBeforeMounted } from "~/hooks/useSyncOnce" import { useUploadFile } from "~/hooks/useUploadFile" import { showConfetti } from "~/lib/confetti" +import { RESERVED_TAGS } from "~/lib/constants" import { getDefaultSlug } from "~/lib/default-slug" import { CSB_SCAN } from "~/lib/env" import { getSiteLink, getTwitterShareUrl } from "~/lib/helpers" @@ -371,7 +372,7 @@ export default function PostEditor() { slug: page.data.metadata?.content?.slug || "", tags: page.data.metadata?.content?.tags - ?.filter((tag) => tag !== "post" && tag !== "page") + ?.filter((tag) => !RESERVED_TAGS.includes(tag)) ?.join(", ") || "", content: page.data.metadata?.content?.content || "", cover: page.data.metadata?.content?.attachments?.find( diff --git a/src/components/dashboard/ImportPreview.tsx b/src/components/dashboard/ImportPreview.tsx index 738f438c..47af3342 100644 --- a/src/components/dashboard/ImportPreview.tsx +++ b/src/components/dashboard/ImportPreview.tsx @@ -3,6 +3,7 @@ import dynamic from "next/dynamic" import { useState } from "react" import { useDate } from "~/hooks/useDate" +import { RESERVED_TAGS } from "~/lib/constants" import { useTranslation } from "~/lib/i18n/client" const DynamicPageContent = dynamic(() => import("../common/PageContent"), { @@ -26,11 +27,10 @@ export const ImportPreview = ({ note }: { note: NoteMetadata }) => { > {date.formatDate(note.date_published!, undefined)} - {!!note.tags?.filter((tag) => tag !== "post" && tag !== "page") - .length && ( + {!!note.tags?.filter((tag) => !RESERVED_TAGS.includes(tag)).length && ( {note.tags - ?.filter((tag) => tag !== "post" && tag !== "page") + ?.filter((tag) => !RESERVED_TAGS.includes(tag)) .map((tag) => ( #{tag} diff --git a/src/components/site/PostMeta.tsx b/src/components/site/PostMeta.tsx index d798a9a8..3e10ed65 100644 --- a/src/components/site/PostMeta.tsx +++ b/src/components/site/PostMeta.tsx @@ -3,6 +3,7 @@ import { BlockchainIcon } from "~/components/icons/BlockchainIcon" import { EditButton } from "~/components/site/EditButton" import PostTag from "~/components/site/PostTag" import { UniLink } from "~/components/ui/UniLink" +import { RESERVED_TAGS } from "~/lib/constants" import { CSB_SCAN } from "~/lib/env" import { ExpandedCharacter, ExpandedNote, NoteType } from "~/lib/types" @@ -24,12 +25,12 @@ export default function PostMeta({