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

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

commit 464f3645103f6285e6ef7823935bd9d1228068be
Author: mintsweet <[email protected]>
AuthorDate: Wed Oct 18 15:23:47 2023 +1300

    refactor(config-ui): remove connections context
---
 config-ui/src/hooks/index.ts                |   1 -
 config-ui/src/hooks/use-connections.ts      |  47 -------
 config-ui/src/store/connections/context.tsx | 182 ----------------------------
 config-ui/src/store/connections/index.ts    |  20 ---
 config-ui/src/store/connections/types.ts    |  45 -------
 config-ui/src/store/index.ts                |   1 -
 6 files changed, 296 deletions(-)

diff --git a/config-ui/src/hooks/index.ts b/config-ui/src/hooks/index.ts
index cba922842..7f38b5d6f 100644
--- a/config-ui/src/hooks/index.ts
+++ b/config-ui/src/hooks/index.ts
@@ -17,7 +17,6 @@
  */
 
 export * from './use-auto-refresh';
-export * from './use-connections';
 export * from './use-refresh-data';
 export * from './use-tips';
 export * from './user-proxy-prefix';
diff --git a/config-ui/src/hooks/use-connections.ts 
b/config-ui/src/hooks/use-connections.ts
deleted file mode 100644
index f129c13ee..000000000
--- a/config-ui/src/hooks/use-connections.ts
+++ /dev/null
@@ -1,47 +0,0 @@
-/*
- * 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 { useContext } from 'react';
-
-import { ConnectionContext } from '@/store';
-
-type UseConnectionsProps = {
-  unique?: string;
-  plugin?: string;
-  filter?: string[];
-  filterBeta?: boolean;
-  filterPlugin?: string[];
-};
-
-export const useConnections = (props?: UseConnectionsProps) => {
-  const { unique, plugin, filter, filterBeta, filterPlugin } = props || {};
-
-  const { connections, onGet, onTest, onRefresh } = 
useContext(ConnectionContext);
-
-  return {
-    connection: unique ? connections.find((cs) => cs.unique === unique) : null,
-    connections: connections
-      .filter((cs) => (plugin ? cs.plugin === plugin : true))
-      .filter((cs) => (filter ? !filter.includes(cs.unique) : true))
-      .filter((cs) => (filterBeta ? !cs.isBeta : true))
-      .filter((cs) => (filterPlugin ? !filterPlugin.includes(cs.plugin) : 
true)),
-    onGet,
-    onTest,
-    onRefresh,
-  };
-};
diff --git a/config-ui/src/store/connections/context.tsx 
b/config-ui/src/store/connections/context.tsx
deleted file mode 100644
index 42e213dea..000000000
--- a/config-ui/src/store/connections/context.tsx
+++ /dev/null
@@ -1,182 +0,0 @@
-/*
- * 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, useMemo } from 'react';
-
-import { PageLoading } from '@/components';
-
-import API from '@/api';
-import type { PluginConfigType } from '@/plugins';
-import { PluginConfig, PluginType } from '@/plugins';
-
-import type { ConnectionItemType } from './types';
-import { ConnectionStatusEnum } from './types';
-
-export const ConnectionContext = React.createContext<{
-  connections: ConnectionItemType[];
-  onGet: (unique: string) => ConnectionItemType;
-  onTest: (unique: string) => void;
-  onRefresh: (plugin?: string) => void;
-}>(undefined!);
-
-interface Props {
-  children?: React.ReactNode;
-}
-
-export const ConnectionContextProvider = ({ children, ...props }: Props) => {
-  const [loading, setLoading] = useState(true);
-  const [connections, setConnections] = useState<ConnectionItemType[]>([]);
-
-  const plugins = useMemo(() => PluginConfig.filter((p) => p.type === 
PluginType.Connection), []);
-
-  const queryConnection = async (plugin: string) => {
-    try {
-      const res = await API.connection.list(plugin);
-      const { name, icon, isBeta, scopeConfig } = plugins.find((p) => p.plugin 
=== plugin) as PluginConfigType;
-
-      return res.map((connection) => ({
-        ...connection,
-        plugin,
-        pluginName: name,
-        icon,
-        isBeta: isBeta ?? false,
-        entities: scopeConfig?.entities ?? [],
-      }));
-    } catch {
-      return [];
-    }
-  };
-
-  const testConnection = async ({
-    plugin,
-    endpoint,
-    proxy,
-    token,
-    username,
-    password,
-    authMethod,
-    secretKey,
-    appId,
-    dbUrl,
-  }: ConnectionItemType) => {
-    try {
-      const res = await API.connection.test(plugin, {
-        endpoint,
-        proxy,
-        token,
-        username,
-        password,
-        authMethod,
-        secretKey,
-        appId,
-        dbUrl,
-      });
-      return res.success ? ConnectionStatusEnum.ONLINE : 
ConnectionStatusEnum.OFFLINE;
-    } catch {
-      return ConnectionStatusEnum.OFFLINE;
-    }
-  };
-
-  const transformConnection = (connections: Omit<ConnectionItemType, 'unique' 
| 'status'>[]) => {
-    return connections.map((it) => ({
-      unique: `${it.plugin}-${it.id}`,
-      plugin: it.plugin,
-      pluginName: it.pluginName,
-      id: it.id,
-      name: it.name,
-      status: ConnectionStatusEnum.NULL,
-      icon: it.icon,
-      isBeta: it.isBeta,
-      entities: it.entities,
-      endpoint: it.endpoint,
-      proxy: it.proxy,
-      token: it.token,
-      username: it.username,
-      password: it.password,
-      authMethod: it.authMethod,
-      secretKey: it.secretKey,
-      appId: it.appId,
-      dbUrl: it.dbUrl,
-    }));
-  };
-
-  const handleGet = (unique: string) => {
-    return connections.find((cs) => cs.unique === unique) as 
ConnectionItemType;
-  };
-
-  const handleTest = async (unique: string) => {
-    setConnections((connections) =>
-      connections.map((cs) =>
-        cs.unique === unique
-          ? {
-              ...cs,
-              status: ConnectionStatusEnum.TESTING,
-            }
-          : cs,
-      ),
-    );
-
-    const connection = handleGet(unique);
-    const status = await testConnection(connection);
-
-    setConnections((connections) =>
-      connections.map((cs) =>
-        cs.unique === unique
-          ? {
-              ...cs,
-              status,
-            }
-          : cs,
-      ),
-    );
-  };
-
-  const handleRefresh = async (plugin?: string) => {
-    if (plugin) {
-      const res = await queryConnection(plugin);
-      setConnections([...connections.filter((cs) => cs.plugin !== plugin), 
...transformConnection(res)]);
-      return;
-    }
-
-    const res = await Promise.all(plugins.map((cs) => 
queryConnection(cs.plugin)));
-
-    setConnections(transformConnection(res.flat()));
-    setLoading(false);
-  };
-
-  useEffect(() => {
-    handleRefresh();
-  }, []);
-
-  if (loading) {
-    return <PageLoading />;
-  }
-
-  return (
-    <ConnectionContext.Provider
-      value={{
-        connections,
-        onGet: handleGet,
-        onTest: handleTest,
-        onRefresh: handleRefresh,
-      }}
-    >
-      {children}
-    </ConnectionContext.Provider>
-  );
-};
diff --git a/config-ui/src/store/connections/index.ts 
b/config-ui/src/store/connections/index.ts
deleted file mode 100644
index 34ae5c1d0..000000000
--- a/config-ui/src/store/connections/index.ts
+++ /dev/null
@@ -1,20 +0,0 @@
-/*
- * 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.
- *
- */
-
-export * from './types';
-export * from './context';
diff --git a/config-ui/src/store/connections/types.ts 
b/config-ui/src/store/connections/types.ts
deleted file mode 100644
index 07475889a..000000000
--- a/config-ui/src/store/connections/types.ts
+++ /dev/null
@@ -1,45 +0,0 @@
-/*
- * 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.
- *
- */
-
-export enum ConnectionStatusEnum {
-  ONLINE = 'online',
-  OFFLINE = 'offline',
-  TESTING = 'testing',
-  NULL = 'null',
-}
-
-export type ConnectionItemType = {
-  unique: string;
-  plugin: string;
-  pluginName: string;
-  id: ID;
-  name: string;
-  status: ConnectionStatusEnum;
-  icon: string;
-  isBeta: boolean;
-  entities: string[];
-  endpoint: string;
-  proxy: string;
-  token?: string;
-  username?: string;
-  password?: string;
-  authMethod?: string;
-  appId?: string;
-  secretKey?: string;
-  dbUrl?: string;
-};
diff --git a/config-ui/src/store/index.ts b/config-ui/src/store/index.ts
index a247f9b21..378c0c717 100644
--- a/config-ui/src/store/index.ts
+++ b/config-ui/src/store/index.ts
@@ -16,5 +16,4 @@
  *
  */
 
-export * from './connections';
 export * from './tips';

Reply via email to