-
Notifications
You must be signed in to change notification settings - Fork 24
/
mapinfo.kt
285 lines (265 loc) · 11.9 KB
/
mapinfo.kt
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
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
package io.beatmaps.maps
import external.Axios
import external.generateConfig
import io.beatmaps.api.CurateMap
import io.beatmaps.api.MapDetail
import io.beatmaps.api.MapInfoUpdate
import io.beatmaps.api.StateUpdate
import io.beatmaps.common.api.EMapState
import io.beatmaps.index.ModalButton
import io.beatmaps.index.ModalComponent
import io.beatmaps.index.ModalData
import io.beatmaps.index.links
import kotlinx.browser.window
import kotlinx.html.InputType
import kotlinx.html.Tag
import kotlinx.html.id
import kotlinx.html.js.onClickFunction
import kotlinx.html.title
import org.w3c.dom.HTMLInputElement
import org.w3c.dom.HTMLTextAreaElement
import react.RBuilder
import react.RComponent
import react.RProps
import react.RReadableRef
import react.RState
import react.ReactElement
import react.createRef
import react.dom.InnerHTML
import react.dom.RDOMBuilder
import react.dom.a
import react.dom.div
import react.dom.i
import react.dom.img
import react.dom.input
import react.dom.p
import react.dom.textarea
import react.setState
import kotlin.collections.set
external interface MapInfoProps : RProps {
var mapInfo: MapDetail
var isOwner: Boolean
var isAdmin: Boolean?
var modal: RReadableRef<ModalComponent>
var reloadMap: () -> Unit
var deleteMap: () -> Unit
var updateMapinfo: (MapDetail) -> Unit
}
data class MapInfoState(var loading: Boolean = false, var editing: Boolean = false) : RState
@JsExport
class MapInfo : RComponent<MapInfoProps, MapInfoState>() {
private val inputRef = createRef<HTMLInputElement>()
private val textareaRef = createRef<HTMLTextAreaElement>()
private val reasonRef = createRef<HTMLTextAreaElement>()
init {
state = MapInfoState()
}
private fun recall() {
props.mapInfo.publishedVersion()?.hash?.let { hash ->
Axios.post<String>("/api/testplay/state", StateUpdate(hash, EMapState.Uploaded, props.mapInfo.intId(), reasonRef.current?.asDynamic()?.value as? String), generateConfig<StateUpdate, String>())
.then({
props.reloadMap()
setState {
loading = false
}
}) {
setState {
loading = false
}
}
}
}
private fun delete() {
Axios.post<String>("/api/maps/update", MapInfoUpdate(props.mapInfo.intId(), deleted = true, reason = reasonRef.current?.asDynamic()?.value as? String), generateConfig<MapInfoUpdate, String>()).then({
props.deleteMap()
}) {
setState {
loading = false
}
}
}
private fun curate(curated: Boolean = true) {
Axios.post<String>("/api/maps/curate", CurateMap(props.mapInfo.intId(), curated), generateConfig<CurateMap, String>()).then({
props.reloadMap()
}) {
setState {
loading = false
}
}
}
override fun RBuilder.render() {
val deleted = props.mapInfo.deletedAt != null
div("card") {
div("card-header d-flex" + if (deleted) " bg-danger" else "") {
if (state.editing) {
+"Edit map"
} else {
+props.mapInfo.name
}
div("ml-auto") {
if (!deleted) {
props.mapInfo.mainVersion()?.let { version ->
links {
attrs.map = props.mapInfo
attrs.version = version
attrs.modal = props.modal
}
val adminLocal = props.isAdmin
if (adminLocal == true || props.isOwner) {
a("#") {
attrs.title = "Edit"
attrs.attributes["aria-label"] = "Edit"
attrs.onClickFunction = {
it.preventDefault()
setState {
editing = !state.editing
}
window.setTimeout(
{
inputRef.current?.value = props.mapInfo.name
},
1
)
}
i("fas fa-pen text-warning") { }
}
}
if (adminLocal == true) {
a("#") {
attrs.title = "Curate"
attrs.attributes["aria-label"] = "Curate"
attrs.onClickFunction = {
it.preventDefault()
curate(props.mapInfo.curator == null)
}
i("fas fa-award " + if (props.mapInfo.curator != null) "text-danger" else "text-success") { }
}
a("#") {
attrs.title = "Delete"
attrs.attributes["aria-label"] = "Delete"
attrs.onClickFunction = {
it.preventDefault()
props.modal.current?.showDialog(
ModalData(
"Delete map",
bodyCallback = {
p {
+"Delete completely so that no more versions can be added or just unpublish the current version?"
}
p {
+"Reason for action:"
}
textarea(classes = "form-control") {
ref = reasonRef
}
},
buttons = listOf(ModalButton("DELETE", "danger", ::delete), ModalButton("Unpublish", "primary", ::recall), ModalButton("Cancel"))
)
)
}
i("fas fa-trash text-danger") { }
}
}
}
}
}
}
div("card-body mapinfo") {
img("Cover Image", props.mapInfo.mainVersion()?.coverURL) {
attrs.width = "200"
attrs.height = "200"
}
div("card-text clearfix") {
if (state.editing) {
input(InputType.text, classes = "form-control m-2") {
attrs.id = "name"
attrs.disabled = state.loading
ref = inputRef
}
textarea("10", classes = "form-control m-2") {
attrs.id = "description"
attrs.disabled = state.loading
+props.mapInfo.description
ref = textareaRef
}
} else {
textToContent(props.mapInfo.description)
}
}
if (state.editing) {
div("text-right") {
if (props.isOwner) {
if (props.mapInfo.publishedVersion() != null) {
a(classes = "btn btn-danger m-1") {
attrs.onClickFunction = {
props.modal.current?.showDialog(
ModalData(
"Are you sure?",
"This will hide your map from other players until you publish a new version",
listOf(ModalButton("OK", "primary", ::recall), ModalButton("Cancel"))
)
)
}
+"Unpublish"
}
} else {
a(classes = "btn btn-danger m-1") {
attrs.onClickFunction = {
props.modal.current?.showDialog(
ModalData(
"Are you sure?",
"You won't be able to recover this map after you delete it",
listOf(ModalButton("DELETE", "primary", ::delete), ModalButton("Cancel"))
)
)
}
+"Delete"
}
}
}
a(classes = "btn btn-primary m-1") {
attrs.onClickFunction = {
val newTitle = inputRef.current?.value ?: ""
val newDescription = textareaRef.current?.asDynamic().value as String
setState {
loading = true
}
Axios.post<String>("/api/maps/update", MapInfoUpdate(props.mapInfo.intId(), newTitle, newDescription), generateConfig<MapInfoUpdate, String>()).then({
props.updateMapinfo(props.mapInfo.copy(name = newTitle, description = newDescription))
setState {
loading = false
editing = false
}
}) {
setState {
loading = false
}
}
}
+"Save"
}
}
}
}
}
}
}
fun String.transformURLIntoLinks() =
replace("\\b((https?|ftp):\\/\\/)?[-a-zA-Z0-9@:%._\\+~#=]{2,256}\\.[A-Za-z]{2,6}\\b(\\/[-a-zA-Z0-9@:%_\\+.~#?&//=]*)*(?:\\/|\\b)".toRegex()) {
if (it.groupValues[1].isEmpty()) it.value else "<a target=\"_blank\" href=\"${it.value}\">${it.value}</a>"
}
fun RBuilder.mapInfo(handler: MapInfoProps.() -> Unit): ReactElement {
return child(MapInfo::class) {
this.attrs(handler)
}
}
// Kotlin IR be adding underscores everywhere
class DangerousHtml(override var __html: String) : InnerHTML
fun <T : Tag> RDOMBuilder<T>.textToContent(text: String) {
domProps.dangerouslySetInnerHTML = DangerousHtml(
text
.replace(Regex("<[^>]+>"), "")
.transformURLIntoLinks()
.replace("\n", "<br />")
)
}