forked from carbon-app/carbon
-
Notifications
You must be signed in to change notification settings - Fork 0
/
SnippetToolbar.js
165 lines (150 loc) · 3.8 KB
/
SnippetToolbar.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
import React from 'react'
import { useAsyncCallback, useOnline, useKeyboardListener } from 'actionsack'
import Button from './Button'
import Toolbar from './Toolbar'
import Input from './Input'
import ConfirmButton from './ConfirmButton'
import Popout, { managePopout } from './Popout'
import { Down as ArrowDown } from './svg/Arrows'
import { useAuth } from './AuthContext'
import { COLORS } from '../lib/constants'
const popoutStyle = { width: '120px', right: -8, top: 40 }
function DeleteButton(props) {
const [onClick, { loading }] = useAsyncCallback(props.onClick)
return (
<ConfirmButton
display="block"
padding="8px"
flex="unset"
center
large
color="#fff"
onClick={onClick}
style={{ color: COLORS.RED }}
>
{loading ? 'Deleting…' : 'Delete'}
</ConfirmButton>
)
}
function DuplicateButton(props) {
const [onClick, { loading }] = useAsyncCallback(props.onClick)
return (
<Button
border
large
center
color={COLORS.GREEN}
onClick={onClick}
data-cy="duplicate-button"
style={{ minWidth: 92 }}
title="Duplicate"
disabled={loading}
>
{loading ? 'Duplicating…' : 'Duplicate'}
</Button>
)
}
function SaveButton({ loading, onClick, sameUser }) {
useKeyboardListener('⌥-s', e => {
if (loading) {
return
}
e.preventDefault()
onClick()
})
return (
<Button
border
large
center
color={COLORS.GREEN}
onClick={onClick}
data-cy="save-button"
style={{
minWidth: 84,
borderBottomRightRadius: sameUser ? 0 : undefined,
borderTopRightRadius: sameUser ? 0 : undefined,
}}
title="Save"
disabled={loading}
>
{loading ? 'Saving…' : 'Save'}
</Button>
)
}
function SnippetToolbar({ toggleVisibility, isVisible, snippet, ...props }) {
const user = useAuth()
const online = useOnline()
const [save, { loading }] = useAsyncCallback(() => {
if (snippet) {
return props.onUpdate()
} else {
return props.onCreate()
}
})
if (!online) return null
if (!user) return null
const sameUser = snippet && user.uid === snippet.userId
return (
<Toolbar
style={{
position: 'relative',
zIndex: 1,
marginTop: 16,
marginBottom: 0,
flexDirection: 'row',
alignItems: 'center',
}}
>
<Popout hidden={!isVisible} borderColor={COLORS.GREEN} pointerRight="6px" style={popoutStyle}>
<div className="menu flex">
<DeleteButton onClick={props.onDelete} />
</div>
</Popout>
<div style={{ marginRight: 'auto' }}>
<Input
align="left"
placeholder="Add a name…"
fontSize="14px"
value={props.name}
onChange={e => props.onChange('name', e.target.value)}
/>
</div>
<div className="flex">
{snippet && !sameUser ? (
<DuplicateButton onClick={props.onCreate} />
) : (
<SaveButton loading={loading} onClick={save} sameUser={sameUser} />
)}
{sameUser && (
<Button
title="Save menu dropdown"
border
large
center
color={COLORS.GREEN}
padding="0 8px"
margin="0 0 0 -1px"
onClick={toggleVisibility}
data-cy="save-button"
style={{
borderBottomLeftRadius: 0,
borderTopLeftRadius: 0,
maxWidth: '26px',
}}
>
<ArrowDown color={COLORS.GREEN} />
</Button>
)}
</div>
<style jsx>
{`
.menu {
flex-direction: column;
}
`}
</style>
</Toolbar>
)
}
export default managePopout(SnippetToolbar)