|
| 1 | +import React, { useCallback, useState } from 'react'; |
| 2 | +import { basename } from 'path'; |
| 3 | +import { useOvermind } from 'app/overmind'; |
| 4 | +import Media from 'react-media'; |
| 5 | + |
| 6 | +import { Spring } from 'react-spring/renderprops'; |
| 7 | + |
| 8 | +import track from '@codesandbox/common/lib/utils/analytics'; |
| 9 | +import { ESC } from '@codesandbox/common/lib/utils/keycodes'; |
| 10 | +import { getSandboxName } from '@codesandbox/common/lib/utils/get-sandbox-name'; |
| 11 | +import { Sandbox } from '@codesandbox/common/lib/types'; |
| 12 | +import { |
| 13 | + Container, |
| 14 | + SandboxName, |
| 15 | + SandboxForm, |
| 16 | + SandboxInput, |
| 17 | + FolderName, |
| 18 | +} from './elements'; |
| 19 | + |
| 20 | +interface ICollectionInfoProps { |
| 21 | + sandbox: Sandbox; |
| 22 | + isLoggedIn: boolean; |
| 23 | +} |
| 24 | + |
| 25 | +const CollectionInfo: React.FC<ICollectionInfoProps> = ({ |
| 26 | + sandbox, |
| 27 | + isLoggedIn, |
| 28 | +}) => { |
| 29 | + const [updatingName, setUpdatingName] = useState(false); |
| 30 | + const [nameValue, setNameValue] = useState(''); |
| 31 | + const { |
| 32 | + actions: { |
| 33 | + workspace: { sandboxInfoUpdated, valueChanged }, |
| 34 | + modalOpened, |
| 35 | + }, |
| 36 | + } = useOvermind(); |
| 37 | + |
| 38 | + const sandboxName = useCallback(() => getSandboxName(sandbox) || 'Untitled', [ |
| 39 | + sandbox, |
| 40 | + ]); |
| 41 | + |
| 42 | + const updateSandboxInfo = useCallback(() => { |
| 43 | + sandboxInfoUpdated(); |
| 44 | + setUpdatingName(false); |
| 45 | + }, [sandboxInfoUpdated]); |
| 46 | + |
| 47 | + const submitNameChange = useCallback( |
| 48 | + e => { |
| 49 | + e.preventDefault(); |
| 50 | + updateSandboxInfo(); |
| 51 | + |
| 52 | + track('Change Sandbox Name From Header'); |
| 53 | + }, |
| 54 | + [updateSandboxInfo] |
| 55 | + ); |
| 56 | + |
| 57 | + const handleNameClick = useCallback(() => { |
| 58 | + setUpdatingName(true); |
| 59 | + setNameValue(sandboxName()); |
| 60 | + }, [sandboxName]); |
| 61 | + |
| 62 | + const handleKeyUp = useCallback( |
| 63 | + e => { |
| 64 | + if (e.keyCode === ESC) { |
| 65 | + updateSandboxInfo(); |
| 66 | + } |
| 67 | + }, |
| 68 | + [updateSandboxInfo] |
| 69 | + ); |
| 70 | + |
| 71 | + const handleBlur = useCallback(() => { |
| 72 | + updateSandboxInfo(); |
| 73 | + }, [updateSandboxInfo]); |
| 74 | + |
| 75 | + const handleInputUpdate = useCallback( |
| 76 | + e => { |
| 77 | + valueChanged({ |
| 78 | + field: 'title', |
| 79 | + value: e.target.value, |
| 80 | + }); |
| 81 | + |
| 82 | + setNameValue(e.target.value); |
| 83 | + }, |
| 84 | + [valueChanged] |
| 85 | + ); |
| 86 | + |
| 87 | + const value = nameValue !== 'Untitled' && updatingName ? nameValue : ''; |
| 88 | + |
| 89 | + const folderName = sandbox.collection |
| 90 | + ? basename(sandbox.collection.path) || |
| 91 | + (sandbox.team ? sandbox.team.name : 'My Sandboxes') |
| 92 | + : 'My Sandboxes'; |
| 93 | + |
| 94 | + return ( |
| 95 | + <Spring<{ opacity: number; pointerEvents: 'none' | 'initial' }> |
| 96 | + from={{ |
| 97 | + opacity: 1, |
| 98 | + }} |
| 99 | + to={ |
| 100 | + updatingName |
| 101 | + ? { |
| 102 | + opacity: 0, |
| 103 | + pointerEvents: 'none', |
| 104 | + } |
| 105 | + : { |
| 106 | + opacity: 1, |
| 107 | + pointerEvents: 'initial', |
| 108 | + } |
| 109 | + } |
| 110 | + > |
| 111 | + {style => ( |
| 112 | + <Media |
| 113 | + query="(min-width: 826px)" |
| 114 | + render={() => ( |
| 115 | + <Container> |
| 116 | + <Media |
| 117 | + query="(min-width: 950px)" |
| 118 | + render={() => ( |
| 119 | + <div |
| 120 | + style={{ |
| 121 | + ...style, |
| 122 | + overflow: 'hidden', |
| 123 | + }} |
| 124 | + > |
| 125 | + {isLoggedIn ? ( |
| 126 | + <FolderName |
| 127 | + onClick={() => { |
| 128 | + modalOpened({ |
| 129 | + modal: 'moveSandbox', |
| 130 | + }); |
| 131 | + }} |
| 132 | + > |
| 133 | + {folderName} |
| 134 | + </FolderName> |
| 135 | + ) : ( |
| 136 | + 'Anonymous ' |
| 137 | + )} |
| 138 | + /{' '} |
| 139 | + </div> |
| 140 | + )} |
| 141 | + /> |
| 142 | + {updatingName ? ( |
| 143 | + <SandboxForm onSubmit={submitNameChange}> |
| 144 | + <SandboxInput |
| 145 | + autoFocus |
| 146 | + innerRef={el => { |
| 147 | + if (el) { |
| 148 | + el.focus(); |
| 149 | + } |
| 150 | + }} |
| 151 | + onKeyUp={handleKeyUp} |
| 152 | + onBlur={handleBlur} |
| 153 | + onChange={handleInputUpdate} |
| 154 | + placeholder={nameValue} |
| 155 | + value={value} |
| 156 | + /> |
| 157 | + </SandboxForm> |
| 158 | + ) : ( |
| 159 | + <SandboxName onClick={handleNameClick}> |
| 160 | + {sandboxName()} |
| 161 | + </SandboxName> |
| 162 | + )} |
| 163 | + </Container> |
| 164 | + )} |
| 165 | + /> |
| 166 | + )} |
| 167 | + </Spring> |
| 168 | + ); |
| 169 | +}; |
| 170 | + |
| 171 | +export { CollectionInfo }; |
0 commit comments