Skip to content

Commit 837015b

Browse files
authored
feat: introduction to publish lifecycle (#3769)
1 parent 4670dc4 commit 837015b

File tree

3 files changed

+92
-5
lines changed

3 files changed

+92
-5
lines changed

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@
5252
"cy": "CYPRESS_dexUrl=https://$INGRESS_HOST:$PORT_HTTPS CYPRESS_baseUrl=http://localhost:9999 cypress open",
5353
"cy:dev": "source ../monitor-ci/.env && CYPRESS_dexUrl=CLOUD CYPRESS_baseUrl=https://$INGRESS_HOST:$PORT_HTTPS cypress open --config testFiles='{cloud,shared}/**/*.*'",
5454
"cy:dev-oss": "source ../monitor-ci/.env && CYPRESS_dexUrl=OSS CYPRESS_baseUrl=https://$INGRESS_HOST:$PORT_HTTPS cypress open --config testFiles='{oss,shared}/**/*.*'",
55-
"generate": "export SHA=7227887eaec5dfcab6774209e660eb797dd9efac && export REMOTE=https://raw.githubusercontent.com/influxdata/openapi/${SHA}/ && yarn generate-meta",
55+
"generate": "export SHA=40fb34a01f631d701c87025b2da98d5a4d62f94b && export REMOTE=https://raw.githubusercontent.com/influxdata/openapi/${SHA}/ && yarn generate-meta",
5656
"generate-local": "export REMOTE=../openapi/ && yarn generate-meta",
5757
"generate-meta": "if [ -z \"${CLOUD_URL}\" ]; then yarn generate-meta-oss; else yarn generate-meta-cloud; fi",
5858
"generate-meta-oss": "yarn oss-api && yarn notebooks && yarn unity && yarn annotations-oss && yarn pinned && yarn mapsd-oss && yarn cloudPriv",

src/flows/components/header/index.tsx

Lines changed: 81 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,15 @@
11
// Libraries
22
import React, {
33
FC,
4+
useCallback,
45
useContext,
56
useState,
67
useEffect,
78
createRef,
89
RefObject,
910
} from 'react'
1011
import {useHistory} from 'react-router-dom'
11-
import {useSelector} from 'react-redux'
12+
import {useDispatch, useSelector} from 'react-redux'
1213

1314
// Contexts
1415
import {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'
4951
import {event} from 'src/cloud/utils/reporting'
5052
import {downloadImage} from 'src/shared/utils/download'
5153
import {serialize} from 'src/flows/context/flow.list'
5254
import {updatePinnedItemByParam} from 'src/shared/contexts/pinneditems'
5355
import {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
5662
import {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

6171
const backgroundColor = '#07070E'
6272

@@ -113,11 +123,15 @@ interface Share {
113123
}
114124

115125
const 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
)}

src/shared/copy/notifications.ts

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1472,3 +1472,13 @@ export const getResourcesTokensFailure = (): Notification => ({
14721472
...defaultErrorNotification,
14731473
message: 'Failed to fetch all resources for creating custom api token',
14741474
})
1475+
1476+
export const publishNotebookSuccessful = (name: string): Notification => ({
1477+
...defaultSuccessNotification,
1478+
message: `Successfully published ${name}`,
1479+
})
1480+
1481+
export const publishNotebookFailed = (name: string): Notification => ({
1482+
...defaultErrorNotification,
1483+
message: `Failed to publish ${name}`,
1484+
})

0 commit comments

Comments
 (0)