diff --git a/src/components/ui/Button.tsx b/src/components/ui/Button.tsx index 72da4594..fc593477 100644 --- a/src/components/ui/Button.tsx +++ b/src/components/ui/Button.tsx @@ -1,4 +1,4 @@ -import React from "react" +import React, { memo } from "react" import { cn } from "~/lib/utils" export const ButtonGroup: React.FC<{ children: React.ReactNode }> = ({ @@ -31,48 +31,50 @@ type ButtonProps = { rounded?: "full" | "lg" } -export const Button = React.forwardRef< - HTMLButtonElement, - React.ButtonHTMLAttributes & ButtonProps ->(function Button( - { - type, - children, - className, - isLoading, - isDisabled, - isBlock, - variant, - variantColor, - outlineColor, - size, - rounded, - isAutoWidth, - ...props - }, - ref, -) { - return ( - - ) -}) +export const Button = memo( + React.forwardRef< + HTMLButtonElement, + React.ButtonHTMLAttributes & ButtonProps + >(function Button( + { + type, + children, + className, + isLoading, + isDisabled, + isBlock, + variant, + variantColor, + outlineColor, + size, + rounded, + isAutoWidth, + ...props + }, + ref, + ) { + return ( + + ) + }), +) diff --git a/src/components/ui/EditorToolbar.tsx b/src/components/ui/EditorToolbar.tsx index 75f09be9..d4fb6daa 100644 --- a/src/components/ui/EditorToolbar.tsx +++ b/src/components/ui/EditorToolbar.tsx @@ -1,6 +1,5 @@ import { EditorView } from "@codemirror/view" -import { cn } from "~/lib/utils" -import { FC, useState } from "react" +import { FC, memo, useCallback, useState } from "react" import { ICommand } from "~/editor" import { Tooltip } from "./Tooltip" import { useTranslation } from "next-i18next" @@ -77,50 +76,56 @@ const ToolbarItemWithPopover: FC = ({ ) } -export const EditorToolbar: FC = ({ view, toolbars }) => { - const { t } = useTranslation("dashboard") - const renderToolbar = - (mode: ToolbarMode) => - // eslint-disable-next-line react/display-name - ({ name, icon, label, execute, ui }: ICommand) => { - return ui ? ( - - ) : ( - - - - ) - } +export const EditorToolbar: FC = memo( + ({ view, toolbars }) => { + const { t } = useTranslation("dashboard") + const renderToolbar = useCallback( + (mode: ToolbarMode) => + // eslint-disable-next-line react/display-name + ({ name, icon, label, execute, ui }: ICommand) => { + return ui ? ( + + ) : ( + + + + ) + }, + [view, t], + ) - return ( -
-
- {toolbars?.map(renderToolbar(ToolbarMode.Normal))} + return ( +
+
+ {toolbars?.map(renderToolbar(ToolbarMode.Normal))} +
-
- ) -} + ) + }, +) + +EditorToolbar.displayName = "EditorToolbar" diff --git a/src/hooks/useDate.ts b/src/hooks/useDate.ts index 464b5012..0b73f7e7 100644 --- a/src/hooks/useDate.ts +++ b/src/hooks/useDate.ts @@ -10,6 +10,7 @@ import "dayjs/locale/en" import "dayjs/locale/zh" import "dayjs/locale/zh-tw" import "dayjs/locale/ja" +import { useMemo } from "react" dayjs.extend(localizedFormat) dayjs.extend(utc) @@ -19,18 +20,23 @@ 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() - }, - } + const memoizedDateUtils = useMemo(() => { + 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() + }, + } + }, [i18n.resolvedLanguage]) + + return memoizedDateUtils } diff --git a/src/pages/dashboard/[subdomain]/editor.tsx b/src/pages/dashboard/[subdomain]/editor.tsx index 77c1c301..0e3f1342 100644 --- a/src/pages/dashboard/[subdomain]/editor.tsx +++ b/src/pages/dashboard/[subdomain]/editor.tsx @@ -198,94 +198,111 @@ export default function SubdomainEditor() { const [isCheersOpen, setIsCheersOpen] = useState(false) - const ExtraProperties = ( -
-
- ) => { - try { - const value = date.inLocalTimezone(e.target.value).toISOString() - updateValue("publishedAt", value) - } catch (error) {} - }} - help={t( - `This ${ - isPost ? "post" : "page" - } will be accessible from this time`, - )} - /> + const extraProperties = useMemo( + () => ( +
+
+ ) => { + try { + const value = date.inLocalTimezone(e.target.value).toISOString() + updateValue("publishedAt", value) + } catch (error) {} + }} + help={t( + `This ${ + isPost ? "post" : "page" + } will be accessible from this time`, + )} + /> +
+
+ ) => + updateValue("slug", e.target.value) + } + help={ + <> + {(values.slug || defaultSlug) && ( + <> + {t( + `This ${isPost ? "post" : "page"} will be accessible at`, + )}{" "} + + {getSiteLink({ + subdomain, + domain: site.data?.custom_domain, + noProtocol: true, + })} + /{encodeURIComponent(values.slug || defaultSlug)} + + + )} + + } + /> +
+
+ ) => + updateValue("tags", e.target.value) + } + help={t("Separate multiple tags with English commas") + ` ","`} + /> +
+
+ ) => { + updateValue("excerpt", e.target.value) + }} + help={t("Leave it blank to use auto-generated excerpt")} + /> +
-
- ) => - updateValue("slug", e.target.value) - } - help={ - <> - {(values.slug || defaultSlug) && ( - <> - {t(`This ${isPost ? "post" : "page"} will be accessible at`)}{" "} - - {getSiteLink({ - subdomain, - domain: site.data?.custom_domain, - noProtocol: true, - })} - /{encodeURIComponent(values.slug || defaultSlug)} - - - )} - - } - /> -
-
- ) => - updateValue("tags", e.target.value) - } - help={t("Separate multiple tags with English commas") + ` ","`} - /> -
-
- ) => { - updateValue("excerpt", e.target.value) - }} - help={t("Leave it blank to use auto-generated excerpt")} - /> -
-
+ ), + [ + date, + defaultSlug, + isPost, + site.data?.custom_domain, + subdomain, + t, + updateValue, + values.excerpt, + values.publishedAt, + values.slug, + values.tags, + ], ) useEffect(() => { @@ -495,6 +512,15 @@ export default function SubdomainEditor() { [onScroll], ) + const onPreviewButtonClick = useCallback(() => { + window.open( + `/_site/${subdomain}/preview/${draftKey.replace( + `draft-${subdomain}-`, + "", + )}`, + ) + }, [draftKey, subdomain]) + return ( <> @@ -560,15 +586,8 @@ export default function SubdomainEditor() { published={visibility !== PageVisibilityEnum.Draft} isRendering={isRendering} renderPage={setIsRendering} - propertiesWidget={ExtraProperties} - previewPage={() => { - window.open( - `/_site/${subdomain}/preview/${draftKey.replace( - `draft-${subdomain}-`, - "", - )}`, - ) - }} + propertiesWidget={extraProperties} + previewPage={onPreviewButtonClick} />
) : ( @@ -585,17 +604,7 @@ export default function SubdomainEditor() { > {t(visibility as string)} -
- {!isMobileLayout && ExtraProperties} + {!isMobileLayout && extraProperties} )}