|
1 | | -import React, { useState } from 'react'; |
2 | | -import { inject, hooksObserver } from 'app/componentConnectors'; |
| 1 | +import { ENTER } from '@codesandbox/common/lib/utils/keycodes'; |
| 2 | +import React, { FunctionComponent, useState } from 'react'; |
| 3 | + |
| 4 | +import { useOvermind } from 'app/overmind'; |
3 | 5 |
|
4 | 6 | import { WorkspaceInputContainer } from '../../elements'; |
| 7 | + |
5 | 8 | import { EditPen } from '../elements'; |
| 9 | + |
6 | 10 | import { SandboxDescription } from './elements'; |
7 | 11 |
|
8 | | -interface IDescriptionProps { |
| 12 | +type Props = { |
9 | 13 | editable: boolean; |
10 | | - store: any; |
11 | | - signals: any; |
12 | | -} |
13 | | - |
14 | | -export const Description = inject('store', 'signals')( |
15 | | - hooksObserver( |
16 | | - ({ |
17 | | - editable, |
18 | | - signals: { |
19 | | - workspace: { sandboxInfoUpdated, valueChanged }, |
| 14 | +}; |
| 15 | +export const Description: FunctionComponent<Props> = ({ editable }) => { |
| 16 | + const { |
| 17 | + actions: { |
| 18 | + workspace: { sandboxInfoUpdated, valueChanged }, |
| 19 | + }, |
| 20 | + state: { |
| 21 | + workspace: { |
| 22 | + project: { description }, |
20 | 23 | }, |
21 | | - store: { |
22 | | - workspace: { |
23 | | - project: { description }, |
24 | | - }, |
25 | | - }, |
26 | | - }: IDescriptionProps) => { |
27 | | - const [editing, setEditing] = useState(false); |
| 24 | + }, |
| 25 | + } = useOvermind(); |
| 26 | + const [editing, setEditing] = useState(false); |
| 27 | + |
| 28 | + const onKeyDown = (event: React.KeyboardEvent<HTMLTextAreaElement>) => { |
| 29 | + if (event.keyCode === ENTER && !event.shiftKey) { |
| 30 | + event.preventDefault(); |
| 31 | + event.stopPropagation(); |
| 32 | + sandboxInfoUpdated(); |
| 33 | + setEditing(false); |
| 34 | + } |
| 35 | + }; |
28 | 36 |
|
29 | | - const onKeyDown = (event: React.KeyboardEvent<HTMLTextAreaElement>) => { |
30 | | - if (event.keyCode === 13 && !event.shiftKey) { |
31 | | - event.preventDefault(); |
32 | | - event.stopPropagation(); |
| 37 | + return editing ? ( |
| 38 | + <WorkspaceInputContainer style={{ margin: '0 -0.25rem' }}> |
| 39 | + <textarea |
| 40 | + rows={2} |
| 41 | + placeholder="Description" |
| 42 | + value={description} |
| 43 | + ref={el => { |
| 44 | + if (el) { |
| 45 | + el.focus(); |
| 46 | + } |
| 47 | + }} |
| 48 | + onKeyDown={onKeyDown} |
| 49 | + onChange={event => { |
| 50 | + valueChanged({ |
| 51 | + field: 'description', |
| 52 | + value: event.target.value, |
| 53 | + }); |
| 54 | + }} |
| 55 | + onBlur={() => { |
33 | 56 | sandboxInfoUpdated(); |
34 | 57 | setEditing(false); |
35 | | - } |
36 | | - }; |
| 58 | + }} |
| 59 | + /> |
| 60 | + </WorkspaceInputContainer> |
| 61 | + ) : ( |
| 62 | + <SandboxDescription empty={Boolean(description)}> |
| 63 | + {description || (editable ? 'No description, create one!' : '')} |
37 | 64 |
|
38 | | - return editing ? ( |
39 | | - <WorkspaceInputContainer style={{ margin: '0 -0.25rem' }}> |
40 | | - <textarea |
41 | | - rows={2} |
42 | | - placeholder="Description" |
43 | | - value={description} |
44 | | - ref={el => { |
45 | | - if (el) { |
46 | | - el.focus(); |
47 | | - } |
48 | | - }} |
49 | | - onKeyDown={onKeyDown} |
50 | | - onChange={event => { |
51 | | - valueChanged({ |
52 | | - field: 'description', |
53 | | - value: event.target.value, |
54 | | - }); |
55 | | - }} |
56 | | - onBlur={() => { |
57 | | - sandboxInfoUpdated(); |
58 | | - setEditing(false); |
59 | | - }} |
60 | | - /> |
61 | | - </WorkspaceInputContainer> |
62 | | - ) : ( |
63 | | - <SandboxDescription empty={description}> |
64 | | - {description || (editable ? 'No description, create one!' : '')} |
65 | | - {editable && <EditPen onClick={() => setEditing(true)} />} |
66 | | - </SandboxDescription> |
67 | | - ); |
68 | | - } |
69 | | - ) |
70 | | -); |
| 65 | + {editable && <EditPen onClick={() => setEditing(true)} />} |
| 66 | + </SandboxDescription> |
| 67 | + ); |
| 68 | +}; |
0 commit comments