Skip to content

Commit df88eba

Browse files
authored
feat: faster notebook loading (#3193)
1 parent e7066a1 commit df88eba

File tree

3 files changed

+47
-30
lines changed

3 files changed

+47
-30
lines changed

src/flows/context/flow.list.tsx

Lines changed: 17 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -30,8 +30,8 @@ import {
3030
import {incrementCloneName} from 'src/utils/naming'
3131

3232
export interface FlowListContextType extends FlowList {
33-
add: (flow?: Flow) => Promise<string>
34-
clone: (id: string) => void
33+
add: (flow?: Flow) => Promise<string | void>
34+
clone: (id: string) => Promise<string | void>
3535
update: (id: string, flow: Partial<Flow>) => void
3636
remove: (id: string) => void
3737
currentID: string | null
@@ -176,7 +176,7 @@ export const FlowListProvider: FC = ({children}) => {
176176
getAll()
177177
}, [])
178178

179-
const clone = async (id: string): Promise<string> => {
179+
const clone = async (id: string): Promise<string | void> => {
180180
if (!flows.hasOwnProperty(id)) {
181181
throw new Error(`${PROJECT_NAME} not found`)
182182
}
@@ -194,7 +194,7 @@ export const FlowListProvider: FC = ({children}) => {
194194
return await add(data)
195195
}
196196

197-
const add = async (flow?: Flow): Promise<string> => {
197+
const add = (flow?: Flow): Promise<string | void> => {
198198
let _flow
199199

200200
if (!flow) {
@@ -217,30 +217,18 @@ export const FlowListProvider: FC = ({children}) => {
217217
}
218218
}
219219

220-
const apiFlow = serialize(_flow)
221-
222-
let id: string = `local_${UUID()}`
223-
try {
224-
const flow = await createAPI(apiFlow)
225-
id = flow.id
226-
227-
_flow = hydrate(flow)
228-
} catch {
229-
dispatch(notify(notebookCreateFail()))
230-
}
231-
232-
return new Promise(resolve => {
233-
setTimeout(() => {
220+
return createAPI(serialize(_flow))
221+
.then(flow => {
234222
setFlows({
235223
...flows,
236-
[id]: _flow,
224+
[flow.id]: hydrate(flow),
237225
})
238226

239-
setCurrentID(id)
240-
241-
resolve(id)
242-
}, 200)
243-
})
227+
return flow.id
228+
})
229+
.catch(() => {
230+
dispatch(notify(notebookCreateFail()))
231+
})
244232
}
245233

246234
const update = useCallback(
@@ -307,6 +295,11 @@ export const FlowListProvider: FC = ({children}) => {
307295
)
308296

309297
const migrate = async () => {
298+
const localFlows = Object.keys(flows).filter(id => id.includes('local'))
299+
if (!localFlows.length) {
300+
return
301+
}
302+
310303
const _flows = await migrateLocalFlowsToAPI(
311304
org.id,
312305
flows,

src/flows/context/flow.query.tsx

Lines changed: 26 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,11 @@
1-
import React, {FC, useContext, useMemo, useEffect, useState} from 'react'
1+
import React, {
2+
FC,
3+
useContext,
4+
useMemo,
5+
useEffect,
6+
useState,
7+
useRef,
8+
} from 'react'
29
import {useDispatch, useSelector} from 'react-redux'
310
import {getVariables} from 'src/variables/selectors'
411
import {FlowContext} from 'src/flows/context/flow.current'
@@ -141,7 +148,8 @@ export const FlowQueryProvider: FC = ({children}) => {
141148
}, {})
142149
}, [variables, flow?.range])
143150

144-
const generateMap = (): Stage[] => {
151+
const _map = useRef([])
152+
const _generateMap = (): Stage[] => {
145153
const stages = (flow?.data?.allIDs ?? []).reduce((acc, panelID) => {
146154
const panel = flow.data.byID[panelID]
147155

@@ -196,9 +204,25 @@ export const FlowQueryProvider: FC = ({children}) => {
196204
return acc
197205
}, [])
198206

207+
_map.current = stages
199208
return stages
200209
}
201210

211+
useEffect(() => {
212+
_generateMap()
213+
}, [flow])
214+
215+
const generateMap = (): Stage[] => {
216+
if (
217+
!_map.current ||
218+
(!_map.current.length && (flow?.data?.allIDs ?? []).length)
219+
) {
220+
_generateMap()
221+
}
222+
223+
return _map.current
224+
}
225+
202226
// TODO figure out a better way to cache these requests
203227
const getPanelQueries = (id: string): Stage | null => {
204228
if (!id) {

src/flows/pipes/QueryBuilder/context.tsx

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -251,9 +251,9 @@ export const QueryBuilderProvider: FC = ({children}) => {
251251
scope
252252
)
253253
.then(resp => {
254-
return Object.values(resp.parsed.table.columns).filter(
254+
return (Object.values(resp.parsed.table.columns).filter(
255255
c => c.name === '_value' && c.type === 'string'
256-
)[0].data as string[]
256+
)[0]?.data ?? []) as string[]
257257
})
258258
.then(keys => {
259259
if (!cards[idx].keys.selected[0]) {
@@ -343,9 +343,9 @@ export const QueryBuilderProvider: FC = ({children}) => {
343343
scope
344344
)
345345
.then(resp => {
346-
return Object.values(resp.parsed.table.columns).filter(
346+
return (Object.values(resp.parsed.table.columns).filter(
347347
c => c.name === '_value' && c.type === 'string'
348-
)[0].data as string[]
348+
)[0]?.data ?? []) as string[]
349349
})
350350
.then(values => {
351351
cardMeta.splice(idx, 1, {

0 commit comments

Comments
 (0)