|
| 1 | +import React, { useState, useRef, useEffect } from 'react'; |
| 2 | +import { uniqueNamesGenerator, adjectives, colors, } from 'unique-names-generator'; |
| 3 | +import { FaSyncAlt } from 'react-icons/fa'; |
| 4 | +import { useProject } from 'providers/Project/projectHooks'; |
| 5 | +import { default as FlowButton } from 'components/Button'; |
| 6 | + |
| 7 | +import { |
| 8 | + FullScreenContainer, |
| 9 | + PopupContainer, |
| 10 | + PopupHeader, |
| 11 | + WhiteOverlay, |
| 12 | + SpaceBetween, |
| 13 | +} from 'components/Common'; |
| 14 | + |
| 15 | +import { createZip } from '../util/generator'; |
| 16 | + |
| 17 | +import { |
| 18 | + Input, |
| 19 | + InputBlock, InputIcon, |
| 20 | + Label, |
| 21 | +} from 'components/Arguments/SingleArgument/styles'; |
| 22 | + |
| 23 | +const generateProjectName = () => { |
| 24 | + const prefix: string = uniqueNamesGenerator({ |
| 25 | + dictionaries: [colors, adjectives], |
| 26 | + separator: '-', |
| 27 | + length: 2, |
| 28 | + }) |
| 29 | + |
| 30 | + return `${prefix}-playground` |
| 31 | +} |
| 32 | + |
| 33 | +const AutoTemplatePopup: React.FC<{ |
| 34 | + visible: boolean; |
| 35 | + triggerClose?: (e: React.SyntheticEvent) => any; |
| 36 | +}> = ({ visible, triggerClose }) => { |
| 37 | + const { project } = useProject(); |
| 38 | + const [processing, setProcessing] = useState(false); |
| 39 | + const [projectName, setProjectName] = useState(generateProjectName()); |
| 40 | + const [folderName, setFolderName] = useState('cadence'); |
| 41 | + |
| 42 | + const regenerateProjectName = () => { |
| 43 | + const newName = generateProjectName() |
| 44 | + setProjectName(newName) |
| 45 | + } |
| 46 | + |
| 47 | + const firstInput = useRef<HTMLInputElement>(null!); |
| 48 | + |
| 49 | + useEffect(() => { |
| 50 | + firstInput.current.focus(); |
| 51 | + }, [firstInput.current]); |
| 52 | + |
| 53 | + const containerFrames = { |
| 54 | + visible: { |
| 55 | + display: 'flex', |
| 56 | + opacity: 1, |
| 57 | + transition: { |
| 58 | + staggerChildren: 0.1, |
| 59 | + }, |
| 60 | + zIndex: 20, |
| 61 | + }, |
| 62 | + hidden: { |
| 63 | + opacity: 0, |
| 64 | + transition: { |
| 65 | + when: 'afterChildren', |
| 66 | + staggerChildren: 0, |
| 67 | + staggerDirection: -1, |
| 68 | + }, |
| 69 | + zIndex: -1, |
| 70 | + }, |
| 71 | + }; |
| 72 | + |
| 73 | + const spring = { |
| 74 | + type: 'spring', |
| 75 | + damping: 11, |
| 76 | + stiffness: 120, |
| 77 | + }; |
| 78 | + |
| 79 | + const popupFrames = { |
| 80 | + visible: { |
| 81 | + opacity: 1, |
| 82 | + y: 0, |
| 83 | + transition: spring, |
| 84 | + }, |
| 85 | + hidden: { |
| 86 | + opacity: 0, |
| 87 | + y: -200, |
| 88 | + transition: { |
| 89 | + ease: [1, 0.5, 0, 0] |
| 90 | + }, |
| 91 | + }, |
| 92 | + }; |
| 93 | + |
| 94 | + return ( |
| 95 | + <FullScreenContainer |
| 96 | + elevation={15} |
| 97 | + initial="hidden" |
| 98 | + animate={visible ? 'visible' : 'hidden'} |
| 99 | + variants={containerFrames} |
| 100 | + > |
| 101 | + <PopupContainer width="350px" variants={popupFrames}> |
| 102 | + <PopupHeader mb="20px" color="#575E89" lineColor="#B4BEFC"> |
| 103 | + Export Project |
| 104 | + </PopupHeader> |
| 105 | + <InputBlock mb={'12px'}> |
| 106 | + <Label>Project Name</Label> |
| 107 | + <Input |
| 108 | + ref={firstInput} |
| 109 | + value={projectName} |
| 110 | + onChange={event => setProjectName(event.target.value)} |
| 111 | + /> |
| 112 | + <InputIcon icon={<FaSyncAlt/>} onClick={regenerateProjectName}/> |
| 113 | + </InputBlock> |
| 114 | + <InputBlock mb={'30px'}> |
| 115 | + <Label>Cadence Folder</Label> |
| 116 | + <Input |
| 117 | + value={folderName} |
| 118 | + onChange={event => setFolderName(event.target.value)} |
| 119 | + /> |
| 120 | + </InputBlock> |
| 121 | + {processing ? ( |
| 122 | + <p>Processing...</p> |
| 123 | + ) : ( |
| 124 | + <SpaceBetween> |
| 125 | + <FlowButton className="grey modal" onClick={triggerClose}> |
| 126 | + Close |
| 127 | + </FlowButton> |
| 128 | + <FlowButton |
| 129 | + className="violet modal" |
| 130 | + onClick={async () => { |
| 131 | + setProcessing(true); |
| 132 | + await createZip(folderName, projectName, project); |
| 133 | + setProcessing(false); |
| 134 | + triggerClose(null); |
| 135 | + }} |
| 136 | + > |
| 137 | + Export |
| 138 | + </FlowButton> |
| 139 | + </SpaceBetween> |
| 140 | + )} |
| 141 | + </PopupContainer> |
| 142 | + <WhiteOverlay onClick={triggerClose} /> |
| 143 | + </FullScreenContainer> |
| 144 | + ); |
| 145 | +}; |
| 146 | + |
| 147 | +export default AutoTemplatePopup; |
0 commit comments