1- import { Fragment } from 'react' ;
1+ import { Fragment , useState , useEffect } from 'react' ;
22
33import Chip from '@material-ui/core/Chip' ;
44import FormControlLabel from '@material-ui/core/FormControlLabel' ;
@@ -9,6 +9,7 @@ import Tooltip from '@material-ui/core/Tooltip';
99import Typography from '@material-ui/core/Typography' ;
1010import AccessibilityIcon from '@material-ui/icons/Accessibility' ;
1111import ArrowForwardIos from '@material-ui/icons/ArrowForwardIos' ;
12+ import CheckIcon from '@material-ui/icons/Check' ;
1213import CloudDownloadIcon from '@material-ui/icons/CloudDownload' ;
1314import CodeIcon from '@material-ui/icons/Code' ;
1415import DeleteForever from '@material-ui/icons/DeleteForever' ;
@@ -21,8 +22,29 @@ import withUI from '../../hoc/withUI';
2122import ExternalIdLink from '../externalid/ExternalIdLink' ;
2223
2324import CodeModal from './CodeModal' ;
25+ import CopyIcon from './CopyIcon' ;
2426import Menu , { MenuItem } from './Menu' ;
2527
28+ function CopyCodeIcon ( { code } ) {
29+ const [ isCopied , setIsCopied ] = useState ( false ) ;
30+ const onClick = ( ) => {
31+ navigator . clipboard . writeText ( typeof code === 'object' ? JSON . stringify ( code , null , 2 ) : code ) ;
32+ setIsCopied ( true ) ;
33+ } ;
34+ useEffect ( ( ) => {
35+ if ( isCopied === false ) return undefined ;
36+ const timerId = setTimeout ( ( ) => setIsCopied ( false ) , 3000 ) ; // 2-second delay
37+ return ( ) => {
38+ clearTimeout ( timerId ) ;
39+ } ;
40+ } , [ isCopied ] ) ;
41+ return (
42+ < Tooltip title = { isCopied === true ? 'Copied' : 'Copy to clipboard' } >
43+ < IconButton onClick = { onClick } > { isCopied ? < CheckIcon /> : < CopyIcon /> } </ IconButton >
44+ </ Tooltip >
45+ ) ;
46+ }
47+
2648function TitleMenu ( { menuList = [ ] , onOpen } ) {
2749 if ( ! Array . isArray ( menuList ) || menuList . length < 1 ) return null ;
2850 return (
@@ -208,6 +230,10 @@ function TitleHeader({
208230 </ Tooltip >
209231 ) ;
210232 }
233+ let copyCodeComponent ;
234+ if ( code ) {
235+ copyCodeComponent = < CopyCodeIcon code = { code } /> ;
236+ }
211237 const autoRefreshSwitch = onChangeAutoRefresh && (
212238 < FormControlLabel
213239 control = { < Switch checked = { autoRefresh } onChange = { onChangeAutoRefresh } /> }
@@ -297,6 +323,7 @@ function TitleHeader({
297323 { openAddAccess }
298324 { openExternalId }
299325 { openCodeComponent }
326+ { copyCodeComponent }
300327 { refeshAction }
301328 { iconList }
302329 { action }
0 commit comments