|
| 1 | +import React, { useState } from "react"; |
| 2 | +import PropTypes from "prop-types"; |
| 3 | +import { useTranslation } from "react-i18next"; |
| 4 | +import { |
| 5 | + CustomProgressBar, |
| 6 | + SelectDropdown, |
| 7 | + V8CustomButton, |
| 8 | +} from "@formsflow/components"; |
| 9 | +// import { StyleServices } from "@formsflow/service"; |
| 10 | + |
| 11 | +// A lightweight, inline Actions panel rendered as a tab instead of a modal. |
| 12 | +const ProcessActionsTab = ({ |
| 13 | + newActionModal, |
| 14 | + renderUpload, |
| 15 | + onExport, |
| 16 | + diagramType, |
| 17 | +}) => { |
| 18 | + const { t } = useTranslation(); |
| 19 | + // const primaryColor = StyleServices.getCSSVariable("--ff-primary"); |
| 20 | + |
| 21 | + // Get diagram-specific labels |
| 22 | + const diagramLabel = diagramType || "Process"; |
| 23 | + |
| 24 | + // State for export functionality |
| 25 | + const [selectedValue, setSelectedValue] = useState("XML"); |
| 26 | + const [progress, setProgress] = useState(0); |
| 27 | + const [isError, setIsError] = useState(false); |
| 28 | + const [isExporting, setIsExporting] = useState(false); |
| 29 | + |
| 30 | + // Export format options based on diagram type |
| 31 | + const formExportOptions = [{ label: diagramLabel, value: "XML" }]; |
| 32 | + |
| 33 | + if (!newActionModal) return null; |
| 34 | + |
| 35 | + const handleSelectChange = (value) => { |
| 36 | + setSelectedValue(value); |
| 37 | + }; |
| 38 | + |
| 39 | + const handleExportClick = async () => { |
| 40 | + if (!onExport) return; |
| 41 | + |
| 42 | + setIsExporting(true); |
| 43 | + setProgress(0); |
| 44 | + setIsError(false); |
| 45 | + |
| 46 | + try { |
| 47 | + // Simulate progress |
| 48 | + const progressInterval = setInterval(() => { |
| 49 | + setProgress((prev) => { |
| 50 | + if (prev >= 90) { |
| 51 | + clearInterval(progressInterval); |
| 52 | + return 90; |
| 53 | + } |
| 54 | + return prev + 10; |
| 55 | + }); |
| 56 | + }, 200); |
| 57 | + |
| 58 | + // Call the actual export function |
| 59 | + await onExport(selectedValue); |
| 60 | + |
| 61 | + // Complete progress |
| 62 | + clearInterval(progressInterval); |
| 63 | + setProgress(100); |
| 64 | + |
| 65 | + // Reset after success |
| 66 | + setTimeout(() => { |
| 67 | + setProgress(0); |
| 68 | + setIsExporting(false); |
| 69 | + }, 1000); |
| 70 | + } catch (error) { |
| 71 | + setIsError(true); |
| 72 | + setProgress(0); |
| 73 | + setIsExporting(false); |
| 74 | + } |
| 75 | + }; |
| 76 | + |
| 77 | + return ( |
| 78 | + <div className="process-actions-tab" data-testid="process-actions-tab"> |
| 79 | + <div className="grid-section"> |
| 80 | + <div className="actions-header">{t(`Import ${diagramLabel}`)}</div> |
| 81 | + <div className="upload-action-content"> |
| 82 | + {renderUpload && renderUpload()} |
| 83 | + </div> |
| 84 | + </div> |
| 85 | + <div className="grid-section"> |
| 86 | + <div className="actions-header">{t(`Export ${diagramLabel}`)}</div> |
| 87 | + <div className="actions-contents"> |
| 88 | + <div className="export-section"> |
| 89 | + <div className="export-dropdown-section"> |
| 90 | + <SelectDropdown |
| 91 | + options={formExportOptions} |
| 92 | + value={selectedValue} |
| 93 | + onChange={handleSelectChange} |
| 94 | + searchDropdown={false} |
| 95 | + ariaLabel={t("Select export format")} |
| 96 | + dataTestId="form-export-dropdown" |
| 97 | + variant="primary" |
| 98 | + /> |
| 99 | + <V8CustomButton |
| 100 | + variant="primary" |
| 101 | + onClick={handleExportClick} |
| 102 | + data-testid="form-export-btn" |
| 103 | + aria-label={t(`Export ${diagramLabel}`)} |
| 104 | + label={t("Export")} |
| 105 | + disabled={isExporting} |
| 106 | + loading={isExporting} |
| 107 | + /> |
| 108 | + </div> |
| 109 | + {isExporting && ( |
| 110 | + <div className="export-progress-section"> |
| 111 | + <div> |
| 112 | + {t("Export process as")} {selectedValue} |
| 113 | + </div> |
| 114 | + <div className="export-progress"> |
| 115 | + <CustomProgressBar |
| 116 | + progress={progress} |
| 117 | + color={isError ? "error" : undefined} |
| 118 | + /> |
| 119 | + </div> |
| 120 | + </div> |
| 121 | + )} |
| 122 | + </div> |
| 123 | + </div> |
| 124 | + </div> |
| 125 | + </div> |
| 126 | + ); |
| 127 | +}; |
| 128 | + |
| 129 | +ProcessActionsTab.propTypes = { |
| 130 | + newActionModal: PropTypes.bool.isRequired, |
| 131 | + diagramType: PropTypes.string, |
| 132 | + renderUpload: PropTypes.func, |
| 133 | + onExport: PropTypes.func, |
| 134 | +}; |
| 135 | + |
| 136 | +export default ProcessActionsTab; |
0 commit comments