This is an automated email from the ASF dual-hosted git repository.

likyh pushed a commit to branch main
in repository https://gitbox.apache.org/repos/asf/incubator-devlake.git


The following commit(s) were added to refs/heads/main by this push:
     new eb3806565 refactor(config-ui): the connection store (#3933)
eb3806565 is described below

commit eb3806565deacb4c789fda667bff527ef388eed7
Author: 青湛 <[email protected]>
AuthorDate: Thu Dec 15 10:59:04 2022 +0800

    refactor(config-ui): the connection store (#3933)
    
    * refactor(config-ui): the global connection store
    
    * refactor(config-ui): remove connection store from app
    
    * fix(config-ui): adjust the connection store on the create blueprint page
---
 config-ui/src/index.js                             | 13 +++----
 .../src/pages/blueprint/create/bp-context.tsx      |  6 +--
 config-ui/src/pages/blueprint/create/index.tsx     | 43 ++++++++++++----------
 .../src/pages/blueprint/create/step-one/index.tsx  |  6 +--
 .../pages/blueprint/create/step-three/index.tsx    |  6 +--
 .../src/pages/blueprint/create/step-two/index.tsx  |  6 +--
 .../store/{store.tsx => connections/context.tsx}   | 37 +++++++++++--------
 config-ui/src/store/connections/index.ts           |  2 +-
 .../{use-connections.ts => use-context-value.ts}   | 11 +++---
 config-ui/src/store/index.ts                       |  1 -
 10 files changed, 67 insertions(+), 64 deletions(-)

diff --git a/config-ui/src/index.js b/config-ui/src/index.js
index 308ec6359..93b76d0be 100644
--- a/config-ui/src/index.js
+++ b/config-ui/src/index.js
@@ -21,7 +21,6 @@ import ReactDOM from 'react-dom'
 import { BrowserRouter as Router } from 'react-router-dom'
 
 import { ErrorBoundary } from '@/components'
-import { StoreContextProvider } from '@/store'
 
 import App from './App'
 import { UIContextProvider } from '@/store/UIContext'
@@ -32,13 +31,11 @@ import './index.css'
 ReactDOM.render(
   <UIContextProvider>
     <IntegrationsContextProvider>
-      <StoreContextProvider>
-        <ErrorBoundary>
-          <Router>
-            <App />
-          </Router>
-        </ErrorBoundary>
-      </StoreContextProvider>
+      <ErrorBoundary>
+        <Router>
+          <App />
+        </Router>
+      </ErrorBoundary>
     </IntegrationsContextProvider>
   </UIContextProvider>,
   document.getElementById('app')
diff --git a/config-ui/src/pages/blueprint/create/bp-context.tsx 
b/config-ui/src/pages/blueprint/create/bp-context.tsx
index 92a6a646b..c520a2356 100644
--- a/config-ui/src/pages/blueprint/create/bp-context.tsx
+++ b/config-ui/src/pages/blueprint/create/bp-context.tsx
@@ -20,7 +20,7 @@ import React, { useState, useMemo, useContext } from 'react'
 import { useHistory } from 'react-router-dom'
 
 import type { ConnectionItemType } from '@/store'
-import { useStore, ConnectionStatusEnum } from '@/store'
+import { useConnection, ConnectionStatusEnum } from '@/store'
 import { operator } from '@/utils'
 
 import type { BPContextType } from './types'
@@ -87,7 +87,7 @@ export const BPContextProvider = ({ from, projectName, 
children }: Props) => {
 
   const history = useHistory()
 
-  const { connections } = useStore()
+  const { connections } = useConnection()
 
   const validRawPlan = (rp: string) => {
     try {
@@ -146,7 +146,7 @@ export const BPContextProvider = ({ from, projectName, 
children }: Props) => {
           return {
             plugin: connection.plugin,
             connectionId: connection.id,
-            scope: scope.map((sc: any) => ({
+            scopes: scope.map((sc: any) => ({
               id: `${sc.id}`,
               entities: sc.entities
             }))
diff --git a/config-ui/src/pages/blueprint/create/index.tsx 
b/config-ui/src/pages/blueprint/create/index.tsx
index 07d7461dd..533d01240 100644
--- a/config-ui/src/pages/blueprint/create/index.tsx
+++ b/config-ui/src/pages/blueprint/create/index.tsx
@@ -20,6 +20,7 @@ import React, { useMemo } from 'react'
 import { useParams } from 'react-router-dom'
 
 import { PageHeader } from '@/components'
+import { ConnectionContextProvider } from '@/store'
 
 import { BPContext, BPContextProvider } from './bp-context'
 
@@ -57,25 +58,27 @@ export const CreateBlueprintPage = ({ from }: Props) => {
   )
 
   return (
-    <BPContextProvider from={from} projectName={pname}>
-      <BPContext.Consumer>
-        {({ step, mode }) => (
-          <PageHeader breadcrumbs={breadcrumbs}>
-            <S.Container>
-              <WorkFlow />
-              <S.Content>
-                {step === 1 && <StepOne />}
-                {mode === ModeEnum.normal && step === 2 && <StepTwo />}
-                {step === 3 && <StepThree />}
-                {((mode === ModeEnum.normal && step === 4) ||
-                  (mode === ModeEnum.advanced && step === 2)) && <StepFour />}
-              </S.Content>
-              <Action />
-              <Inspector />
-            </S.Container>
-          </PageHeader>
-        )}
-      </BPContext.Consumer>
-    </BPContextProvider>
+    <ConnectionContextProvider>
+      <BPContextProvider from={from} projectName={pname}>
+        <BPContext.Consumer>
+          {({ step, mode }) => (
+            <PageHeader breadcrumbs={breadcrumbs}>
+              <S.Container>
+                <WorkFlow />
+                <S.Content>
+                  {step === 1 && <StepOne />}
+                  {mode === ModeEnum.normal && step === 2 && <StepTwo />}
+                  {step === 3 && <StepThree />}
+                  {((mode === ModeEnum.normal && step === 4) ||
+                    (mode === ModeEnum.advanced && step === 2)) && <StepFour 
/>}
+                </S.Content>
+                <Action />
+                <Inspector />
+              </S.Container>
+            </PageHeader>
+          )}
+        </BPContext.Consumer>
+      </BPContextProvider>
+    </ConnectionContextProvider>
   )
 }
diff --git a/config-ui/src/pages/blueprint/create/step-one/index.tsx 
b/config-ui/src/pages/blueprint/create/step-one/index.tsx
index a6f5c81cf..5976e4d5b 100644
--- a/config-ui/src/pages/blueprint/create/step-one/index.tsx
+++ b/config-ui/src/pages/blueprint/create/step-one/index.tsx
@@ -28,7 +28,7 @@ import {
 } from '@blueprintjs/core'
 import { Popover2 } from '@blueprintjs/popover2'
 
-import { useStore } from '@/store/store'
+import { useConnection } from '@/store'
 import { Card, Divider, MultiSelector, Loading } from '@/components'
 
 import { ModeEnum } from '../types'
@@ -40,7 +40,7 @@ import * as S from './styled'
 export const StepOne = () => {
   const [isOpen, setIsOpen] = useState(false)
 
-  const { connections, onTestConnections } = useStore()
+  const { connections, onTest } = useConnection()
 
   const {
     mode,
@@ -86,7 +86,7 @@ export const StepOne = () => {
                 uniqueList.includes(cs.unique)
               )}
               onChangeItems={(selectedItems) => {
-                onTestConnections(selectedItems)
+                onTest(selectedItems)
                 onChangeUniqueList(selectedItems.map((sc) => sc.unique))
               }}
             />
diff --git a/config-ui/src/pages/blueprint/create/step-three/index.tsx 
b/config-ui/src/pages/blueprint/create/step-three/index.tsx
index cac1017e6..d61470429 100644
--- a/config-ui/src/pages/blueprint/create/step-three/index.tsx
+++ b/config-ui/src/pages/blueprint/create/step-three/index.tsx
@@ -19,9 +19,9 @@
 import React, { useState, useMemo } from 'react'
 import { Icon } from '@blueprintjs/core'
 
-import type { ConnectionItemType } from '@/store'
 import { Card, Table, Divider } from '@/components'
-import { useStore } from '@/store'
+import type { ConnectionItemType } from '@/store'
+import { useConnection } from '@/store'
 import { Transformation } from '@/plugins'
 
 import { useCreateBP } from '../bp-context'
@@ -33,7 +33,7 @@ export const StepThree = () => {
     ConnectionItemType & { scope: any }
   >()
 
-  const { connections } = useStore()
+  const { connections } = useConnection()
   const { uniqueList, scopeMap, onChangeShowDetail } = useCreateBP()
 
   const handleGoDetail = (c: ConnectionItemType & { scope: any }) => {
diff --git a/config-ui/src/pages/blueprint/create/step-two/index.tsx 
b/config-ui/src/pages/blueprint/create/step-two/index.tsx
index 5247f1945..418469883 100644
--- a/config-ui/src/pages/blueprint/create/step-two/index.tsx
+++ b/config-ui/src/pages/blueprint/create/step-two/index.tsx
@@ -19,9 +19,9 @@
 import React, { useState, useMemo } from 'react'
 import { Icon } from '@blueprintjs/core'
 
-import type { ConnectionItemType } from '@/store'
 import { Card, Table, Divider } from '@/components'
-import { useStore } from '@/store'
+import type { ConnectionItemType } from '@/store'
+import { useConnection } from '@/store'
 import { DataScope } from '@/plugins'
 
 import { useCreateBP } from '../bp-context'
@@ -33,7 +33,7 @@ export const StepTwo = () => {
     ConnectionItemType & { scope: any }
   >()
 
-  const { connections } = useStore()
+  const { connections } = useConnection()
   const { uniqueList, scopeMap, onChangeScopeMap, onChangeShowDetail } =
     useCreateBP()
 
diff --git a/config-ui/src/store/store.tsx 
b/config-ui/src/store/connections/context.tsx
similarity index 60%
rename from config-ui/src/store/store.tsx
rename to config-ui/src/store/connections/context.tsx
index 14c49abe4..1e70c95c6 100644
--- a/config-ui/src/store/store.tsx
+++ b/config-ui/src/store/connections/context.tsx
@@ -19,44 +19,49 @@
 import React, { useContext } from 'react'
 
 import { PageLoading } from '@/components'
+import { Plugins } from '@/plugins'
 
-import type { ConnectionItemType } from './connections'
-import { useConnections } from './connections'
+import type { ConnectionItemType } from './types'
+import { useContextValue } from './use-context-value'
 
-export const StoreContext = React.createContext<{
+const ConnectionContext = React.createContext<{
   connections: ConnectionItemType[]
-  onRefreshConnections: () => void
-  onTestConnections: (selectedConnections: ConnectionItemType[]) => void
+  onRefresh: () => void
+  onTest: (selectedConnections: ConnectionItemType[]) => void
 }>({
   connections: [],
-  onRefreshConnections: () => {},
-  onTestConnections: () => {}
+  onRefresh: () => {},
+  onTest: () => {}
 })
 
 interface Props {
+  plugins?: Plugins[]
   children?: React.ReactNode
 }
 
-export const StoreContextProvider = ({ children }: Props) => {
-  const { loading, connections, onRefresh, onTest } = useConnections()
+export const ConnectionContextProvider = ({
+  plugins = [],
+  children
+}: Props) => {
+  const { loading, connections, onRefresh, onTest } = useContextValue(plugins)
 
   if (loading) {
     return <PageLoading />
   }
 
   return (
-    <StoreContext.Provider
+    <ConnectionContext.Provider
       value={{
         connections,
-        onRefreshConnections: onRefresh,
-        onTestConnections: onTest
+        onRefresh,
+        onTest
       }}
     >
       {children}
-    </StoreContext.Provider>
+    </ConnectionContext.Provider>
   )
 }
 
-export const useStore = () => {
-  return useContext(StoreContext)
-}
+export const ConnectionContextConsumer = ConnectionContext.Consumer
+
+export const useConnection = () => useContext(ConnectionContext)
diff --git a/config-ui/src/store/connections/index.ts 
b/config-ui/src/store/connections/index.ts
index 63e0a3414..5f25db57c 100644
--- a/config-ui/src/store/connections/index.ts
+++ b/config-ui/src/store/connections/index.ts
@@ -16,5 +16,5 @@
  *
  */
 
-export * from './use-connections'
 export * from './types'
+export * from './context'
diff --git a/config-ui/src/store/connections/use-connections.ts 
b/config-ui/src/store/connections/use-context-value.ts
similarity index 95%
rename from config-ui/src/store/connections/use-connections.ts
rename to config-ui/src/store/connections/use-context-value.ts
index 122dd26a4..7822b05e0 100644
--- a/config-ui/src/store/connections/use-connections.ts
+++ b/config-ui/src/store/connections/use-context-value.ts
@@ -24,17 +24,16 @@ import type { ConnectionItemType } from './types'
 import { ConnectionStatusEnum } from './types'
 import * as API from './api'
 
-export const useConnections = (plugin?: string | string[]) => {
+export const useContextValue = (plugins: string[]) => {
   const [loading, setLoading] = useState(false)
   const [connections, setConnections] = useState<ConnectionItemType[]>([])
 
   const allConnections = useMemo(
     () =>
-      Plugins.filter((p) => p.type === 'integration').filter((p) => {
-        if (!plugin) return true
-        return Array.isArray(plugin) ? plugin.includes(p.id) : p.id === plugin
-      }),
-    [plugin]
+      Plugins.filter((p) => p.type === 'integration').filter((p) =>
+        !plugins.length ? true : plugins.includes(p.id)
+      ),
+    [plugins]
   )
 
   const getConnection = async (plugin: string) => {
diff --git a/config-ui/src/store/index.ts b/config-ui/src/store/index.ts
index cbb28550d..839be970a 100644
--- a/config-ui/src/store/index.ts
+++ b/config-ui/src/store/index.ts
@@ -16,5 +16,4 @@
  *
  */
 
-export * from './store'
 export * from './connections'

Reply via email to