|
| 1 | +import Popper from '@material-ui/core/Popper'; |
| 2 | +import Paper from '@material-ui/core/Paper'; |
| 3 | +import { |
| 4 | + CheckIcon, |
| 5 | + ChevronDownIcon, |
| 6 | + CodeIcon, |
| 7 | + CopyIcon, |
| 8 | + TerminalIcon, |
| 9 | +} from '@primer/octicons-react'; |
| 10 | +import React, { useState } from 'react'; |
| 11 | + |
| 12 | +const CodeActionButton = ({ cloneURL }) => { |
| 13 | + const [anchorEl, setAnchorEl] = useState(null); |
| 14 | + const [open, setOpen] = useState(false); |
| 15 | + const [placement, setPlacement] = useState(); |
| 16 | + const [isCopied, setIsCopied] = useState(false); |
| 17 | + |
| 18 | + const handleClick = (newPlacement) => (event) => { |
| 19 | + setIsCopied(false); |
| 20 | + setAnchorEl(event.currentTarget); |
| 21 | + setOpen((prev) => placement !== newPlacement || !prev); |
| 22 | + setPlacement(newPlacement); |
| 23 | + }; |
| 24 | + |
| 25 | + return ( |
| 26 | + <> |
| 27 | + <span |
| 28 | + style={{ |
| 29 | + background: '#2da44e', |
| 30 | + borderRadius: '5px', |
| 31 | + color: 'white', |
| 32 | + padding: '8px 10px 8px 10px', |
| 33 | + fontWeight: 'bold', |
| 34 | + cursor: 'pointer', |
| 35 | + border: '1px solid rgba(240,246,252,0.1)', |
| 36 | + whiteSpace: 'nowrap', |
| 37 | + }} |
| 38 | + onClick={handleClick('bottom-end')} |
| 39 | + > |
| 40 | + <CodeIcon size='small' verticalAlign='middle' />{' '} |
| 41 | + <span style={{ paddingLeft: '6px', paddingRight: '10px' }}>Code</span> |
| 42 | + <ChevronDownIcon size='small' verticalAlign='text-top' /> |
| 43 | + </span> |
| 44 | + <Popper |
| 45 | + open={open} |
| 46 | + anchorEl={anchorEl} |
| 47 | + placement={placement} |
| 48 | + style={{ |
| 49 | + border: '1px solid rgba(211, 211, 211, 0.3)', |
| 50 | + borderRadius: '5px', |
| 51 | + minWidth: '300px', |
| 52 | + maxWidth: '450px', |
| 53 | + zIndex: 99, |
| 54 | + }} |
| 55 | + > |
| 56 | + <Paper> |
| 57 | + <div style={{ padding: '15px', gap: '5px' }}> |
| 58 | + <TerminalIcon size='small' verticalAlign='middle' />{' '} |
| 59 | + <span style={{ paddingLeft: '5px', fontSize: '14px', fontWeight: 'bold' }}>Clone</span> |
| 60 | + <div style={{ marginTop: '5px', maxWidth: '299px' }}> |
| 61 | + <div |
| 62 | + style={{ |
| 63 | + padding: '3px 8px 3px 8px', |
| 64 | + border: '1px solid gray', |
| 65 | + borderRadius: '5px', |
| 66 | + fontSize: '12px', |
| 67 | + minHeight: '22px', |
| 68 | + }} |
| 69 | + > |
| 70 | + <span |
| 71 | + style={{ |
| 72 | + float: 'left', |
| 73 | + overflow: 'hidden', |
| 74 | + textOverflow: 'ellipsis', |
| 75 | + whiteSpace: 'nowrap', |
| 76 | + width: '90%', |
| 77 | + }} |
| 78 | + > |
| 79 | + {cloneURL} |
| 80 | + </span> |
| 81 | + <span |
| 82 | + style={{ |
| 83 | + float: 'right', |
| 84 | + }} |
| 85 | + > |
| 86 | + {!isCopied && ( |
| 87 | + <span |
| 88 | + style={{ cursor: 'pointer' }} |
| 89 | + onClick={() => { |
| 90 | + navigator.clipboard.writeText(`git clone ${cloneURL}`); |
| 91 | + setIsCopied(true); |
| 92 | + }} |
| 93 | + > |
| 94 | + <CopyIcon /> |
| 95 | + </span> |
| 96 | + )} |
| 97 | + {isCopied && ( |
| 98 | + <span style={{ color: 'green' }}> |
| 99 | + <CheckIcon /> |
| 100 | + </span> |
| 101 | + )} |
| 102 | + </span> |
| 103 | + </div> |
| 104 | + </div> |
| 105 | + <div style={{ marginTop: '5px' }}> |
| 106 | + <span style={{ fontWeight: 'lighter', fontSize: '12px', opacity: 0.9 }}> |
| 107 | + Use Git and run this command in your IDE or Terminal 👍 |
| 108 | + </span> |
| 109 | + </div> |
| 110 | + </div> |
| 111 | + </Paper> |
| 112 | + </Popper> |
| 113 | + </> |
| 114 | + ); |
| 115 | +}; |
| 116 | + |
| 117 | +export default CodeActionButton; |
0 commit comments