-
Notifications
You must be signed in to change notification settings - Fork 253
Expand file tree
/
Copy patheventModal.svelte
More file actions
384 lines (346 loc) 路 13.8 KB
/
Copy patheventModal.svelte
File metadata and controls
384 lines (346 loc) 路 13.8 KB
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
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
<script lang="ts">
import { Button } from '$lib/elements/forms';
import { Modal } from '$lib/components';
import { Pill } from '$lib/elements';
import { createEventDispatcher } from 'svelte';
import { at, empty } from '$lib/helpers/array';
import Copy from './copy.svelte';
import { selectionStart } from '$lib/actions/selectionStart';
import { singular } from '$lib/helpers/string';
import {
eventServices as services,
type EventAction,
type EventResource,
type EventService
} from '$lib/constants';
import { tooltip } from '$lib/actions/tooltip';
import { trackEvent } from '$lib/actions/analytics';
// Props
export let show = false;
export let initialValue = '';
// Constants
const serviceNames = services.map((s) => s.name);
const resourceNames = services.flatMap((s) => s.resources.map((r) => r.name));
const actionNames = [
...services.flatMap((s) => s.actions.map((a) => a.name)),
...services.flatMap((s) => s.resources.flatMap((r) => r.actions.map((a) => a.name)))
];
const attributeNames = [
...services.flatMap((s) => s.actions.flatMap((a) => a.attributes ?? [])),
...services.flatMap((s) =>
s.resources.flatMap((r) => r.actions.flatMap((a) => a.attributes ?? []))
)
];
// Helpers
const isService = (s: string) => serviceNames.includes(s);
const isResource = (s: string) => resourceNames.includes(s);
const isAction = (s: string) => actionNames.includes(s);
const isAttribute = (s: string) => attributeNames.includes(s);
const dispatch = createEventDispatcher();
function create() {
show = false;
trackEvent('events_add', {
value: inputValue
});
dispatch('created', inputValue);
}
function select(field: 'service', value: EventService): void;
function select(field: 'resource', value: EventResource): void;
function select(field: 'action', value: EventAction): void;
function select(field: 'attribute', value: string): void;
function select(
field: string,
value: string | EventService | EventResource | EventAction
): void {
if (typeof value === 'string') {
selected[field as 'attribute'] = selected[field] === value ? null : value;
} else {
selected[field] = selected[field]?.name === value.name ? null : value;
}
customInput = null;
switch (field) {
case 'service': {
selected.resource = null;
selected.action = null;
selected.attribute = null;
break;
}
case 'resource': {
const availableActions = selected.resource
? selected.resource?.actions
: selected.service?.actions;
// We need to check if the selected action name is still
// on the available list, and if so, change tje action to the
// appropriate one, as it may contain different attributes
const availableAction = availableActions.find(
(a) => a.name === selected.action?.name
);
if (!availableAction) {
selected.action = null;
selected.attribute = null;
} else {
selected.action = availableAction;
if (!selected?.action?.attributes?.includes(selected.attribute)) {
selected.attribute = null;
}
}
break;
}
case 'action': {
selected.attribute = null;
break;
}
}
}
function resetSelected() {
Object.keys(selected).forEach((key) => (selected[key] = null));
}
function inferSelectedFromCustomInput() {
resetSelected();
for (const field of customInput.split('.')) {
if (isService(field)) {
selected.service = services.find((s) => s.name === field);
} else if (isResource(field)) {
const resource = selected.service?.resources.find((r) => r.name === field);
selected.resource = resource;
} else if (isAction(field)) {
const action =
selected.resource?.actions.find((a) => a.name === field) ||
selected.service?.actions.find((a) => a.name === field);
selected.action = action;
} else if (isAttribute(field)) {
const attribute = selected.action?.attributes?.find((a) => a === field);
selected.attribute = attribute;
}
}
}
function toggleShowInput(e: Event) {
e.preventDefault();
showInput = !showInput;
if (showInput) return;
helper = null;
inferSelectedFromCustomInput();
}
function getHelperStr(fields: string[], index: number) {
const currField = at(fields, index);
const prevField = at(fields, index - 1);
const secondToLastField = at(fields, index - 2);
if (index === 0 || isService(currField)) return 'service';
if (isAttribute(currField) || isAction(prevField)) return 'attribute';
if (isAction(currField)) return 'action';
if (isResource(currField)) return 'resource';
if (isService(prevField) || isResource(prevField) || index === 1) {
return `ID of ${singular(prevField)}`;
}
// TODO: Find better name for 'resource or action'
if (isService(secondToLastField) || index === 2) return 'resource or action';
if (isResource(secondToLastField)) return 'action';
return '';
}
function getCustomInputHelperStr(input: string, selectionStart?: number): string {
const fields = input.split('.');
let fieldIndex = -1;
if (selectionStart > -1) {
let currentIndex = 0;
let arrayIndex = 0;
for (const item of fields) {
currentIndex += item.length + 1;
if (currentIndex > selectionStart) {
fieldIndex = arrayIndex;
break;
}
arrayIndex++;
}
}
return getHelperStr(fields, fieldIndex);
}
// State & Reactive Declarations
let selected = {
service: null as EventService | null,
resource: null as EventResource | null,
action: null as EventAction | null,
attribute: null as string | null
};
let helper: string = null;
let customInput: string = null;
let customInputCursor = -1;
let showInput = false;
let copyParent: HTMLElement;
$: if (show && initialValue) {
customInput = initialValue;
inferSelectedFromCustomInput();
}
$: available = {
services: services,
resources: selected.service?.resources || [],
actions: (selected.resource ? selected.resource?.actions : selected.service?.actions) || [],
attributes: selected.action?.attributes || []
};
$: eventString = (function createEventString(): Array<{ value: string; description: string }> {
let fields: string[] = [];
// Avoid calculating the event string if the user is typing, as it is not shown
if (showInput) return [];
if (customInput) {
fields = customInput.split('.');
} else {
if (selected.service) {
fields.push(selected.service.name, '*');
}
if (selected.resource?.name === 'documents') {
fields.push('collections', '*');
}
if (selected.resource) {
fields.push(selected.resource.name, '*');
}
if (selected.action) {
fields.push(selected.action.name);
}
if (selected.attribute) {
fields.push(selected.attribute);
}
}
return fields.map((value, i, arr) => ({ value, description: getHelperStr(arr, i) }));
})();
$: inputValue = eventString.map((d) => d.value).join('.');
$: if (!show) {
resetSelected();
helper = null;
customInput = null;
showInput = false;
}
$: if (showInput) {
helper = getCustomInputHelperStr(customInput, customInputCursor);
}
</script>
<Modal bind:show onSubmit={create} size="big">
<svelte:fragment slot="header">Create event</svelte:fragment>
<slot />
<div>
<p class="u-text">Choose a service</p>
<div class="u-flex u-gap-8 u-margin-block-start-8">
{#each available.services as service}
<Pill
disabled={showInput}
selected={selected.service?.name === service.name}
button
on:click={() => select('service', service)}>
{service.name}
</Pill>
{/each}
</div>
</div>
{#if !empty(available.resources)}
<div>
<p class="u-text">Choose a resource (optional)</p>
<div class="u-flex u-gap-8 u-margin-block-start-8">
{#each available.resources as resource}
<Pill
disabled={showInput}
selected={selected.resource?.name === resource.name}
button
on:click={() => select('resource', resource)}>
{resource.name}
</Pill>
{/each}
</div>
</div>
{/if}
{#if !empty(available.actions)}
<div>
<p class="u-text">Choose an action (optional)</p>
<div class="u-flex u-gap-8 u-margin-block-start-8">
{#each available.actions as action}
<Pill
disabled={showInput}
selected={selected.action?.name === action.name}
button
on:click={() => select('action', action)}>
{action.name}
</Pill>
{/each}
</div>
</div>
{/if}
{#if !empty(available.attributes)}
<div>
<p class="u-text">Choose an attribute (optional)</p>
<div class="u-flex u-gap-8 u-margin-block-start-8">
{#each available.attributes as attribute}
<Pill
disabled={showInput}
selected={selected.attribute === attribute}
button
on:click={() => select('attribute', attribute)}>
{attribute}
</Pill>
{/each}
</div>
</div>
{/if}
{#if showInput}
<div class="input-text-wrapper" style="--amount-of-buttons:2">
<input
type="text"
placeholder="Enter custom event"
bind:value={customInput}
use:selectionStart={{ onChange: (newValue) => (customInputCursor = newValue) }} />
<div class="options-list">
<button on:click={toggleShowInput} class="options-list-button" aria-label="confirm">
<span class="icon-check" aria-hidden="true" />
</button>
<button on:click={toggleShowInput} class="options-list-button" aria-label="cancel">
<span class="icon-x" aria-hidden="true" />
</button>
</div>
<p style="height: 2rem;">{helper ?? ''}</p>
</div>
{:else}
<div class="input-text-wrapper" style="--amount-of-buttons:2" bind:this={copyParent}>
<!--
This object syntax avoids TS erroring because 'type' isn't a valid HTMLDivElement attribute
(we need to set it to 'text' to add styling)
-->
<div {...{ type: 'text' }} style="min-height: 2.5rem;">
{#each eventString as route, i}
<span
class:u-opacity-0-5={helper !== route.description}
on:mouseenter={() => (helper = route.description)}
on:mouseleave={() => (helper = null)}>
{route.value}
</span>
<span class="u-opacity-0-5">
{i + 1 < eventString?.length ? '.' : ''}
</span>
{/each}
</div>
<div class="options-list">
{#key copyParent}
<button
on:click={(e) => {
customInput = inputValue;
toggleShowInput(e);
}}
use:tooltip={{ content: 'Edit event', appendTo: copyParent }}
class="options-list-button"
aria-label="edit event">
<span class="icon-pencil" aria-hidden="true" />
</button>
<button
disabled={!inputValue}
class="options-list-button"
aria-label="copy text">
<Copy value={inputValue} appendTo={copyParent}>
<span class="icon-duplicate" aria-hidden="true" />
</Copy>
</button>
{/key}
</div>
<p style="height: 2rem;">{helper ?? ''}</p>
</div>
{/if}
<svelte:fragment slot="footer">
<Button secondary on:click={() => (show = false)}>Cancel</Button>
<Button disabled={showInput || !inputValue} submit
>{initialValue ? 'Update' : 'Create'}</Button>
</svelte:fragment>
</Modal>