You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@devlake.apache.org by li...@apache.org on 2023/01/05 08:11:03 UTC

[incubator-devlake] branch main updated: fix(config-ui): adjust the rateLimitPerHour default value (#4095)

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 54cefa2cf fix(config-ui): adjust the rateLimitPerHour default value (#4095)
54cefa2cf is described below

commit 54cefa2cf4e151deafd1b6467752cc5ba15c6335
Author: 青湛 <0x...@gmail.com>
AuthorDate: Thu Jan 5 16:10:58 2023 +0800

    fix(config-ui): adjust the rateLimitPerHour default value (#4095)
    
    * fix(config-ui): adjust the rateLimitPerHour default value
    
    * refactor(config-ui): separate the rate-limit component
---
 .../src/pages/connection/form/components/index.ts  |  1 +
 .../form/components/rate-limit/index.tsx           | 58 ++++++++++++++++++++++
 .../components/{index.ts => rate-limit/styled.ts}  | 15 +++++-
 config-ui/src/pages/connection/form/index.tsx      | 52 ++++++-------------
 config-ui/src/pages/connection/form/use-form.ts    |  2 +-
 config-ui/src/plugins/github/config.ts             |  5 +-
 config-ui/src/plugins/gitlab/config.ts             |  7 ++-
 config-ui/src/plugins/jenkins/config.ts            |  7 ++-
 config-ui/src/plugins/jira/config.ts               |  7 ++-
 config-ui/src/plugins/types.ts                     |  2 +-
 10 files changed, 108 insertions(+), 48 deletions(-)

diff --git a/config-ui/src/pages/connection/form/components/index.ts b/config-ui/src/pages/connection/form/components/index.ts
index d54071fb7..90e7488a6 100644
--- a/config-ui/src/pages/connection/form/components/index.ts
+++ b/config-ui/src/pages/connection/form/components/index.ts
@@ -17,3 +17,4 @@
  */
 
 export * from './github-token';
+export * from './rate-limit';
diff --git a/config-ui/src/pages/connection/form/components/rate-limit/index.tsx b/config-ui/src/pages/connection/form/components/rate-limit/index.tsx
new file mode 100644
index 000000000..6b7fcffb1
--- /dev/null
+++ b/config-ui/src/pages/connection/form/components/rate-limit/index.tsx
@@ -0,0 +1,58 @@
+/*
+ * 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 { NumericInput, Switch } from '@blueprintjs/core';
+
+import * as S from './styled';
+
+interface Props {
+  initialValue?: number;
+  value?: number;
+  onChange?: (value: number) => void;
+}
+
+export const RateLimit = ({ initialValue, value, onChange }: Props) => {
+  const [show, setShow] = useState(false);
+
+  useEffect(() => {
+    setShow(value ? true : false);
+  }, [value]);
+
+  const handleChangeValue = (value: number) => {
+    onChange?.(show ? value : 0);
+  };
+
+  const handleChangeShow = (e: React.FormEvent<HTMLInputElement>) => {
+    const checked = (e.target as HTMLInputElement).checked;
+    setShow(checked);
+    if (!checked) {
+      onChange?.(0);
+    } else {
+      onChange?.(initialValue ?? 0);
+    }
+  };
+
+  return (
+    <S.Wrapper>
+      {show && <NumericInput value={value} onValueChange={handleChangeValue} />}
+      <Switch checked={show} onChange={handleChangeShow} />
+      <span>{show ? `Enabled - ${value} Requests/hr` : 'Disabled'}</span>
+    </S.Wrapper>
+  );
+};
diff --git a/config-ui/src/pages/connection/form/components/index.ts b/config-ui/src/pages/connection/form/components/rate-limit/styled.ts
similarity index 79%
copy from config-ui/src/pages/connection/form/components/index.ts
copy to config-ui/src/pages/connection/form/components/rate-limit/styled.ts
index d54071fb7..b94328874 100644
--- a/config-ui/src/pages/connection/form/components/index.ts
+++ b/config-ui/src/pages/connection/form/components/rate-limit/styled.ts
@@ -16,4 +16,17 @@
  *
  */
 
-export * from './github-token';
+import styled from 'styled-components';
+
+export const Wrapper = styled.div`
+  display: flex;
+  align-items: center;
+
+  & > .bp4-numeric-input {
+    margin-right: 8px;
+  }
+
+  & > .bp4-switch {
+    margin: 0;
+  }
+`;
diff --git a/config-ui/src/pages/connection/form/index.tsx b/config-ui/src/pages/connection/form/index.tsx
index 0aa28c50f..bda33a0dc 100644
--- a/config-ui/src/pages/connection/form/index.tsx
+++ b/config-ui/src/pages/connection/form/index.tsx
@@ -19,30 +19,19 @@
 import React, { useState, useEffect, useMemo } from 'react';
 import { useParams, useHistory } from 'react-router-dom';
 import { pick } from 'lodash';
-import {
-  FormGroup,
-  InputGroup,
-  NumericInput,
-  Switch,
-  ButtonGroup,
-  Button,
-  Icon,
-  Intent,
-  Position,
-} from '@blueprintjs/core';
+import { FormGroup, InputGroup, Switch, ButtonGroup, Button, Icon, Intent, Position } from '@blueprintjs/core';
 import { Tooltip2 } from '@blueprintjs/popover2';
 
 import { PageHeader, Card, PageLoading } from '@/components';
 import type { PluginConfigConnectionType } from '@/plugins';
 import { Plugins, PluginConfig } from '@/plugins';
 
-import { GitHubToken } from './components';
+import { GitHubToken, RateLimit } from './components';
 import { useForm } from './use-form';
 import * as S from './styled';
 
 export const ConnectionFormPage = () => {
   const [form, setForm] = useState<Record<string, any>>({});
-  const [showRateLimit, setShowRateLimit] = useState(false);
 
   const history = useHistory();
   const { plugin, cid } = useParams<{ plugin: Plugins; cid?: string }>();
@@ -56,11 +45,8 @@ export const ConnectionFormPage = () => {
   useEffect(() => {
     setForm({
       ...form,
-      ...(initialValues ?? {}),
       ...(connection ?? {}),
     });
-
-    setShowRateLimit(connection?.rateLimitPerHour ? true : false);
   }, [initialValues, connection]);
 
   const error = useMemo(
@@ -115,29 +101,9 @@ export const ConnectionFormPage = () => {
             onChange={(e) => setForm({ ...form, [`${key}`]: e.target.value })}
           />
         )}
-        {type === 'numeric' && (
-          <S.RateLimit>
-            {showRateLimit && (
-              <NumericInput
-                placeholder={placeholder}
-                value={form[key]}
-                onValueChange={(value) =>
-                  setForm({
-                    ...form,
-                    [key]: value,
-                  })
-                }
-              />
-            )}
-            <Switch
-              checked={showRateLimit}
-              onChange={(e) => setShowRateLimit((e.target as HTMLInputElement).checked)}
-            />
-          </S.RateLimit>
-        )}
         {type === 'switch' && (
           <Switch
-            checked={form[key] ?? false}
+            checked={form[key] ?? initialValues?.[key] ?? false}
             onChange={(e) =>
               setForm({
                 ...form,
@@ -146,6 +112,18 @@ export const ConnectionFormPage = () => {
             }
           />
         )}
+        {type === 'rateLimit' && (
+          <RateLimit
+            initialValue={initialValues?.rateLimitPerHour}
+            value={form.rateLimitPerHour}
+            onChange={(value) =>
+              setForm({
+                ...form,
+                rateLimitPerHour: value,
+              })
+            }
+          />
+        )}
         {type === 'github-token' && (
           <GitHubToken
             form={form}
diff --git a/config-ui/src/pages/connection/form/use-form.ts b/config-ui/src/pages/connection/form/use-form.ts
index 15fc52de7..97ab6318e 100644
--- a/config-ui/src/pages/connection/form/use-form.ts
+++ b/config-ui/src/pages/connection/form/use-form.ts
@@ -32,7 +32,7 @@ interface Props {
 export const useForm = ({ plugin, id }: Props) => {
   const [loading, setLoading] = useState(false);
   const [operating, setOperating] = useState(false);
-  const [connection, setConnection] = useState<any>({});
+  const [connection, setConnection] = useState<any>();
 
   const history = useHistory();
 
diff --git a/config-ui/src/plugins/github/config.ts b/config-ui/src/plugins/github/config.ts
index 1e6e0bd80..3c9b96813 100644
--- a/config-ui/src/plugins/github/config.ts
+++ b/config-ui/src/plugins/github/config.ts
@@ -29,6 +29,7 @@ export const GitHubConfig: PluginConfigType = {
   connection: {
     initialValues: {
       enableGraphql: true,
+      rateLimitPerHour: 4500,
     },
     fields: [
       {
@@ -67,8 +68,8 @@ export const GitHubConfig: PluginConfigType = {
       },
       {
         key: 'rateLimitPerHour',
-        label: 'Rate Limit (per hour)',
-        type: 'numeric',
+        label: 'Fixed Rate Limit (per hour)',
+        type: 'rateLimit',
         tooltip: 'Rate Limit requests per hour,\nEnter a numeric value > 0 to enable.',
       },
     ],
diff --git a/config-ui/src/plugins/gitlab/config.ts b/config-ui/src/plugins/gitlab/config.ts
index e07810def..fb124c544 100644
--- a/config-ui/src/plugins/gitlab/config.ts
+++ b/config-ui/src/plugins/gitlab/config.ts
@@ -27,6 +27,9 @@ export const GitLabConfig: PluginConfigType = {
   type: PluginType.Connection,
   icon: Icon,
   connection: {
+    initialValues: {
+      rateLimitPerHour: 5000,
+    },
     fields: [
       {
         key: 'name',
@@ -57,8 +60,8 @@ export const GitLabConfig: PluginConfigType = {
       },
       {
         key: 'rateLimitPerHour',
-        label: 'Rate Limit (per hour)',
-        type: 'numeric',
+        label: 'Fixed Rate Limit (per hour)',
+        type: 'rateLimit',
         tooltip: 'Rate Limit requests per hour,\nEnter a numeric value > 0 to enable.',
       },
     ],
diff --git a/config-ui/src/plugins/jenkins/config.ts b/config-ui/src/plugins/jenkins/config.ts
index 6069d09f8..44bd8199f 100644
--- a/config-ui/src/plugins/jenkins/config.ts
+++ b/config-ui/src/plugins/jenkins/config.ts
@@ -27,6 +27,9 @@ export const JenkinsConfig: PluginConfigType = {
   type: PluginType.Connection,
   icon: Icon,
   connection: {
+    initialValues: {
+      rateLimitPerHour: 10000,
+    },
     fields: [
       {
         key: 'name',
@@ -64,8 +67,8 @@ export const JenkinsConfig: PluginConfigType = {
       },
       {
         key: 'rateLimitPerHour',
-        label: 'Rate Limit (per hour)',
-        type: 'numeric',
+        label: 'Fixed Rate Limit (per hour)',
+        type: 'rateLimit',
         tooltip: 'Rate Limit requests per hour,\nEnter a numeric value > 0 to enable.',
       },
     ],
diff --git a/config-ui/src/plugins/jira/config.ts b/config-ui/src/plugins/jira/config.ts
index 88f8d4823..773abe24b 100644
--- a/config-ui/src/plugins/jira/config.ts
+++ b/config-ui/src/plugins/jira/config.ts
@@ -27,6 +27,9 @@ export const JIRAConfig: PluginConfigType = {
   type: PluginType.Connection,
   icon: Icon,
   connection: {
+    initialValues: {
+      rateLimitPerHour: 3000,
+    },
     fields: [
       {
         key: 'name',
@@ -65,8 +68,8 @@ export const JIRAConfig: PluginConfigType = {
       },
       {
         key: 'rateLimitPerHour',
-        label: 'Rate Limit (per hour)',
-        type: 'numeric',
+        label: 'Fixed Rate Limit (per hour)',
+        type: 'rateLimit',
         tooltip: 'Rate Limit requests per hour,\nEnter a numeric value > 0 to enable.',
       },
     ],
diff --git a/config-ui/src/plugins/types.ts b/config-ui/src/plugins/types.ts
index 20dedfc31..6a6d9f5db 100644
--- a/config-ui/src/plugins/types.ts
+++ b/config-ui/src/plugins/types.ts
@@ -52,7 +52,7 @@ export type PluginConfigConnectionType = {
     initialValues?: Record<string, any>;
     fields: Array<{
       key: string;
-      type: 'text' | 'password' | 'numeric' | 'switch' | 'github-token';
+      type: 'text' | 'password' | 'switch' | 'rateLimit' | 'github-token';
       label: string;
       required?: boolean;
       placeholder?: string;