diff --git a/client/components/new-post/index.tsx b/client/components/new-post/index.tsx index a7f8349..8745937 100644 --- a/client/components/new-post/index.tsx +++ b/client/components/new-post/index.tsx @@ -23,6 +23,7 @@ import getPostPath from "@lib/get-post-path" import EditDocumentList from "@components/edit-document-list" import { ChangeEvent } from "react" import DatePicker from "react-datepicker" +import getTitleForPostCopy from "@lib/get-title-for-post-copy" const Post = ({ initialPost, @@ -52,7 +53,6 @@ const Post = ({ // the /new/from/{id} route fetches an initial post useEffect(() => { if (initialPost) { - setTitle(`Copy of ${initialPost.title}`) setDocs( initialPost.files?.map((doc) => ({ title: doc.title, @@ -60,9 +60,12 @@ const Post = ({ id: doc.id })) || emptyDoc ) + + setTitle(getTitleForPostCopy(initialPost.title)) } }, [emptyDoc, initialPost]) + const [passwordModalVisible, setPasswordModalVisible] = useState(false) const sendRequest = useCallback( diff --git a/client/lib/get-title-for-post-copy.ts b/client/lib/get-title-for-post-copy.ts new file mode 100644 index 0000000..f9b4d82 --- /dev/null +++ b/client/lib/get-title-for-post-copy.ts @@ -0,0 +1,29 @@ +const replaceLastInString = ( + string: string, + search: string, + replace: string +): string => { + const index = string.lastIndexOf(search); + if (index === -1) { + return string; + } + return string.substring(0, index) + replace + string.substring(index + search.length); +} + +const getTitleForPostCopy = ( + title: string, +) => { + const numberAtEndOfTitle = title.split(" ").pop() + if (numberAtEndOfTitle) { + const number = parseInt(numberAtEndOfTitle) + if (number) { + return replaceLastInString(title, numberAtEndOfTitle, (number + 1).toString()) + } else { + return title + " 1" + } + } else { + return title + " 1" + } +} + +export default getTitleForPostCopy \ No newline at end of file