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

     1  // This file is part of MinIO Console Server
     2  // Copyright (c) 2021 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 from "react";
    18  import { ProgressBar, ProgressBarProps } from "mds";
    19  
    20  interface IProgressBarWrapper {
    21    value: number;
    22    ready: boolean;
    23    indeterminate?: boolean;
    24    withLabel?: boolean;
    25    size?: string;
    26    error?: boolean;
    27    cancelled?: boolean;
    28    notificationLabel?: string;
    29  }
    30  
    31  function LinearProgressWithLabel(
    32    props: { error: boolean; cancelled: boolean } & ProgressBarProps,
    33  ) {
    34    let label = "";
    35  
    36    if (props.error) {
    37      label = `Error: ${props.notificationLabel || ""}`;
    38    } else if (props.cancelled) {
    39      label = `Cancelled`;
    40    }
    41  
    42    return (
    43      <ProgressBar
    44        variant={"determinate"}
    45        value={props.value}
    46        color={props.color}
    47        progressLabel
    48        notificationLabel={label}
    49      />
    50    );
    51  }
    52  
    53  const ProgressBarWrapper = ({
    54    value,
    55    ready,
    56    indeterminate,
    57    withLabel,
    58    size = "regular",
    59    error,
    60    cancelled,
    61    notificationLabel,
    62  }: IProgressBarWrapper) => {
    63    let color: any;
    64    if (error) {
    65      color = "red";
    66    } else if (cancelled) {
    67      color = "orange";
    68    } else if (value === 100 && ready) {
    69      color = "green";
    70    } else {
    71      color = "blue";
    72    }
    73    const propsComponent: ProgressBarProps = {
    74      variant:
    75        indeterminate && !ready && !cancelled ? "indeterminate" : "determinate",
    76      value: ready && !error ? 100 : value,
    77      color: color,
    78      notificationLabel: notificationLabel || "",
    79    };
    80    if (withLabel) {
    81      return (
    82        <LinearProgressWithLabel
    83          {...propsComponent}
    84          error={!!error}
    85          cancelled={!!cancelled}
    86        />
    87      );
    88    }
    89    if (size === "small") {
    90      return (
    91        <ProgressBar {...propsComponent} sx={{ height: 6, borderRadius: 6 }} />
    92      );
    93    }
    94  
    95    return <ProgressBar {...propsComponent} />;
    96  };
    97  
    98  export default ProgressBarWrapper;