github.com/minio/console@v1.4.1/web-app/src/screens/Console/EventDestinations/WebhookSettings/WebhookSettings.tsx (about)

     1  // This file is part of MinIO Console Server
     2  // Copyright (c) 2023 MinIO, Inc.
     3  //
     4  // This program is free software: you can redistribute it and/or modify
     5  // it under the terms of the GNU Affero General Public License as published by
     6  // the Free Software Foundation, either version 3 of the License, or
     7  // (at your option) any later version.
     8  //
     9  // This program is distributed in the hope that it will be useful,
    10  // but WITHOUT ANY WARRANTY; without even the implied warranty of
    11  // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    12  // GNU Affero General Public License for more details.
    13  //
    14  // You should have received a copy of the GNU Affero General Public License
    15  // along with this program.  If not, see <http://www.gnu.org/licenses/>.
    16  
    17  import React, { Fragment, useState } from "react";
    18  import { IConfigurationSys, IElementValue } from "../../Configurations/types";
    19  import {
    20    Button,
    21    ConsoleIcon,
    22    DataTable,
    23    Grid,
    24    TierOfflineIcon,
    25    TierOnlineIcon,
    26  } from "mds";
    27  import AddEndpointModal from "./AddEndpointModal";
    28  import DeleteWebhookEndpoint from "./DeleteWebhookEndpoint";
    29  import EditWebhookEndpoint from "./EditWebhookEndpoint";
    30  import { Configuration } from "api/consoleApi";
    31  
    32  interface WebhookSettingsProps {
    33    WebhookSettingslist: Configuration[];
    34    setResetConfigurationOpen: () => void;
    35    type: string;
    36  }
    37  
    38  const WebhookSettings = ({
    39    setResetConfigurationOpen,
    40    WebhookSettingslist,
    41    type,
    42  }: WebhookSettingsProps) => {
    43    const [newEndpointOpen, setNewEndpointOpen] = useState<boolean>(false);
    44    const [deleteWebhookOpen, setDeleteWebhookOpen] = useState<boolean>(false);
    45    const [editWebhookOpen, setEditWebhookOpen] = useState<boolean>(false);
    46    const [selectedARN, setSelectedARN] = useState<string>("");
    47    const [selectedEndpoint, setSelectedEndpoint] =
    48      useState<IConfigurationSys | null>(null);
    49  
    50    const renderEndpoint = (item: IElementValue[]) => {
    51      const endpointFilter = item.find((itm) => itm.key === "endpoint");
    52  
    53      if (endpointFilter) {
    54        if (endpointFilter.env_override) {
    55          return endpointFilter.env_override.value;
    56        }
    57  
    58        return endpointFilter.value;
    59      }
    60  
    61      return "";
    62    };
    63  
    64    const renderWebhookStatus = (item: IElementValue[]) => {
    65      const EnableFilter = item.find((itm) => itm.key === "enable");
    66  
    67      if (EnableFilter?.env_override) {
    68        const overrideEnabled =
    69          !EnableFilter?.env_override.value ||
    70          EnableFilter?.env_override.value === "on" ||
    71          !EnableFilter?.env_override.value
    72            ? "Enabled"
    73            : "Disabled";
    74        return (
    75          <Grid
    76            container
    77            sx={{
    78              display: "flex",
    79              flexDirection: "column",
    80              alignItems: "center",
    81              justifyItems: "start",
    82              fontSize: "8px",
    83            }}
    84          >
    85            <ConsoleIcon style={{ fill: "#052F51", width: "14px" }} />
    86            {overrideEnabled ? "Enabled" : "Disabled"}
    87          </Grid>
    88        );
    89      }
    90  
    91      // If enable is not set, then enabled by default
    92      if (!EnableFilter || EnableFilter.value === "on" || !EnableFilter.value) {
    93        return (
    94          <Grid
    95            container
    96            sx={{
    97              display: "flex",
    98              flexDirection: "column",
    99              alignItems: "center",
   100              justifyItems: "start",
   101              fontSize: "8px",
   102            }}
   103          >
   104            <TierOnlineIcon style={{ fill: "#4CCB92", width: 14, height: 14 }} />
   105            Enabled
   106          </Grid>
   107        );
   108      }
   109  
   110      return (
   111        <Grid
   112          container
   113          sx={{
   114            display: "flex",
   115            flexDirection: "column",
   116            alignItems: "center",
   117            justifyItems: "start",
   118            fontSize: "8px",
   119          }}
   120        >
   121          <TierOfflineIcon style={{ fill: "#C83B51", width: 14, height: 14 }} />
   122          Disabled
   123        </Grid>
   124      );
   125    };
   126  
   127    const onCloseDelete = () => {
   128      setDeleteWebhookOpen(false);
   129      setSelectedARN("");
   130    };
   131  
   132    const onCloseEditWebhook = () => {
   133      setEditWebhookOpen(false);
   134      setSelectedEndpoint(null);
   135    };
   136  
   137    const actions = [
   138      {
   139        type: "view",
   140        onClick: (item: IConfigurationSys) => {
   141          if (item.name) {
   142            setEditWebhookOpen(true);
   143            setSelectedEndpoint(item);
   144          }
   145        },
   146      },
   147      {
   148        type: "delete",
   149        onClick: (item: IConfigurationSys) => {
   150          if (item.name) {
   151            setDeleteWebhookOpen(true);
   152            setSelectedARN(item.name);
   153          }
   154        },
   155        disableButtonFunction: (item: string) => {
   156          const wHook = WebhookSettingslist.find(
   157            (element) => element.name === item,
   158          );
   159  
   160          if (wHook) {
   161            const hasOverride = wHook.key_values?.filter(
   162              (itm) => !!itm.env_override,
   163            );
   164  
   165            // Has override values, we cannot delete.
   166            if (hasOverride && hasOverride.length > 0) {
   167              return true;
   168            }
   169  
   170            return false;
   171          }
   172          return false;
   173        },
   174      },
   175    ];
   176    return (
   177      <Grid container>
   178        {newEndpointOpen && (
   179          <AddEndpointModal
   180            open={newEndpointOpen}
   181            type={type}
   182            onCloseEndpoint={() => {
   183              setNewEndpointOpen(false);
   184            }}
   185          />
   186        )}
   187        {deleteWebhookOpen && (
   188          <DeleteWebhookEndpoint
   189            modalOpen={deleteWebhookOpen}
   190            onClose={onCloseDelete}
   191            selectedARN={selectedARN}
   192            type={type}
   193          />
   194        )}
   195        {editWebhookOpen && selectedEndpoint && (
   196          <EditWebhookEndpoint
   197            open={editWebhookOpen}
   198            type={type}
   199            endpointInfo={selectedEndpoint}
   200            onCloseEndpoint={onCloseEditWebhook}
   201          />
   202        )}
   203        <Grid item xs={12} sx={{ display: "flex", justifyContent: "flex-end" }}>
   204          <Button
   205            id={"newWebhook"}
   206            variant="callAction"
   207            onClick={() => {
   208              setNewEndpointOpen(true);
   209            }}
   210          >
   211            New Endpoint
   212          </Button>
   213        </Grid>
   214        <Grid item xs={12} sx={{ padding: "0 10px 10px" }}>
   215          <Fragment>
   216            <h3>Currently Configured Endpoints</h3>
   217            <DataTable
   218              columns={[
   219                {
   220                  label: "Status",
   221                  elementKey: "key_values",
   222                  renderFunction: renderWebhookStatus,
   223                  width: 50,
   224                },
   225                { label: "Name", elementKey: "name" },
   226                {
   227                  label: "Endpoint",
   228                  elementKey: "key_values",
   229                  renderFunction: renderEndpoint,
   230                },
   231              ]}
   232              itemActions={actions}
   233              idField="name"
   234              isLoading={false}
   235              records={WebhookSettingslist}
   236              entityName="endpoints"
   237              customPaperHeight={"calc(100vh - 750px)"}
   238            />
   239          </Fragment>
   240        </Grid>
   241      </Grid>
   242    );
   243  };
   244  export default WebhookSettings;