From e8131c23827886cddec344f3bb989cf698da472b Mon Sep 17 00:00:00 2001 From: Kishore <42832651+kishore03109@users.noreply.github.com> Date: Thu, 14 Mar 2024 14:33:55 +0800 Subject: [PATCH] fix(permalinks): default permalink for create --- .../PageSettingsModal/PageSettingsModal.tsx | 37 ++++++++++++++++++- 1 file changed, 35 insertions(+), 2 deletions(-) diff --git a/src/components/PageSettingsModal/PageSettingsModal.tsx b/src/components/PageSettingsModal/PageSettingsModal.tsx index 14fe987062..3da8eefd38 100644 --- a/src/components/PageSettingsModal/PageSettingsModal.tsx +++ b/src/components/PageSettingsModal/PageSettingsModal.tsx @@ -96,6 +96,7 @@ export const PageSettingsModal = ({ }: PageSettingsModalParams) => { const { siteName, fileName } = params const isTiptapEnabled = useFeatureIsOn("is-tiptap-enabled") + const isPageCreated = !fileName const existingTitlesArray = pagesData .filter((page) => page.name !== fileName) @@ -146,6 +147,24 @@ export const PageSettingsModal = ({ }) } + const getDefaultPermalink = (title: string) => { + if (!title) return "" + const titleSlug = title + .toLowerCase() + .replace(/[^a-z0-9]+/g, "-") + .replace(/^-+|-+$/g, "") + return `/${titleSlug}/` + } + + useEffect(() => { + if ( + isPageCreated && + watch("permalink") !== getDefaultPermalink(watch("title")) + ) { + setValue("permalink", getDefaultPermalink(watch("title"))) + } + }, [isPageCreated, setValue, watch]) + return ( @@ -170,7 +189,10 @@ export const PageSettingsModal = ({ /> {/* Title */} - Page title + Page title + + This appears on the top of the browser window or tab + Page URL - {`${siteUrl}${watch("permalink")}`} + {isPageCreated + ? "You can change this later in Page Settings." + : `${siteUrl}${watch("permalink")}`} + {isPageCreated && ( + + + {`${siteUrl}${watch("permalink")}`} + + + )} + {errors.permalink?.message}