@@ -22,16 +22,23 @@ import { useMarkdownHotkeys } from "@/markdoc/editor/hotkeys/hotkeys.markdoc";
2222import { useMarkdownShortcuts } from "@/markdoc/editor/shortcuts/shortcuts.markdoc" ;
2323import { markdocComponents } from "@/markdoc/components" ;
2424import { config } from "@/markdoc/config" ;
25- import { useParams , useRouter } from "next/navigation" ;
25+ import { notFound , useParams , useRouter } from "next/navigation" ;
2626import { usePrompt } from "@/components/PromptService" ;
2727import { Switch } from "@/components/Switch/Switch" ;
2828import copy from "copy-to-clipboard" ;
29- import { PostStatus , getPostStatus , isValidScheduleTime } from "@/utils/post" ;
29+ import {
30+ type PostStatus ,
31+ getPostStatus ,
32+ isValidScheduleTime ,
33+ status ,
34+ } from "@/utils/post" ;
3035import { ImageUp , LoaderCircle } from "lucide-react" ;
3136import { uploadFile } from "@/utils/s3helpers" ;
3237import { getUploadUrl } from "@/app/actions/getUploadUrl" ;
38+ import EditorNav from "./navigation" ;
39+ import { type Session } from "next-auth" ;
3340
34- const Create = ( ) => {
41+ const Create = ( { session } : { session : Session | null } ) => {
3542 const params = useParams ( ) ;
3643 const router = useRouter ( ) ;
3744
@@ -52,6 +59,7 @@ const Create = () => {
5259 const [ uploadStatus , setUploadStatus ] = useState <
5360 "loading" | "error" | "success" | "default"
5461 > ( "default" ) ;
62+ const [ postStatus , setPostStatus ] = useState < PostStatus | null > ( null ) ;
5563
5664 const { unsavedChanges : _unsaved , setUnsavedChanges : _setUnsaved } =
5765 usePrompt ( ) ;
@@ -186,16 +194,10 @@ const Create = () => {
186194 toast . error ( "Error saving" ) ;
187195 }
188196
189- if ( isSuccess ) {
190- toast . success ( "Saved" ) ;
191- }
192-
193197 if ( draftFetchError ) {
194- toast . error (
195- "Something went wrong fetching your draft, refresh your page or you may lose data" ,
196- ) ;
198+ notFound ( ) ;
197199 }
198- } , [ draftFetchError , isError , isSuccess ] ) ;
200+ } , [ draftFetchError , isError ] ) ;
199201
200202 useEffect ( ( ) => {
201203 if ( shouldRefetch ) {
@@ -227,7 +229,6 @@ const Create = () => {
227229 await create ( { ...formData } ) ;
228230 } else {
229231 await save ( { ...formData , id : postId } ) ;
230- toast . success ( "Saved" ) ;
231232 setSavedTime (
232233 new Date ( ) . toLocaleString ( undefined , {
233234 dateStyle : "medium" ,
@@ -243,9 +244,9 @@ const Create = () => {
243244 saveStatus === "loading" ||
244245 dataStatus === "loading" ;
245246
246- const postStatus = data ?. published
247+ const currentPostStatus = data ?. published
247248 ? getPostStatus ( new Date ( data . published ) )
248- : PostStatus . DRAFT ;
249+ : status . DRAFT ;
249250
250251 const onSubmit = async ( inputData : SavePostInput ) => {
251252 // validate markdoc syntax
@@ -264,7 +265,7 @@ const Create = () => {
264265
265266 await savePost ( ) ;
266267
267- if ( postStatus === PostStatus . PUBLISHED ) {
268+ if ( currentPostStatus === status . PUBLISHED ) {
268269 if ( data ) {
269270 router . push ( `/articles/${ data . slug } ` ) ;
270271 }
@@ -341,10 +342,21 @@ const Create = () => {
341342 published : published ? published : undefined ,
342343 } ) ;
343344 setIsPostScheduled ( published ? new Date ( published ) > new Date ( ) : false ) ;
345+ setPostStatus (
346+ published ? getPostStatus ( new Date ( published ) ) : status . DRAFT ,
347+ ) ;
344348 } , [ data ] ) ;
345349
346350 useEffect ( ( ) => {
347- if ( postStatus !== PostStatus . DRAFT ) return ;
351+ if ( ( title + body ) . length < 5 ) {
352+ setPostStatus ( null ) ;
353+ } else if ( postStatus === null ) {
354+ setPostStatus ( status . DRAFT ) ;
355+ }
356+ } , [ title , body ] ) ;
357+
358+ useEffect ( ( ) => {
359+ if ( currentPostStatus !== status . DRAFT ) return ;
348360 if ( ( title + body ) . length < 5 ) return ;
349361 if ( debouncedValue === ( data ?. title || "" ) + data ?. body ) return ;
350362 if ( allowUpdate ) savePost ( ) ;
@@ -374,8 +386,21 @@ const Create = () => {
374386 }
375387 } , [ publishStatus , publishData , isPostScheduled , router ] ) ;
376388
389+ const handlePublish = ( ) => {
390+ if ( isDisabled ) return ;
391+ setOpen ( true ) ;
392+ } ;
393+
377394 return (
378395 < >
396+ < EditorNav
397+ session = { session }
398+ username = { session ?. user ?. name || null }
399+ postStatus = { postStatus }
400+ unsavedChanges = { unsavedChanges }
401+ onPublish = { handlePublish }
402+ isDisabled = { isDisabled }
403+ />
379404 < form onSubmit = { handleSubmit ( onSubmit ) } >
380405 < Transition show = { open } as = { Fragment } >
381406 < div className = "old-input fixed bottom-0 left-0 top-0 z-50 h-screen w-full overflow-y-scroll bg-white dark:bg-black" >
@@ -549,7 +574,7 @@ const Create = () => {
549574 </ Disclosure >
550575 </ div >
551576 < div className = "mt-4 flex w-full justify-end sm:col-span-12 sm:mt-0" >
552- { postStatus === PostStatus . DRAFT && (
577+ { currentPostStatus === status . DRAFT && (
553578 < button
554579 type = "button"
555580 disabled = { isDisabled }
@@ -578,14 +603,17 @@ const Create = () => {
578603 </ >
579604 ) : (
580605 < >
581- { postStatus === PostStatus . DRAFT &&
582- ( isPostScheduled ? "Schedule" : "Publish now" ) }
583- { postStatus === PostStatus . SCHEDULED &&
584- ( isPostScheduled
585- ? "Save changes"
586- : "Publish now" ) }
587- { postStatus === PostStatus . PUBLISHED &&
588- "Save changes" }
606+ { currentPostStatus === status . PUBLISHED
607+ ? "Save changes"
608+ : currentPostStatus === status . DRAFT
609+ ? isPostScheduled
610+ ? "Schedule"
611+ : "Publish now"
612+ : currentPostStatus === status . SCHEDULED
613+ ? isPostScheduled
614+ ? "Update schedule"
615+ : "Publish now"
616+ : "Publish" }
589617 </ >
590618 ) }
591619 </ button >
@@ -743,36 +771,6 @@ const Create = () => {
743771 inputRef = { textareaRef }
744772 />
745773 </ div >
746-
747- < div className = "flex items-center justify-between" >
748- < >
749- { saveStatus === "loading" && (
750- < p > Auto-saving...</ p >
751- ) }
752- { saveStatus === "error" && savedTime && (
753- < p className = "text-xs text-red-600 lg:text-sm" >
754- { `Error saving, last saved: ${ savedTime . toString ( ) } ` }
755- </ p >
756- ) }
757- { saveStatus === "success" && savedTime && (
758- < p className = "text-xs text-neutral-400 lg:text-sm" >
759- { `Saved: ${ savedTime . toString ( ) } ` }
760- </ p >
761- ) }
762- </ >
763- < div />
764-
765- < div className = "flex" >
766- < button
767- type = "button"
768- disabled = { isDisabled }
769- className = "ml-5 inline-flex justify-center bg-gradient-to-r from-orange-400 to-pink-600 px-4 py-2 text-sm font-medium text-white shadow-sm hover:from-orange-300 hover:to-pink-500 focus:outline-none focus:ring-2 focus:ring-pink-300 focus:ring-offset-2 disabled:opacity-50"
770- onClick = { ( ) => setOpen ( true ) }
771- >
772- Next
773- </ button >
774- </ div >
775- </ div >
776774 </ div >
777775 ) }
778776 </ div >
0 commit comments