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 bf8a199c3 feat(config-ui): support webhook in the project detail page 
(#3929)
bf8a199c3 is described below

commit bf8a199c354eebd766b7bc871b25a204249c0fc0
Author: 青湛 <[email protected]>
AuthorDate: Wed Dec 14 21:43:46 2022 +0800

    feat(config-ui): support webhook in the project detail page (#3929)
    
    * chore(config-ui): remove console.log from use-transformation.ts
    
    * fix(config-ui): missed key in sync-policy
    
    * refactor(config-ui): about the webhook connection
    
    * feat(config-ui): add miller-columns for webhook
    
    * feat(config-ui): add webhook-selector-dialog for webhook plugin
    
    * feat(config-ui): support the webhook type in plugins
    
    * feat(config-ui): add webhook support in the project detail page
    
    * fix(config-ui): scopes may not exist
---
 config-ui/src/hooks/useDataScopesManager.jsx       | 12 ++--
 .../src/pages/blueprints/blueprint-detail.jsx      |  2 +-
 config-ui/src/pages/project/detail/api.ts          |  3 +
 config-ui/src/pages/project/detail/index.tsx       | 66 +++++++++--------
 .../src/pages/project/detail/panel/blueprint.tsx   | 15 ++--
 .../project/detail/panel/incoming-webhooks.tsx     | 70 +++++++++++++++---
 .../src/pages/project/detail/panel/settings.tsx    |  7 +-
 config-ui/src/pages/project/detail/styled.ts       | 47 ++----------
 config-ui/src/pages/project/detail/types.ts        |  2 +-
 config-ui/src/pages/project/detail/use-project.ts  | 74 +++++++++++++++++--
 config-ui/src/plugins/common/sync-policy/index.tsx |  2 +-
 .../common/transformation/use-transformation.ts    |  2 -
 config-ui/src/plugins/common/types.ts              |  3 +-
 .../src/plugins/webook/{connection => }/api.ts     |  0
 config-ui/src/plugins/webook/components/index.ts   |  4 +-
 .../webook/components/miller-columns/index.tsx     | 66 +++++++++++++++++
 .../miller-columns/use-miller-columns.ts}          | 55 ++++++++------
 config-ui/src/plugins/webook/connection/index.tsx  | 50 +++++++------
 .../plugins/webook/connection/use-connection.ts    | 60 +++-------------
 .../{components => }/create-dialog/index.tsx       | 43 ++++-------
 .../{components => }/create-dialog/styled.ts       |  0
 .../src/plugins/webook/create-dialog/use-create.ts | 78 ++++++++++++++++++++
 .../{components => }/delete-dialog/index.tsx       | 21 +++---
 .../{components => }/delete-dialog/styled.ts       |  0
 .../styled.ts => delete-dialog/use-delete.ts}      | 64 +++++++++--------
 config-ui/src/plugins/webook/index.ts              |  5 ++
 .../delete-dialog => selector-dialog}/index.tsx    | 41 ++++++-----
 .../delete-dialog => selector-dialog}/styled.ts    | 14 +++-
 .../src/plugins/webook/{index.ts => types.ts}      |  5 +-
 .../{components => }/view-or-edit-dialog/index.tsx | 49 ++++---------
 .../{components => }/view-or-edit-dialog/styled.ts |  0
 .../webook/view-or-edit-dialog/use-view-or-edit.ts | 84 ++++++++++++++++++++++
 32 files changed, 615 insertions(+), 329 deletions(-)

diff --git a/config-ui/src/hooks/useDataScopesManager.jsx 
b/config-ui/src/hooks/useDataScopesManager.jsx
index 2b11384d5..812a232b5 100644
--- a/config-ui/src/hooks/useDataScopesManager.jsx
+++ b/config-ui/src/hooks/useDataScopesManager.jsx
@@ -224,7 +224,7 @@ function useDataScopesManager({
 
   const getGithubProjects = useCallback(
     (c) =>
-      c.scopes.map(
+      c.scopes?.map(
         (s) =>
           new GitHubProject({
             id: `${s.options?.owner}/${s.options?.repo}`,
@@ -240,7 +240,7 @@ function useDataScopesManager({
 
   const getGitlabProjects = useCallback(
     (c) =>
-      c.scopes.map(
+      c.scopes?.map(
         (s) =>
           new GitlabProject({
             id: s.options?.projectId,
@@ -271,7 +271,7 @@ function useDataScopesManager({
 
   const getJiraBoard = useCallback(
     (c) =>
-      c.scopes.map(
+      c.scopes?.map(
         (s) =>
           new JiraBoard({
             id: s.options?.boardId,
@@ -423,12 +423,12 @@ function useDataScopesManager({
         `${ProviderLabels[c.plugin?.toUpperCase()]} #${c.connectionId || 
cIdx}`,
       // FIXME: entities in `c.scopes[0]?.entities` means one of 
ALL_DATA_DOMAINS and is saved in db,
       // So it kept here.
-      dataDomains: c.scopes[0]?.entities?.map((e) =>
+      dataDomains: c.scopes?.[0].entities?.map((e) =>
         ALL_DATA_DOMAINS.find((de) => de.value === e)
       ),
       scopeEntities: getProjects(c),
-      transformations: c.scopes.map((s) => ({ ...s.transformation })),
-      transformationStates: c.scopes.map((s) =>
+      transformations: c.scopes?.map((s) => ({ ...s.transformation })),
+      transformationStates: c.scopes?.map((s) =>
         Object.values(s.transformation ?? {}).some((v) =>
           Array.isArray(v)
             ? v.length > 0
diff --git a/config-ui/src/pages/blueprints/blueprint-detail.jsx 
b/config-ui/src/pages/blueprints/blueprint-detail.jsx
index 4eabe1162..bf7a4b421 100644
--- a/config-ui/src/pages/blueprints/blueprint-detail.jsx
+++ b/config-ui/src/pages/blueprints/blueprint-detail.jsx
@@ -267,7 +267,7 @@ const BlueprintDetail = ({ id }) => {
             ProviderLabels[connection?.plugin.toUpperCase()]
           } Connection (ID #${connection?.connectionId})`,
           dataScope: connection?.scopes
-            .map((s) => [`${s.options?.owner}/${s?.options?.repo}`])
+            ?.map((s) => [`${s.options?.owner}/${s?.options?.repo}`])
             .join(', '),
           dataDomains: []
         }))
diff --git a/config-ui/src/pages/project/detail/api.ts 
b/config-ui/src/pages/project/detail/api.ts
index 151bf28f6..525c58b42 100644
--- a/config-ui/src/pages/project/detail/api.ts
+++ b/config-ui/src/pages/project/detail/api.ts
@@ -35,3 +35,6 @@ export const updateProject = (name: string, payload: 
UpdateProjectPayload) =>
     method: 'patch',
     data: payload
   })
+
+export const updateBlueprint = (id: ID, payload: any) =>
+  request(`/blueprints/${id}`, { method: 'patch', data: payload })
diff --git a/config-ui/src/pages/project/detail/index.tsx 
b/config-ui/src/pages/project/detail/index.tsx
index cf990397e..a3a5aed01 100644
--- a/config-ui/src/pages/project/detail/index.tsx
+++ b/config-ui/src/pages/project/detail/index.tsx
@@ -16,8 +16,9 @@
  *
  */
 
-import React, { useState } from 'react'
+import React from 'react'
 import { useParams } from 'react-router-dom'
+import { Tabs, Tab } from '@blueprintjs/core'
 
 import { PageHeader, PageLoading } from '@/components'
 
@@ -28,11 +29,16 @@ import * as S from './styled'
 export const ProjectDetailPage = () => {
   const { pname } = useParams<{ pname: string }>()
 
-  const [activeTab, setActiveTab] = useState<'bp' | 'iw' | 'st'>('bp')
+  const {
+    loading,
+    project,
+    saving,
+    onUpdate,
+    onSelectWebhook,
+    onCreateWebhook
+  } = useProject(pname)
 
-  const { loading, project, updateProject } = useProject(pname)
-
-  if (loading) {
+  if (loading || !project) {
     return <PageLoading />
   }
 
@@ -43,31 +49,33 @@ export const ProjectDetailPage = () => {
         { name: pname, path: `/projects/${pname}` }
       ]}
     >
-      <S.Tabs>
-        <S.Tab active={activeTab === 'bp'} onClick={() => setActiveTab('bp')}>
-          Blueprint
-        </S.Tab>
-        <S.Tab
-          active={activeTab === 'iw'}
-          disabled={!project?.blueprint}
-          onClick={() => {
-            if (!project?.blueprint) {
-              return
+      <S.Wrapper>
+        <Tabs>
+          <Tab
+            id='bp'
+            title='Blueprint'
+            panel={<BlueprintPanel project={project} />}
+          />
+          <Tab
+            id='iw'
+            title='Incoming Webhooks'
+            disabled={!project.blueprint}
+            panel={
+              <IncomingWebhooksPanel
+                project={project}
+                saving={saving}
+                onSelectWebhook={onSelectWebhook}
+                onCreateWebhook={onCreateWebhook}
+              />
             }
-            setActiveTab('iw')
-          }}
-        >
-          Incoming Webhooks
-        </S.Tab>
-        <S.Tab active={activeTab === 'st'} onClick={() => setActiveTab('st')}>
-          Settings
-        </S.Tab>
-      </S.Tabs>
-      {activeTab === 'bp' && <BlueprintPanel name={pname} project={project} />}
-      {activeTab === 'iw' && <IncomingWebhooksPanel project={project} />}
-      {activeTab === 'st' && (
-        <SettingsPanel project={project} onUpdate={updateProject} />
-      )}
+          />
+          <Tab
+            id='st'
+            title='Settings'
+            panel={<SettingsPanel project={project} onUpdate={onUpdate} />}
+          />
+        </Tabs>
+      </S.Wrapper>
     </PageHeader>
   )
 }
diff --git a/config-ui/src/pages/project/detail/panel/blueprint.tsx 
b/config-ui/src/pages/project/detail/panel/blueprint.tsx
index fae9e5235..0dbd94417 100644
--- a/config-ui/src/pages/project/detail/panel/blueprint.tsx
+++ b/config-ui/src/pages/project/detail/panel/blueprint.tsx
@@ -21,24 +21,23 @@ import { useHistory } from 'react-router-dom'
 import { Button, Intent } from '@blueprintjs/core'
 
 import NoData from '@/images/no-data.svg'
+import { Card } from '@/components'
 import { BlueprintDetail } from '@/pages'
 
 import type { ProjectType } from '../types'
-import * as S from '../styled'
 
 interface Props {
-  name: string
-  project?: ProjectType
+  project: ProjectType
 }
 
-export const BlueprintPanel = ({ name, project }: Props) => {
+export const BlueprintPanel = ({ project }: Props) => {
   const history = useHistory()
 
   const handleGoCreateBlueprint = () =>
-    history.push(`/projects/${name}/create-blueprint`)
+    history.push(`/projects/${project.name}/create-blueprint`)
 
-  return !project?.blueprint ? (
-    <S.Panel>
+  return !project.blueprint ? (
+    <Card>
       <div className='blueprint'>
         <div className='logo'>
           <img src={NoData} alt='' />
@@ -55,7 +54,7 @@ export const BlueprintPanel = ({ name, project }: Props) => {
           />
         </div>
       </div>
-    </S.Panel>
+    </Card>
   ) : (
     <BlueprintDetail id={project.blueprint.id} />
   )
diff --git a/config-ui/src/pages/project/detail/panel/incoming-webhooks.tsx 
b/config-ui/src/pages/project/detail/panel/incoming-webhooks.tsx
index d50b8c833..34b42191a 100644
--- a/config-ui/src/pages/project/detail/panel/incoming-webhooks.tsx
+++ b/config-ui/src/pages/project/detail/panel/incoming-webhooks.tsx
@@ -16,21 +16,52 @@
  *
  */
 
-import React from 'react'
+import React, { useState, useMemo } from 'react'
 import { Button, Intent } from '@blueprintjs/core'
 
 import NoData from '@/images/no-webhook.svg'
+import { Card } from '@/components'
+import type { WebhookItemType } from '@/plugins'
+import {
+  Plugins,
+  WebhookCreateDialog,
+  WebhookSelectorDialog,
+  WebHookConnection
+} from '@/plugins'
 
 import type { ProjectType } from '../types'
-import * as S from '../styled'
 
 interface Props {
-  project?: ProjectType
+  project: ProjectType
+  saving: boolean
+  onSelectWebhook: (items: WebhookItemType[]) => void
+  onCreateWebhook: (id: ID) => any
 }
 
-export const IncomingWebhooksPanel = ({ project }: Props) => {
-  return (
-    <S.Panel>
+export const IncomingWebhooksPanel = ({
+  project,
+  saving,
+  onSelectWebhook,
+  onCreateWebhook
+}: Props) => {
+  const [type, setType] = useState<'selectExist' | 'create'>()
+
+  const webhookIds = useMemo(
+    () =>
+      project.blueprint
+        ? project.blueprint.settings.connections
+            .filter((cs: any) => cs.plugin === Plugins.Webhook)
+            .map((cs: any) => cs.connectionId)
+        : [],
+    [project]
+  )
+
+  const handleCancel = () => {
+    setType(undefined)
+  }
+
+  return !webhookIds.length ? (
+    <Card>
       <div className='webhook'>
         <div className='logo'>
           <img src={NoData} alt='' />
@@ -42,15 +73,38 @@ export const IncomingWebhooksPanel = ({ project }: Props) 
=> {
           </p>
         </div>
         <div className='action'>
-          <Button intent={Intent.PRIMARY} icon='plus' text='Add a Webhook' />
+          <Button
+            intent={Intent.PRIMARY}
+            icon='plus'
+            text='Add a Webhook'
+            onClick={() => setType('create')}
+          />
           <span className='or'>or</span>
           <Button
             outlined
             intent={Intent.PRIMARY}
             text='Select Existing Webhooks'
+            onClick={() => setType('selectExist')}
           />
         </div>
       </div>
-    </S.Panel>
+      {type === 'create' && (
+        <WebhookCreateDialog
+          isOpen
+          onCancel={handleCancel}
+          onSubmitAfter={onCreateWebhook}
+        />
+      )}
+      {type === 'selectExist' && (
+        <WebhookSelectorDialog
+          isOpen
+          saving={saving}
+          onCancel={handleCancel}
+          onSubmit={onSelectWebhook}
+        />
+      )}
+    </Card>
+  ) : (
+    <WebHookConnection filterIds={webhookIds} onCreateAfter={onCreateWebhook} 
/>
   )
 }
diff --git a/config-ui/src/pages/project/detail/panel/settings.tsx 
b/config-ui/src/pages/project/detail/panel/settings.tsx
index ef1751566..0725a4747 100644
--- a/config-ui/src/pages/project/detail/panel/settings.tsx
+++ b/config-ui/src/pages/project/detail/panel/settings.tsx
@@ -25,8 +25,9 @@ import {
   Intent
 } from '@blueprintjs/core'
 
+import { Card } from '@/components'
+
 import type { ProjectType } from '../types'
-import * as S from '../styled'
 
 interface Props {
   project: ProjectType
@@ -47,7 +48,7 @@ export const SettingsPanel = ({ project, onUpdate }: Props) 
=> {
   const handleSave = () => onUpdate(name, enableDora)
 
   return (
-    <S.Panel>
+    <Card>
       <div className='settings'>
         <div className='block'>
           <h3>Project Name *</h3>
@@ -71,6 +72,6 @@ export const SettingsPanel = ({ project, onUpdate }: Props) 
=> {
           <Button text='Save' intent={Intent.PRIMARY} onClick={handleSave} />
         </ButtonGroup>
       </div>
-    </S.Panel>
+    </Card>
   )
 }
diff --git a/config-ui/src/pages/project/detail/styled.ts 
b/config-ui/src/pages/project/detail/styled.ts
index 2f2eaba0c..e85a14e9c 100644
--- a/config-ui/src/pages/project/detail/styled.ts
+++ b/config-ui/src/pages/project/detail/styled.ts
@@ -18,50 +18,11 @@
 
 import styled from '@emotion/styled'
 
-export const Tabs = styled.ul`
-  margin: 0;
-  padding: 0;
-  list-style: none;
-  display: flex;
-  align-items: center;
-  justify-content: center;
-`
-
-export const Tab = styled.li<{ active: boolean; disabled?: boolean }>`
-  margin-right: 24px;
-  padding: 6px 0;
-  font-size: 14px;
-  border-bottom: 2px solid transparent;
-  transition: all 0.3s ease;
-  cursor: pointer;
-
-  ${({ active }) =>
-    active
-      ? `
-    color: #7497F7;
-    border-color: #7497F7;
-  `
-      : ''}
-
-  ${({ disabled }) =>
-    disabled
-      ? `
-  color: #a1a1a1;
-    cursor: no-drop;
-  `
-      : ''}
-
-  &:last-child {
-    margin-right: 0;
+export const Wrapper = styled.div`
+  & > .bp4-tabs > .bp4-tab-list {
+    display: flex;
+    justify-content: center;
   }
-`
-
-export const Panel = styled.div`
-  margin-top: 24px;
-  background-color: #ffffff;
-  box-shadow: 0px 2.4px 4.8px -0.8px rgba(0, 0, 0, 0.1),
-    0px 1.6px 8px rgba(0, 0, 0, 0.07);
-  border-radius: 4px;
 
   .blueprint,
   .webhook {
diff --git a/config-ui/src/pages/project/detail/types.ts 
b/config-ui/src/pages/project/detail/types.ts
index 747c0b888..50dc20e7a 100644
--- a/config-ui/src/pages/project/detail/types.ts
+++ b/config-ui/src/pages/project/detail/types.ts
@@ -21,4 +21,4 @@ export type ProjectType = {
   description: string
   blueprint: any | null
   enableDora: boolean
-} | null
+}
diff --git a/config-ui/src/pages/project/detail/use-project.ts 
b/config-ui/src/pages/project/detail/use-project.ts
index 0ea20986c..7ab5d6af0 100644
--- a/config-ui/src/pages/project/detail/use-project.ts
+++ b/config-ui/src/pages/project/detail/use-project.ts
@@ -19,6 +19,8 @@
 import { useState, useEffect, useMemo } from 'react'
 import { useHistory } from 'react-router-dom'
 
+import type { WebhookItemType } from '@/plugins'
+import { Plugins } from '@/plugins'
 import { operator } from '@/utils'
 
 import type { ProjectType } from './types'
@@ -26,11 +28,12 @@ import * as API from './api'
 
 export const useProject = (name: string) => {
   const [loading, setLoading] = useState(false)
-  const [project, setProject] = useState<ProjectType>(null)
+  const [project, setProject] = useState<ProjectType>()
+  const [saving, setSaving] = useState(false)
 
   const history = useHistory()
 
-  const getProject = async (name: string) => {
+  const getProject = async () => {
     setLoading(true)
     try {
       const res = await API.getProject(name)
@@ -50,10 +53,10 @@ export const useProject = (name: string) => {
   }
 
   useEffect(() => {
-    getProject(name)
-  }, [name])
+    getProject()
+  }, [])
 
-  const updateProject = async (newName: string, enableDora: boolean) => {
+  const handleUpdate = async (newName: string, enableDora: boolean) => {
     const payload = {
       name: newName,
       description: '',
@@ -73,12 +76,69 @@ export const useProject = (name: string) => {
     }
   }
 
+  const handleSelectWebhook = async (items: WebhookItemType[]) => {
+    const payload = {
+      ...project?.blueprint,
+      settings: {
+        version: '2.0.0',
+        connections: [
+          ...project?.blueprint.settings.connections,
+          ...items.map((it) => ({
+            plugin: Plugins.Webhook,
+            connectionId: it.id
+          }))
+        ]
+      }
+    }
+
+    const [success] = await operator(
+      () => API.updateBlueprint(project?.blueprint.id, payload),
+      {
+        setOperating: setSaving
+      }
+    )
+
+    if (success) {
+      getProject()
+    }
+  }
+
+  const handleCreateWebhook = async (id: ID) => {
+    const payload = {
+      ...project?.blueprint,
+      settings: {
+        version: '2.0.0',
+        connections: [
+          ...project?.blueprint.settings.connections,
+          {
+            plugin: Plugins.Webhook,
+            connectionId: id
+          }
+        ]
+      }
+    }
+
+    const [success] = await operator(
+      () => API.updateBlueprint(project?.blueprint.id, payload),
+      {
+        setOperating: setSaving
+      }
+    )
+
+    if (success) {
+      getProject()
+    }
+  }
+
   return useMemo(
     () => ({
       loading,
       project,
-      updateProject
+      saving,
+      onUpdate: handleUpdate,
+      onSelectWebhook: handleSelectWebhook,
+      onCreateWebhook: handleCreateWebhook
     }),
-    [loading, project]
+    [loading, project, saving]
   )
 }
diff --git a/config-ui/src/plugins/common/sync-policy/index.tsx 
b/config-ui/src/plugins/common/sync-policy/index.tsx
index bb37ad642..3e8cda5cb 100644
--- a/config-ui/src/plugins/common/sync-policy/index.tsx
+++ b/config-ui/src/plugins/common/sync-policy/index.tsx
@@ -94,7 +94,7 @@ export const SyncPolicy = ({
         <p style={{ margin: '10px 0' }}>{cron.description}</p>
         <RadioGroup selectedValue={cron.value} 
onChange={handleChangeFrequency}>
           {options.map(({ label, value }) => (
-            <Radio label={label} value={value} />
+            <Radio key={value} label={label} value={value} />
           ))}
         </RadioGroup>
         {cron.value === 'custom' && (
diff --git a/config-ui/src/plugins/common/transformation/use-transformation.ts 
b/config-ui/src/plugins/common/transformation/use-transformation.ts
index 443a39ae0..9b04dd9f7 100644
--- a/config-ui/src/plugins/common/transformation/use-transformation.ts
+++ b/config-ui/src/plugins/common/transformation/use-transformation.ts
@@ -88,8 +88,6 @@ export const useTransformation = ({
       transformationRuleId: tid
     }))
 
-    console.log(payload)
-
     const [success] = await operator(() =>
       API.updateDataScope(plugin, connectionId, {
         data: payload
diff --git a/config-ui/src/plugins/common/types.ts 
b/config-ui/src/plugins/common/types.ts
index f14a70836..b4a97122c 100644
--- a/config-ui/src/plugins/common/types.ts
+++ b/config-ui/src/plugins/common/types.ts
@@ -20,5 +20,6 @@ export enum Plugins {
   GitHub = 'github',
   JIRA = 'jira',
   GitLab = 'gitlab',
-  Jenkins = 'jenkins'
+  Jenkins = 'jenkins',
+  Webhook = 'webhook'
 }
diff --git a/config-ui/src/plugins/webook/connection/api.ts 
b/config-ui/src/plugins/webook/api.ts
similarity index 100%
copy from config-ui/src/plugins/webook/connection/api.ts
copy to config-ui/src/plugins/webook/api.ts
diff --git a/config-ui/src/plugins/webook/components/index.ts 
b/config-ui/src/plugins/webook/components/index.ts
index 83796a8aa..1c06557ce 100644
--- a/config-ui/src/plugins/webook/components/index.ts
+++ b/config-ui/src/plugins/webook/components/index.ts
@@ -16,6 +16,4 @@
  *
  */
 
-export * from './create-dialog'
-export * from './delete-dialog'
-export * from './view-or-edit-dialog'
+export * from './miller-columns'
diff --git a/config-ui/src/plugins/webook/components/miller-columns/index.tsx 
b/config-ui/src/plugins/webook/components/miller-columns/index.tsx
new file mode 100644
index 000000000..10e71617f
--- /dev/null
+++ b/config-ui/src/plugins/webook/components/miller-columns/index.tsx
@@ -0,0 +1,66 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ *
+ */
+
+import React, { useState, useEffect } from 'react'
+import MillerColumnsSelect from 'miller-columns-select'
+
+import { Loading } from '@/components'
+
+import { useMillerColumns } from './use-miller-columns'
+
+interface Props {
+  selectedItems: any[]
+  onChangeItems: (selectedItems: any[]) => void
+}
+
+export const MillerColumns = ({ selectedItems, onChangeItems }: Props) => {
+  const [seletedIds, setSelectedIds] = useState<ID[]>([])
+
+  const { items, getHasMore } = useMillerColumns()
+
+  useEffect(() => {
+    setSelectedIds(selectedItems.map((it) => it.boardId))
+  }, [])
+
+  useEffect(() => {
+    onChangeItems(
+      items
+        .filter((it) => seletedIds.includes(it.id))
+        .map((it) => ({
+          id: it.id,
+          name: it.name
+        }))
+    )
+  }, [seletedIds])
+
+  const renderLoading = () => {
+    return <Loading size={20} style={{ padding: '4px 12px' }} />
+  }
+
+  return (
+    <MillerColumnsSelect
+      columnCount={1}
+      columnHeight={160}
+      getHasMore={getHasMore}
+      renderLoading={renderLoading}
+      items={items}
+      selectedIds={seletedIds}
+      onSelectItemIds={setSelectedIds}
+    />
+  )
+}
diff --git a/config-ui/src/plugins/webook/connection/api.ts 
b/config-ui/src/plugins/webook/components/miller-columns/use-miller-columns.ts
similarity index 51%
rename from config-ui/src/plugins/webook/connection/api.ts
rename to 
config-ui/src/plugins/webook/components/miller-columns/use-miller-columns.ts
index afae3a921..5e585e88e 100644
--- a/config-ui/src/plugins/webook/connection/api.ts
+++ 
b/config-ui/src/plugins/webook/components/miller-columns/use-miller-columns.ts
@@ -16,30 +16,43 @@
  *
  */
 
-import request from '@/components/utils/request'
+import { useState, useEffect, useMemo } from 'react'
+import type { ItemType } from 'miller-columns-select'
 
-export const getConnections = () => request('/plugins/webhook/connections')
+import * as API from '../../api'
 
-export const getConnection = (id: ID) =>
-  request(`/plugins/webhook/connections/${id}`)
-
-type Paylod = {
+type WebhookItemType = ItemType<{
+  id: ID
   name: string
-}
+}>
 
-export const createConnection = (payload: Paylod) =>
-  request('/plugins/webhook/connections', {
-    method: 'post',
-    data: payload
-  })
+export const useMillerColumns = () => {
+  const [items, setItems] = useState<WebhookItemType[]>([])
+  const [isLast, setIsLast] = useState(false)
 
-export const updateConnection = (id: ID, payload: Paylod) =>
-  request(`/plugins/webhook/connections/${id}`, {
-    method: 'patch',
-    data: payload
-  })
+  const updateItems = (arr: any) =>
+    arr.map((it: any) => ({
+      parentId: null,
+      id: it.id,
+      title: it.name,
+      name: it.name
+    }))
 
-export const deleteConnection = (id: ID) =>
-  request(`/plugins/webhook/connections/${id}`, {
-    method: 'delete'
-  })
+  useEffect(() => {
+    ;(async () => {
+      const res = await API.getConnections()
+      setItems([...updateItems(res)])
+      setIsLast(true)
+    })()
+  }, [])
+
+  return useMemo(
+    () => ({
+      items,
+      getHasMore() {
+        return !isLast
+      }
+    }),
+    [items, isLast]
+  )
+}
diff --git a/config-ui/src/plugins/webook/connection/index.tsx 
b/config-ui/src/plugins/webook/connection/index.tsx
index 4b23dae57..c35e8da44 100644
--- a/config-ui/src/plugins/webook/connection/index.tsx
+++ b/config-ui/src/plugins/webook/connection/index.tsx
@@ -21,32 +21,38 @@ import { ButtonGroup, Button, Icon, Intent } from 
'@blueprintjs/core'
 
 import { Table, ColumnType } from '@/components'
 
-import { CreateDialog, ViewOrEditDialog, DeleteDialog } from '../components'
+import type { WebhookItemType } from '../types'
+import { WebhookCreateDialog } from '../create-dialog'
+import { WebhookDeleteDialog } from '../delete-dialog'
+import { WebhookViewOrEditDialog } from '../view-or-edit-dialog'
 
-import type { ConnectionItem } from './use-connection'
+import type { UseConnectionProps } from './use-connection'
 import { useConnection } from './use-connection'
 import * as S from './styled'
 
 type Type = 'add' | 'edit' | 'show' | 'delete'
 
-export const WebHookConnection = () => {
+interface Props extends UseConnectionProps {
+  onCreateAfter?: (id: ID) => void
+}
+
+export const WebHookConnection = ({ onCreateAfter, ...props }: Props) => {
   const [type, setType] = useState<Type>()
-  const [record, setRecord] = useState<ConnectionItem>()
+  const [record, setRecord] = useState<WebhookItemType>()
 
-  const { loading, saving, connections, onCreate, onUpdate, onDelete } =
-    useConnection()
+  const { loading, connections, onRefresh } = useConnection({ ...props })
 
   const handleHideDialog = () => {
     setType(undefined)
     setRecord(undefined)
   }
 
-  const handleShowDialog = (t: Type, r?: ConnectionItem) => {
+  const handleShowDialog = (t: Type, r?: WebhookItemType) => {
     setType(t)
     setRecord(r)
   }
 
-  const columns: ColumnType<ConnectionItem> = [
+  const columns: ColumnType<WebhookItemType> = [
     {
       title: 'ID',
       dataIndex: 'id',
@@ -86,30 +92,30 @@ export const WebHookConnection = () => {
       </ButtonGroup>
       <Table loading={loading} columns={columns} dataSource={connections} />
       {type === 'add' && (
-        <CreateDialog
+        <WebhookCreateDialog
           isOpen
-          saving={saving}
-          onSubmit={onCreate}
           onCancel={handleHideDialog}
+          onSubmitAfter={(id) => {
+            onRefresh()
+            onCreateAfter?.(id)
+          }}
         />
       )}
-      {(type === 'edit' || type === 'show') && (
-        <ViewOrEditDialog
-          type={type}
-          initialValues={record}
+      {type === 'delete' && (
+        <WebhookDeleteDialog
           isOpen
-          saving={saving}
-          onSubmit={onUpdate}
+          initialValues={record}
           onCancel={handleHideDialog}
+          onSubmitAfter={onRefresh}
         />
       )}
-      {type === 'delete' && (
-        <DeleteDialog
-          initialValues={record}
+      {(type === 'edit' || type === 'show') && (
+        <WebhookViewOrEditDialog
+          type={type}
           isOpen
-          saving={saving}
-          onSubmit={onDelete}
+          initialValues={record}
           onCancel={handleHideDialog}
+          onSubmitAfter={onRefresh}
         />
       )}
     </S.Wrapper>
diff --git a/config-ui/src/plugins/webook/connection/use-connection.ts 
b/config-ui/src/plugins/webook/connection/use-connection.ts
index 348aff788..f5caef867 100644
--- a/config-ui/src/plugins/webook/connection/use-connection.ts
+++ b/config-ui/src/plugins/webook/connection/use-connection.ts
@@ -18,25 +18,24 @@
 
 import { useState, useEffect, useMemo } from 'react'
 
-import { operator } from '@/utils'
+import type { WebhookItemType } from '../types'
+import * as API from '../api'
 
-import * as API from './api'
-
-export type ConnectionItem = {
-  id: ID
-  name: string
+export interface UseConnectionProps {
+  filterIds?: ID[]
 }
 
-export const useConnection = () => {
+export const useConnection = ({ filterIds }: UseConnectionProps) => {
   const [loading, setLoading] = useState(false)
-  const [saving, setSaving] = useState(false)
-  const [connections, setConnections] = useState<ConnectionItem[]>([])
+  const [connections, setConnections] = useState<WebhookItemType[]>([])
 
   const getConnections = async () => {
     setLoading(true)
     try {
       const res = await API.getConnections()
-      setConnections(res)
+      setConnections(
+        res.filter((cs: any) => (filterIds ? filterIds.includes(cs.id) : true))
+      )
     } finally {
       setLoading(false)
     }
@@ -46,49 +45,12 @@ export const useConnection = () => {
     getConnections()
   }, [])
 
-  const handleCreate = async (name: string) => {
-    const [success, res] = await operator(
-      () => API.createConnection({ name }),
-      {
-        setOperating: setSaving
-      }
-    )
-
-    if (success) {
-      getConnections()
-      return API.getConnection(res.id)
-    }
-  }
-
-  const handleUpdate = async (id: ID, name: string) => {
-    const [success] = await operator(() => API.updateConnection(id, { name }), 
{
-      setOperating: setSaving
-    })
-
-    if (success) {
-      getConnections()
-    }
-  }
-
-  const handleDelete = async (id: ID) => {
-    const [success] = await operator(() => API.deleteConnection(id), {
-      setOperating: setSaving
-    })
-
-    if (success) {
-      getConnections()
-    }
-  }
-
   return useMemo(
     () => ({
       loading,
-      saving,
       connections,
-      onCreate: handleCreate,
-      onUpdate: handleUpdate,
-      onDelete: handleDelete
+      onRefresh: getConnections
     }),
-    [loading, saving, connections]
+    [loading, connections]
   )
 }
diff --git a/config-ui/src/plugins/webook/components/create-dialog/index.tsx 
b/config-ui/src/plugins/webook/create-dialog/index.tsx
similarity index 73%
rename from config-ui/src/plugins/webook/components/create-dialog/index.tsx
rename to config-ui/src/plugins/webook/create-dialog/index.tsx
index 3dd430255..cbe6572c5 100644
--- a/config-ui/src/plugins/webook/components/create-dialog/index.tsx
+++ b/config-ui/src/plugins/webook/create-dialog/index.tsx
@@ -16,30 +16,24 @@
  *
  */
 
-import React, { useState, useMemo, useCallback } from 'react'
+import React, { useMemo } from 'react'
 import { InputGroup, Icon } from '@blueprintjs/core'
 import { CopyToClipboard } from 'react-copy-to-clipboard'
 
 import { Dialog, toast } from '@/components'
 
+import type { UseCreateProps } from './use-create'
+import { useCreate } from './use-create'
 import * as S from './styled'
 
-const prefix = `${window.location.origin}/api`
-
-interface Props {
+interface Props extends UseCreateProps {
   isOpen: boolean
-  saving: boolean
-  onSubmit: (name: string) => Promise<any>
   onCancel: () => void
 }
 
-export const CreateDialog = ({ isOpen, saving, onSubmit, onCancel }: Props) => 
{
-  const [step, setStep] = useState(1)
-  const [name, setName] = useState('')
-  const [record, setRecord] = useState({
-    postIssuesEndpoint: '',
-    closeIssuesEndpoint: '',
-    postDeploymentsCurl: ''
+export const WebhookCreateDialog = ({ isOpen, onCancel, ...props }: Props) => {
+  const { saving, step, name, record, onChangeName, onSubmit } = useCreate({
+    ...props
   })
 
   const [okText, okDisabled] = useMemo(
@@ -47,29 +41,19 @@ export const CreateDialog = ({ isOpen, saving, onSubmit, 
onCancel }: Props) => {
     [step, name]
   )
 
-  const handleSubmit = useCallback(async () => {
+  const handleSubmit = () => {
     if (step === 1) {
-      const res = await onSubmit(name)
-      setStep(2)
-      setRecord({
-        postIssuesEndpoint: `${prefix}${res.postIssuesEndpoint}`,
-        closeIssuesEndpoint: `${prefix}${res.closeIssuesEndpoint}`,
-        postDeploymentsCurl: `curl 
${prefix}${res.postPipelineDeployTaskEndpoint} -X 'POST' -d "{
-        \\"commit_sha\\":\\"the sha of deployment commit\\",
-        \\"repo_url\\":\\"the repo URL of the deployment commit\\",
-        \\"start_time\\":\\"Optional, eg. 2020-01-01T12:00:00+00:00\\"
-      }"`
-      })
+      onSubmit()
     } else {
       onCancel()
     }
-  }, [step, name])
+  }
 
   return (
     <Dialog
       isOpen={isOpen}
       title='Add a New Incoming Webhook'
-      style={{ width: 600, top: -100 }}
+      style={{ width: 600 }}
       okText={okText}
       okDisabled={okDisabled}
       okLoading={saving}
@@ -83,7 +67,10 @@ export const CreateDialog = ({ isOpen, saving, onSubmit, 
onCancel }: Props) => {
             Give your Incoming Webhook a unique name to help you identify it in
             the future.
           </p>
-          <InputGroup value={name} onChange={(e) => setName(e.target.value)} />
+          <InputGroup
+            value={name}
+            onChange={(e) => onChangeName(e.target.value)}
+          />
         </S.Wrapper>
       )}
       {step === 2 && (
diff --git a/config-ui/src/plugins/webook/components/create-dialog/styled.ts 
b/config-ui/src/plugins/webook/create-dialog/styled.ts
similarity index 100%
copy from config-ui/src/plugins/webook/components/create-dialog/styled.ts
copy to config-ui/src/plugins/webook/create-dialog/styled.ts
diff --git a/config-ui/src/plugins/webook/create-dialog/use-create.ts 
b/config-ui/src/plugins/webook/create-dialog/use-create.ts
new file mode 100644
index 000000000..7f519a855
--- /dev/null
+++ b/config-ui/src/plugins/webook/create-dialog/use-create.ts
@@ -0,0 +1,78 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ *
+ */
+
+import { useState, useMemo } from 'react'
+
+import { operator } from '@/utils'
+
+import * as API from '../api'
+
+export interface UseCreateProps {
+  onSubmitAfter?: (id: ID) => void
+}
+
+export const useCreate = ({ onSubmitAfter }: UseCreateProps) => {
+  const [saving, setSaving] = useState(false)
+  const [step, setStep] = useState(1)
+  const [name, setName] = useState('')
+  const [record, setRecord] = useState({
+    postIssuesEndpoint: '',
+    closeIssuesEndpoint: '',
+    postDeploymentsCurl: ''
+  })
+
+  const prefix = useMemo(() => `${window.location.origin}/api`, [])
+
+  const handleCreate = async () => {
+    const [success, res] = await operator(
+      async () => {
+        const res = await API.createConnection({ name })
+        return API.getConnection(res.id)
+      },
+      {
+        setOperating: setSaving
+      }
+    )
+
+    if (success) {
+      setStep(2)
+      setRecord({
+        postIssuesEndpoint: `${prefix}${res.postIssuesEndpoint}`,
+        closeIssuesEndpoint: `${prefix}${res.closeIssuesEndpoint}`,
+        postDeploymentsCurl: `curl 
${prefix}${res.postPipelineDeployTaskEndpoint} -X 'POST' -d "{
+        \\"commit_sha\\":\\"the sha of deployment commit\\",
+        \\"repo_url\\":\\"the repo URL of the deployment commit\\",
+        \\"start_time\\":\\"Optional, eg. 2020-01-01T12:00:00+00:00\\"
+      }"`
+      })
+      onSubmitAfter?.(res.id)
+    }
+  }
+
+  return useMemo(
+    () => ({
+      saving,
+      step,
+      name,
+      record,
+      onChangeName: setName,
+      onSubmit: handleCreate
+    }),
+    [saving, step, name, record]
+  )
+}
diff --git a/config-ui/src/plugins/webook/components/delete-dialog/index.tsx 
b/config-ui/src/plugins/webook/delete-dialog/index.tsx
similarity index 81%
copy from config-ui/src/plugins/webook/components/delete-dialog/index.tsx
copy to config-ui/src/plugins/webook/delete-dialog/index.tsx
index 89bfbc8cd..0eb60e676 100644
--- a/config-ui/src/plugins/webook/components/delete-dialog/index.tsx
+++ b/config-ui/src/plugins/webook/delete-dialog/index.tsx
@@ -20,25 +20,20 @@ import React from 'react'
 
 import { Dialog } from '@/components'
 
+import type { UseDeleteProps } from './use-delete'
+import { useDelete } from './use-delete'
 import * as S from './styled'
 
-interface Props {
-  initialValues: any
+interface Props extends UseDeleteProps {
   isOpen: boolean
-  saving: boolean
-  onSubmit: (id: ID) => Promise<any>
   onCancel: () => void
 }
 
-export const DeleteDialog = ({
-  initialValues,
-  isOpen,
-  saving,
-  onSubmit,
-  onCancel
-}: Props) => {
+export const WebhookDeleteDialog = ({ isOpen, onCancel, ...props }: Props) => {
+  const { saving, onSubmit } = useDelete({ ...props })
+
   const handleSubmit = () => {
-    onSubmit(initialValues.id)
+    onSubmit()
     onCancel()
   }
 
@@ -46,7 +41,7 @@ export const DeleteDialog = ({
     <Dialog
       isOpen={isOpen}
       title='Delete this Incoming Webhook?'
-      style={{ width: 600, top: -100 }}
+      style={{ width: 600 }}
       okText='Confirm'
       okLoading={saving}
       onCancel={onCancel}
diff --git a/config-ui/src/plugins/webook/components/delete-dialog/styled.ts 
b/config-ui/src/plugins/webook/delete-dialog/styled.ts
similarity index 100%
copy from config-ui/src/plugins/webook/components/delete-dialog/styled.ts
copy to config-ui/src/plugins/webook/delete-dialog/styled.ts
diff --git a/config-ui/src/plugins/webook/components/create-dialog/styled.ts 
b/config-ui/src/plugins/webook/delete-dialog/use-delete.ts
similarity index 53%
rename from config-ui/src/plugins/webook/components/create-dialog/styled.ts
rename to config-ui/src/plugins/webook/delete-dialog/use-delete.ts
index 347b93853..3112c5567 100644
--- a/config-ui/src/plugins/webook/components/create-dialog/styled.ts
+++ b/config-ui/src/plugins/webook/delete-dialog/use-delete.ts
@@ -16,36 +16,42 @@
  *
  */
 
-import styled from 'styled-components'
-import { Colors } from '@blueprintjs/core'
-
-export const Wrapper = styled.div`
-  h2 {
-    display: flex;
-    align-items: center;
-    justify-content: center;
-    margin: 0;
-    margin-bottom: 16px;
-    padding: 0;
-    font-weight: 600;
-    color: ${Colors.GREEN5};
-
-    .bp4-icon {
-      margin-right: 8px;
-    }
-  }
+import { useState, useMemo } from 'react'
+
+import { operator } from '@/utils'
 
-  h3 {
-    margin: 0;
-    padding: 0;
+import * as API from '../api'
+
+export interface UseDeleteProps {
+  initialValues?: {
+    id: ID
   }
+  onSubmitAfter?: (id: ID) => void
+}
+
+export const useDelete = ({ initialValues, onSubmitAfter }: UseDeleteProps) => 
{
+  const [saving, setSaving] = useState(false)
 
-  .block {
-    display: flex;
-    align-items: center;
-    justify-content: space-between;
-    margin-bottom: 8px;
-    padding: 10px 16px;
-    background: #f0f4fe;
+  const handleDelete = async () => {
+    if (!initialValues) return
+
+    const [success] = await operator(
+      () => API.deleteConnection(initialValues.id),
+      {
+        setOperating: setSaving
+      }
+    )
+
+    if (success) {
+      onSubmitAfter?.(initialValues.id)
+    }
   }
-`
+
+  return useMemo(
+    () => ({
+      saving,
+      onSubmit: handleDelete
+    }),
+    [saving]
+  )
+}
diff --git a/config-ui/src/plugins/webook/index.ts 
b/config-ui/src/plugins/webook/index.ts
index de78e5c49..2011e2dd9 100644
--- a/config-ui/src/plugins/webook/index.ts
+++ b/config-ui/src/plugins/webook/index.ts
@@ -16,4 +16,9 @@
  *
  */
 
+export * from './types'
 export * from './connection'
+export * from './create-dialog'
+export * from './delete-dialog'
+export * from './view-or-edit-dialog'
+export * from './selector-dialog'
diff --git a/config-ui/src/plugins/webook/components/delete-dialog/index.tsx 
b/config-ui/src/plugins/webook/selector-dialog/index.tsx
similarity index 61%
rename from config-ui/src/plugins/webook/components/delete-dialog/index.tsx
rename to config-ui/src/plugins/webook/selector-dialog/index.tsx
index 89bfbc8cd..428400875 100644
--- a/config-ui/src/plugins/webook/components/delete-dialog/index.tsx
+++ b/config-ui/src/plugins/webook/selector-dialog/index.tsx
@@ -16,46 +16,53 @@
  *
  */
 
-import React from 'react'
+import React, { useState } from 'react'
 
 import { Dialog } from '@/components'
 
+import type { WebhookItemType } from '../types'
+
+import { MillerColumns } from '../components'
+
 import * as S from './styled'
 
 interface Props {
-  initialValues: any
   isOpen: boolean
   saving: boolean
-  onSubmit: (id: ID) => Promise<any>
   onCancel: () => void
+  onSubmit: (items: WebhookItemType[]) => void
 }
 
-export const DeleteDialog = ({
-  initialValues,
+export const WebhookSelectorDialog = ({
   isOpen,
   saving,
-  onSubmit,
-  onCancel
+  onCancel,
+  onSubmit
 }: Props) => {
-  const handleSubmit = () => {
-    onSubmit(initialValues.id)
-    onCancel()
-  }
+  const [selectedItems, setSelectedItems] = useState<WebhookItemType[]>([])
+
+  const handleSubmit = () => onSubmit(selectedItems)
 
   return (
     <Dialog
       isOpen={isOpen}
-      title='Delete this Incoming Webhook?'
-      style={{ width: 600, top: -100 }}
-      okText='Confirm'
+      title='Select Existing Webhooks'
+      style={{
+        width: 820
+      }}
+      okText='Confrim'
       okLoading={saving}
+      okDisabled={!selectedItems.length}
       onCancel={onCancel}
       onOk={handleSubmit}
     >
       <S.Wrapper>
-        <div className='message'>
-          <p>This Incoming Webhook cannot be recovered once it’s deleted.</p>
-        </div>
+        <h3>Webhooks</h3>
+        <p>Select an existing Webhook to import to the current project.</p>
+        <MillerColumns
+          selectedItems={selectedItems}
+          onChangeItems={setSelectedItems}
+        />
       </S.Wrapper>
     </Dialog>
   )
diff --git a/config-ui/src/plugins/webook/components/delete-dialog/styled.ts 
b/config-ui/src/plugins/webook/selector-dialog/styled.ts
similarity index 84%
rename from config-ui/src/plugins/webook/components/delete-dialog/styled.ts
rename to config-ui/src/plugins/webook/selector-dialog/styled.ts
index 9a54e477e..614786f07 100644
--- a/config-ui/src/plugins/webook/components/delete-dialog/styled.ts
+++ b/config-ui/src/plugins/webook/selector-dialog/styled.ts
@@ -18,4 +18,16 @@
 
 import styled from 'styled-components'
 
-export const Wrapper = styled.div``
+export const Wrapper = styled.div`
+  padding: 0 8px;
+
+  h3 {
+    margin: 8px 0;
+    font-size: 14px;
+  }
+
+  p {
+    margin: 0 0 8px;
+    font-size: 12px;
+  }
+`
diff --git a/config-ui/src/plugins/webook/index.ts 
b/config-ui/src/plugins/webook/types.ts
similarity index 93%
copy from config-ui/src/plugins/webook/index.ts
copy to config-ui/src/plugins/webook/types.ts
index de78e5c49..f212d2683 100644
--- a/config-ui/src/plugins/webook/index.ts
+++ b/config-ui/src/plugins/webook/types.ts
@@ -16,4 +16,7 @@
  *
  */
 
-export * from './connection'
+export type WebhookItemType = {
+  id: ID
+  name: string
+}
diff --git 
a/config-ui/src/plugins/webook/components/view-or-edit-dialog/index.tsx 
b/config-ui/src/plugins/webook/view-or-edit-dialog/index.tsx
similarity index 72%
rename from 
config-ui/src/plugins/webook/components/view-or-edit-dialog/index.tsx
rename to config-ui/src/plugins/webook/view-or-edit-dialog/index.tsx
index 2a933f877..ad3076647 100644
--- a/config-ui/src/plugins/webook/components/view-or-edit-dialog/index.tsx
+++ b/config-ui/src/plugins/webook/view-or-edit-dialog/index.tsx
@@ -16,66 +16,45 @@
  *
  */
 
-import React, { useState, useEffect, useCallback } from 'react'
+import React from 'react'
 import { InputGroup, Icon } from '@blueprintjs/core'
 import { CopyToClipboard } from 'react-copy-to-clipboard'
 
 import { Dialog, toast } from '@/components'
 
+import type { UseViewOrEditProps } from './use-view-or-edit'
+import { useViewOrEdit } from './use-view-or-edit'
 import * as S from './styled'
 
-const prefix = `${window.location.origin}/api`
-
-interface Props {
+interface Props extends UseViewOrEditProps {
   type: 'edit' | 'show'
-  initialValues: any
   isOpen: boolean
-  saving: boolean
-  onSubmit: (id: ID, name: string) => Promise<any>
   onCancel: () => void
 }
 
-export const ViewOrEditDialog = ({
+export const WebhookViewOrEditDialog = ({
   type,
-  initialValues,
   isOpen,
-  saving,
-  onSubmit,
-  onCancel
+  onCancel,
+  ...props
 }: Props) => {
-  const [name, setName] = useState('')
-  const [record, setRecord] = useState({
-    postIssuesEndpoint: '',
-    closeIssuesEndpoint: '',
-    postDeploymentsCurl: ''
+  const { saving, name, record, onChangeName, onSubmit } = useViewOrEdit({
+    ...props
   })
 
-  useEffect(() => {
-    setName(initialValues.name)
-    setRecord({
-      postIssuesEndpoint: `${prefix}${initialValues.postIssuesEndpoint}`,
-      closeIssuesEndpoint: `${prefix}${initialValues.closeIssuesEndpoint}`,
-      postDeploymentsCurl: `curl 
${prefix}${initialValues.postPipelineDeployTaskEndpoint} -X 'POST' -d "{
-\\"commit_sha\\":\\"the sha of deployment commit\\",
-\\"repo_url\\":\\"the repo URL of the deployment commit\\",
-\\"start_time\\":\\"Optional, eg. 2020-01-01T12:00:00+00:00\\"
-}"`
-    })
-  }, [initialValues])
-
-  const handleSubmit = useCallback(async () => {
+  const handleSubmit = () => {
     if (type === 'edit') {
-      await onSubmit(initialValues.id, name)
+      onSubmit()
     }
 
     onCancel()
-  }, [name])
+  }
 
   return (
     <Dialog
       isOpen={isOpen}
       title='View or Edit Incoming Webhook'
-      style={{ width: 600, top: -100 }}
+      style={{ width: 600 }}
       okText={type === 'edit' ? 'Save' : 'Done'}
       okDisabled={!name}
       okLoading={saving}
@@ -91,7 +70,7 @@ export const ViewOrEditDialog = ({
         <InputGroup
           disabled={type !== 'edit'}
           value={name}
-          onChange={(e) => setName(e.target.value)}
+          onChange={(e) => onChangeName(e.target.value)}
         />
         <h2>
           <Icon icon='endorsed' size={30} />
diff --git 
a/config-ui/src/plugins/webook/components/view-or-edit-dialog/styled.ts 
b/config-ui/src/plugins/webook/view-or-edit-dialog/styled.ts
similarity index 100%
rename from 
config-ui/src/plugins/webook/components/view-or-edit-dialog/styled.ts
rename to config-ui/src/plugins/webook/view-or-edit-dialog/styled.ts
diff --git 
a/config-ui/src/plugins/webook/view-or-edit-dialog/use-view-or-edit.ts 
b/config-ui/src/plugins/webook/view-or-edit-dialog/use-view-or-edit.ts
new file mode 100644
index 000000000..ddb8cc2d8
--- /dev/null
+++ b/config-ui/src/plugins/webook/view-or-edit-dialog/use-view-or-edit.ts
@@ -0,0 +1,84 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ *
+ */
+
+import { useState, useMemo, useEffect } from 'react'
+
+import { operator } from '@/utils'
+
+import * as API from '../api'
+
+export interface UseViewOrEditProps {
+  initialValues?: {
+    id: ID
+  } & any
+  onSubmitAfter?: (id: ID) => void
+}
+
+export const useViewOrEdit = ({
+  initialValues,
+  onSubmitAfter
+}: UseViewOrEditProps) => {
+  const [saving, setSaving] = useState(false)
+  const [name, setName] = useState('')
+  const [record, setRecord] = useState({
+    postIssuesEndpoint: '',
+    closeIssuesEndpoint: '',
+    postDeploymentsCurl: ''
+  })
+
+  const prefix = useMemo(() => `${window.location.origin}/api`, [])
+
+  useEffect(() => {
+    setName(initialValues.name)
+    setRecord({
+      postIssuesEndpoint: `${prefix}${initialValues.postIssuesEndpoint}`,
+      closeIssuesEndpoint: `${prefix}${initialValues.closeIssuesEndpoint}`,
+      postDeploymentsCurl: `curl 
${prefix}${initialValues.postPipelineDeployTaskEndpoint} -X 'POST' -d "{
+\\"commit_sha\\":\\"the sha of deployment commit\\",
+\\"repo_url\\":\\"the repo URL of the deployment commit\\",
+\\"start_time\\":\\"Optional, eg. 2020-01-01T12:00:00+00:00\\"
+}"`
+    })
+  }, [initialValues])
+
+  const handleUpdate = async () => {
+    if (!initialValues) return
+
+    const [success] = await operator(
+      () => API.updateConnection(initialValues.id, { name }),
+      {
+        setOperating: setSaving
+      }
+    )
+
+    if (success) {
+      onSubmitAfter?.(initialValues.id)
+    }
+  }
+
+  return useMemo(
+    () => ({
+      saving,
+      name,
+      record,
+      onChangeName: setName,
+      onSubmit: handleUpdate
+    }),
+    [saving, name, record]
+  )
+}

Reply via email to