11// Libraries
22import React , {
33 FC ,
4+ useCallback ,
45 useContext ,
56 useState ,
67 useEffect ,
78 createRef ,
89 RefObject ,
910} from 'react'
1011import { useHistory } from 'react-router-dom'
11- import { useSelector } from 'react-redux'
12+ import { useDispatch , useSelector } from 'react-redux'
1213
1314// Contexts
1415import { FlowContext } from 'src/flows/context/flow.current'
@@ -45,18 +46,27 @@ import {
4546 getNotebooksShare ,
4647 deleteNotebooksShare ,
4748 postNotebooksShare ,
49+ postNotebooksVersion ,
4850} from 'src/client/notebooksRoutes'
4951import { event } from 'src/cloud/utils/reporting'
5052import { downloadImage } from 'src/shared/utils/download'
5153import { serialize } from 'src/flows/context/flow.list'
5254import { updatePinnedItemByParam } from 'src/shared/contexts/pinneditems'
5355import { getOrg } from 'src/organizations/selectors'
54-
56+ import { notify } from 'src/shared/actions/notifications'
57+ import {
58+ publishNotebookFailed ,
59+ publishNotebookSuccessful ,
60+ } from 'src/shared/copy/notifications'
5561// Types
5662import { RemoteDataState } from 'src/types'
5763
5864// Constants
59- import { DEFAULT_PROJECT_NAME , PROJECT_NAME_PLURAL } from 'src/flows'
65+ import {
66+ DEFAULT_PROJECT_NAME ,
67+ PROJECT_NAME ,
68+ PROJECT_NAME_PLURAL ,
69+ } from 'src/flows'
6070
6171const backgroundColor = '#07070E'
6272
@@ -113,11 +123,15 @@ interface Share {
113123}
114124
115125const FlowHeader : FC = ( ) => {
126+ const dispatch = useDispatch ( )
116127 const { remove, clone} = useContext ( FlowListContext )
117128 const { flow, updateOther} = useContext ( FlowContext )
118129 const history = useHistory ( )
119130 const { id : orgID } = useSelector ( getOrg )
120131 const [ sharing , setSharing ] = useState ( false )
132+ const [ publishLoading , setPublishLoading ] = useState < RemoteDataState > (
133+ RemoteDataState . NotStarted
134+ )
121135 const [ share , setShare ] = useState < Share > ( )
122136 const [ linkLoading , setLinkLoading ] = useState ( RemoteDataState . NotStarted )
123137 const [ linkDeleting , setLinkDeleting ] = useState ( RemoteDataState . NotStarted )
@@ -133,6 +147,52 @@ const FlowHeader: FC = () => {
133147 . catch ( err => console . error ( 'failed to get notebook share' , err ) )
134148 } , [ flow . id ] )
135149
150+ const handlePublish = useCallback ( async ( ) => {
151+ if ( isFlagEnabled ( 'flowPublishLifecycle' ) ) {
152+ setPublishLoading ( RemoteDataState . Loading )
153+ try {
154+ const response = await postNotebooksVersion ( { id : flow . id } )
155+
156+ if ( response . status !== 204 ) {
157+ throw new Error ( response . data . message )
158+ }
159+
160+ dispatch ( notify ( publishNotebookSuccessful ( flow . name ) ) )
161+ setPublishLoading ( RemoteDataState . Done )
162+ } catch ( error ) {
163+ console . error ( { error} )
164+ dispatch ( notify ( publishNotebookFailed ( flow . name ) ) )
165+ setPublishLoading ( RemoteDataState . Error )
166+ }
167+ }
168+ } , [ dispatch , flow . id , flow . name ] )
169+
170+ const handleSave = useCallback (
171+ event => {
172+ if ( isFlagEnabled ( 'flowPublishLifecycle' ) ) {
173+ if ( ( event . ctrlKey || event . metaKey ) && event . key === 's' ) {
174+ // Prevent the Save dialog to open
175+ event . preventDefault ( )
176+ handlePublish ( )
177+ // Place your code here
178+ }
179+ }
180+ } ,
181+ [ handlePublish ]
182+ )
183+
184+ useEffect ( ( ) => {
185+ if ( isFlagEnabled ( 'flowPublishLifecycle' ) ) {
186+ window . addEventListener ( 'keydown' , handleSave )
187+ }
188+
189+ return ( ) => {
190+ if ( isFlagEnabled ( 'flowPublishLifecycle' ) ) {
191+ window . removeEventListener ( 'keydown' , handleSave )
192+ }
193+ }
194+ } , [ handleSave ] )
195+
136196 const handleRename = ( name : string ) => {
137197 updateOther ( { name} )
138198 try {
@@ -247,6 +307,10 @@ const FlowHeader: FC = () => {
247307 return
248308 }
249309
310+ if ( isFlagEnabled ( 'flowPublishLifecycle' ) ) {
311+ handlePublish ( )
312+ }
313+
250314 setLinkLoading ( RemoteDataState . Loading )
251315 postNotebooksShare ( {
252316 data : {
@@ -360,8 +424,21 @@ const FlowHeader: FC = () => {
360424 ? ComponentStatus . Loading
361425 : ComponentStatus . Default
362426 }
363- titleText = " Share Notebook"
427+ titleText = { ` Share ${ PROJECT_NAME } ` }
364428 />
429+ { isFlagEnabled ( 'flowPublishLifecycle' ) && (
430+ < SquareButton
431+ icon = { IconFont . Checkmark }
432+ onClick = { handlePublish }
433+ color = { ComponentColor . Primary }
434+ status = {
435+ publishLoading === RemoteDataState . Loading
436+ ? ComponentStatus . Loading
437+ : ComponentStatus . Default
438+ }
439+ titleText = { `Publish ${ PROJECT_NAME } ` }
440+ />
441+ ) }
365442 < MenuButton menuItems = { menuItems } />
366443 </ >
367444 ) }
0 commit comments