|
1 | | -import React, { useState, useRef } from 'react'; |
2 | | -import { inject, hooksObserver } from 'app/componentConnectors'; |
3 | | -import { Link } from 'react-router-dom'; |
4 | | -import { useClickAway } from 'react-use'; |
5 | | -import { SketchPicker } from 'react-color'; |
6 | | -import Tooltip from '@codesandbox/common/lib/components/Tooltip'; |
7 | 1 | import Switch from '@codesandbox/common/lib/components/Switch'; |
| 2 | +import Tooltip from '@codesandbox/common/lib/components/Tooltip'; |
8 | 3 | import * as templates from '@codesandbox/common/lib/templates'; |
| 4 | +import React, { FunctionComponent, useRef, useState } from 'react'; |
| 5 | +import { SketchPicker } from 'react-color'; |
| 6 | +import { Link } from 'react-router-dom'; |
| 7 | +import { useClickAway } from 'react-use'; |
| 8 | + |
| 9 | +import { useOvermind } from 'app/overmind'; |
| 10 | + |
| 11 | +import { WorkspaceItem } from '../../WorkspaceItem'; |
| 12 | + |
9 | 13 | import { |
| 14 | + Explanation, |
| 15 | + Icon as QuestionIcon, |
10 | 16 | Item, |
11 | 17 | PropertyName, |
12 | 18 | PropertyValue, |
13 | | - Explanation, |
14 | | - Icon as QuestionIcon, |
15 | 19 | } from '../elements'; |
| 20 | + |
16 | 21 | import { PickColor, PickerContainer, PublicValue } from './elements'; |
17 | | -import { WorkspaceItem } from '../../WorkspaceItem'; |
18 | 22 | import { Icon } from './Icon'; |
19 | 23 |
|
20 | | -export const TemplateConfig = inject('store', 'signals')( |
21 | | - hooksObserver( |
22 | | - ({ |
23 | | - signals: { |
24 | | - workspace: { editTemplate }, |
| 24 | +export const TemplateConfig: FunctionComponent = () => { |
| 25 | + const { |
| 26 | + actions: { |
| 27 | + workspace: { editTemplate }, |
| 28 | + }, |
| 29 | + state: { |
| 30 | + editor: { |
| 31 | + currentSandbox: { customTemplate, template }, |
25 | 32 | }, |
26 | | - store: { |
27 | | - editor: { |
28 | | - currentSandbox: { template, customTemplate }, |
29 | | - }, |
30 | | - }, |
31 | | - }) => { |
32 | | - const picker = useRef(null); |
33 | | - const defaultColor = |
34 | | - (customTemplate && customTemplate.color) || |
35 | | - templates.default(template).color(); |
36 | | - const [showPicker, setShowPicker] = useState(false); |
37 | | - const [publicTemplate, setPublic] = useState(customTemplate.published); |
38 | | - const [selectedColor, setSelectedColor] = useState(defaultColor); |
39 | | - const colors = Object.keys(templates) |
40 | | - .filter(x => x !== 'default') |
41 | | - .map(t => templates[t]) |
42 | | - .map(a => ({ color: a.color(), title: a.niceName })); |
43 | | - |
44 | | - useClickAway(picker, () => { |
45 | | - setShowPicker(false); |
46 | | - editTemplate({ |
47 | | - template: { |
48 | | - color: selectedColor, |
49 | | - }, |
50 | | - }); |
51 | | - }); |
52 | | - |
53 | | - const togglePublic = () => { |
54 | | - editTemplate({ |
55 | | - template: { |
56 | | - ...customTemplate, |
57 | | - published: !publicTemplate, |
58 | | - }, |
59 | | - }); |
60 | | - setPublic(!publicTemplate); |
61 | | - }; |
62 | | - |
63 | | - return ( |
64 | | - <WorkspaceItem showOverflow defaultOpen title="Template"> |
65 | | - <Explanation style={{ marginTop: 0, marginBottom: '.5rem' }}> |
66 | | - This is a template, you can find more info about templates |
67 | | - <Link target="_blank" to="/docs/templates"> |
68 | | - {' '} |
69 | | - here |
70 | | - </Link> |
71 | | - . |
72 | | - </Explanation> |
73 | | - <Item style={{ marginTop: '0.5rem' }}> |
74 | | - <PropertyName>Color</PropertyName> |
75 | | - <PropertyValue relative> |
76 | | - <PickColor |
77 | | - onClick={() => setShowPicker(true)} |
| 33 | + }, |
| 34 | + } = useOvermind(); |
| 35 | + const picker = useRef(null); |
| 36 | + const [showPicker, setShowPicker] = useState(false); |
| 37 | + const [publicTemplate, setPublic] = useState( |
| 38 | + customTemplate.published || false |
| 39 | + ); |
| 40 | + const [selectedColor, setSelectedColor] = useState( |
| 41 | + () => customTemplate.color || templates.default(template).color() |
| 42 | + ); |
| 43 | + |
| 44 | + const colors = Object.keys(templates) |
| 45 | + .filter(x => x !== 'default') |
| 46 | + .map(t => templates[t]) |
| 47 | + .map(a => ({ color: a.color(), title: a.niceName })); |
| 48 | + |
| 49 | + useClickAway(picker, () => { |
| 50 | + setShowPicker(false); |
| 51 | + |
| 52 | + editTemplate({ |
| 53 | + ...customTemplate, |
| 54 | + color: selectedColor, |
| 55 | + }); |
| 56 | + }); |
| 57 | + |
| 58 | + const togglePublic = () => { |
| 59 | + editTemplate({ |
| 60 | + ...customTemplate, |
| 61 | + published: !publicTemplate, |
| 62 | + }); |
| 63 | + |
| 64 | + setPublic(!publicTemplate); |
| 65 | + }; |
| 66 | + |
| 67 | + return ( |
| 68 | + <WorkspaceItem showOverflow defaultOpen title="Template"> |
| 69 | + <Explanation style={{ marginTop: 0, marginBottom: '.5rem' }}> |
| 70 | + This is a template, you can find more info about templates{' '} |
| 71 | + <Link target="_blank" to="/docs/templates"> |
| 72 | + here |
| 73 | + </Link> |
| 74 | + . |
| 75 | + </Explanation> |
| 76 | + |
| 77 | + <Item style={{ marginTop: '0.5rem' }}> |
| 78 | + <PropertyName>Color</PropertyName> |
| 79 | + |
| 80 | + <PropertyValue relative> |
| 81 | + <PickColor |
| 82 | + onClick={() => setShowPicker(true)} |
| 83 | + color={selectedColor} |
| 84 | + /> |
| 85 | + |
| 86 | + {showPicker && ( |
| 87 | + <PickerContainer ref={picker}> |
| 88 | + <SketchPicker |
| 89 | + disableAlpha |
| 90 | + id="color" |
| 91 | + onChangeComplete={(color: { hex: string }) => |
| 92 | + setSelectedColor(color.hex) |
| 93 | + } |
78 | 94 | color={selectedColor} |
| 95 | + presetColors={[...new Set(colors)]} |
79 | 96 | /> |
80 | | - {showPicker && ( |
81 | | - <PickerContainer ref={picker}> |
82 | | - <SketchPicker |
83 | | - disableAlpha |
84 | | - id="color" |
85 | | - onChangeComplete={(color: { hex: string }) => |
86 | | - setSelectedColor(color.hex) |
87 | | - } |
88 | | - color={selectedColor} |
89 | | - presetColors={[...new Set(colors)]} |
90 | | - /> |
91 | | - </PickerContainer> |
92 | | - )} |
93 | | - </PropertyValue> |
94 | | - </Item> |
95 | | - <Item> |
96 | | - <PropertyName> |
97 | | - Public |
98 | | - <Tooltip |
99 | | - boundary="viewport" |
100 | | - content="Whether this template will show in our upcoming templates page" |
101 | | - > |
102 | | - <QuestionIcon /> |
103 | | - </Tooltip> |
104 | | - </PropertyName> |
105 | | - <PublicValue> |
106 | | - <Switch |
107 | | - small |
108 | | - onClick={togglePublic} |
109 | | - right={publicTemplate} |
110 | | - offMode |
111 | | - secondary |
112 | | - /> |
113 | | - </PublicValue> |
114 | | - </Item> |
115 | | - <Icon /> |
116 | | - </WorkspaceItem> |
117 | | - ); |
118 | | - } |
119 | | - ) |
120 | | -); |
| 97 | + </PickerContainer> |
| 98 | + )} |
| 99 | + </PropertyValue> |
| 100 | + </Item> |
| 101 | + |
| 102 | + <Item> |
| 103 | + <PropertyName> |
| 104 | + Public |
| 105 | + <Tooltip |
| 106 | + boundary="viewport" |
| 107 | + content="Whether this template will show in our upcoming templates page" |
| 108 | + > |
| 109 | + <QuestionIcon /> |
| 110 | + </Tooltip> |
| 111 | + </PropertyName> |
| 112 | + |
| 113 | + <PublicValue> |
| 114 | + <Switch |
| 115 | + small |
| 116 | + onClick={togglePublic} |
| 117 | + right={publicTemplate} |
| 118 | + offMode |
| 119 | + secondary |
| 120 | + /> |
| 121 | + </PublicValue> |
| 122 | + </Item> |
| 123 | + |
| 124 | + <Icon /> |
| 125 | + </WorkspaceItem> |
| 126 | + ); |
| 127 | +}; |
0 commit comments