{"version":3,"file":"CopyToClipboard.mjs","sources":["../../../src/lib/components/CopyToClipboard/CopyToClipboard.jsx"],"sourcesContent":["/*\nCopyright 2019 Iguazio Systems Ltd.\n\nLicensed under the Apache License, Version 2.0 (the \"License\") with\nan addition restriction as set forth herein. You may not use this\nfile except in compliance with the License. You may obtain a copy of\nthe License at http://www.apache.org/licenses/LICENSE-2.0.\n\nUnless required by applicable law or agreed to in writing, software\ndistributed under the License is distributed on an \"AS IS\" BASIS,\nWITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or\nimplied. See the License for the specific language governing\npermissions and limitations under the License.\n\nIn addition, you may not use the software for any purposes that are\nillegal under applicable law, and the grant of the foregoing license\nunder the Apache 2.0 license is conditioned upon your compliance with\nsuch restriction.\n*/\nimport PropTypes from 'prop-types'\nimport { useMemo } from 'react'\nimport { useDispatch } from 'react-redux'\n\nimport RoundedIcon from '../RoundedIcon/RoundedIcon'\nimport TextTooltipTemplate from '../TooltipTemplate/TextTooltipTemplate'\nimport Tooltip from '../Tooltip/Tooltip'\n\nimport { setNotification } from '../../reducers/notificationReducer'\nimport { showErrorNotification } from '../../utils/notification.util'\n\nimport Copy from '../../images/copy-to-clipboard-icon.svg?react'\n\nconst CopyToClipboard = ({\n  children = null,\n  className = '',\n  disabled = false,\n  textToCopy = '',\n  tooltipText\n}) => {\n  const dispatch = useDispatch()\n  const copyIsDisabled = useMemo(() => disabled || !textToCopy, [disabled, textToCopy])\n\n  const copyToClipboard = textToCopy => {\n    navigator.clipboard\n      .writeText(textToCopy)\n      .then(() => {\n        dispatch(\n          setNotification({\n            status: 200,\n            id: Math.random(),\n            message: 'Copied to clipboard successfully'\n          })\n        )\n      })\n      .catch(error => {\n        showErrorNotification(dispatch, error, '', 'Copy to clipboard failed')\n      })\n  }\n\n  return (\n    <div className={className} data-testid=\"copy-to-clipboard\">\n      {children ? (\n        <Tooltip template={<TextTooltipTemplate text={tooltipText} />} textShow>\n          <span onClick={() => copyToClipboard(textToCopy)}>{children}</span>\n        </Tooltip>\n      ) : (\n        <RoundedIcon\n          tooltipText={tooltipText}\n          onClick={() => copyToClipboard(textToCopy)}\n          disabled={copyIsDisabled}\n        >\n          <Copy />\n        </RoundedIcon>\n      )}\n    </div>\n  )\n}\n\nCopyToClipboard.propTypes = {\n  children: PropTypes.oneOfType([PropTypes.string, PropTypes.array, PropTypes.element]),\n  className: PropTypes.string,\n  disabled: PropTypes.bool,\n  textToCopy: PropTypes.string,\n  tooltipText: PropTypes.string.isRequired\n}\n\nexport default CopyToClipboard\n"],"names":["CopyToClipboard","children","className","disabled","textToCopy","tooltipText","dispatch","useDispatch","copyIsDisabled","useMemo","copyToClipboard","setNotification","error","showErrorNotification","jsx","Tooltip","TextTooltipTemplate","RoundedIcon","Copy","PropTypes"],"mappings":";;;;;;;;;;AAgCA,MAAMA,IAAkB,CAAC;AAAA,EACvB,UAAAC,IAAW;AAAA,EACX,WAAAC,IAAY;AAAA,EACZ,UAAAC,IAAW;AAAA,EACX,YAAAC,IAAa;AAAA,EACb,aAAAC;AACF,MAAM;AACJ,QAAMC,IAAWC,EAAA,GACXC,IAAiBC,EAAQ,MAAMN,KAAY,CAACC,GAAY,CAACD,GAAUC,CAAU,CAAC,GAE9EM,IAAkB,CAAAN,MAAc;AACpC,cAAU,UACP,UAAUA,CAAU,EACpB,KAAK,MAAM;AACV,MAAAE;AAAA,QACEK,EAAgB;AAAA,UACd,QAAQ;AAAA,UACR,IAAI,KAAK,OAAA;AAAA,UACT,SAAS;AAAA,QAAA,CACV;AAAA,MAAA;AAAA,IAEL,CAAC,EACA,MAAM,CAAAC,MAAS;AACd,MAAAC,EAAsBP,GAAUM,GAAO,IAAI,0BAA0B;AAAA,IACvE,CAAC;AAAA,EACL;AAEA,SACE,gBAAAE,EAAC,OAAA,EAAI,WAAAZ,GAAsB,eAAY,qBACpC,cACC,gBAAAY,EAACC,GAAA,EAAQ,UAAU,gBAAAD,EAACE,GAAA,EAAoB,MAAMX,GAAa,GAAI,UAAQ,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