This repository was archived by the owner on Apr 3, 2026. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 12
Expand file tree
/
Copy pathindex.tsx
More file actions
1103 lines (1019 loc) · 42 KB
/
index.tsx
File metadata and controls
1103 lines (1019 loc) · 42 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
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
import React from 'react';
import {
_cs,
isDefined,
unique,
isNotDefined,
isTruthyString,
difference,
listToMap,
} from '@togglecorp/fujs';
import {
useForm,
getErrorObject,
createSubmitHandler,
analyzeErrors,
useFormArray,
} from '@togglecorp/toggle-form';
import {
getStorage,
ref as storageRef,
uploadBytes,
getDownloadURL,
} from 'firebase/storage';
import {
getDatabase,
ref as databaseRef,
push as pushToDatabase,
set as setToDatabase,
} from 'firebase/database';
import {
MdSwipe,
MdOutlinePublishedWithChanges,
MdOutlineUnpublished,
MdAdd,
} from 'react-icons/md';
import {
IoIosTrash,
} from 'react-icons/io';
import {
IoInformationCircleOutline,
} from 'react-icons/io5';
import { Link } from 'react-router-dom';
import UserContext from '#base/context/UserContext';
import projectTypeOptions from '#base/configs/projectTypes';
import useMountedRef from '#hooks/useMountedRef';
import Modal from '#components/Modal';
import TextInput from '#components/TextInput';
import NumberInput from '#components/NumberInput';
import Heading from '#components/Heading';
import SegmentInput from '#components/SegmentInput';
import GeoJsonFileInput from '#components/GeoJsonFileInput';
import ExpandableContainer from '#components/ExpandableContainer';
import PopupButton from '#components/PopupButton';
import TileServerInput, {
TILE_SERVER_BING,
TILE_SERVER_ESRI,
tileServerDefaultCredits,
} from '#components/TileServerInput';
import InputSection from '#components/InputSection';
import Button from '#components/Button';
import NonFieldError from '#components/NonFieldError';
import EmptyMessage from '#components/EmptyMessage';
import AlertBanner from '#components/AlertBanner';
import {
valueSelector,
labelSelector,
PROJECT_TYPE_BUILD_AREA,
PROJECT_TYPE_COMPLETENESS,
PROJECT_TYPE_CHANGE_DETECTION,
PROJECT_TYPE_FOOTPRINT,
PROJECT_TYPE_STREET,
ProjectType,
projectTypeLabelMap,
} from '#utils/common';
import {
tileServerUrls,
tutorialFormSchema,
getDefaultOptions,
TutorialFormType,
PartialTutorialFormType,
PartialInformationPagesType,
ScenarioPagesType,
CustomOptionType,
InformationPagesType,
InformationPageTemplateKey,
infoPageTemplateOptions,
infoPageBlocksMap,
MAX_INFO_PAGES,
MAX_OPTIONS,
deleteKey,
TutorialTasksGeoJSON,
BuildAreaProperties,
ChangeDetectionProperties,
} from './utils';
import CustomOptionPreview from './CustomOptionInput/CustomOptionPreview';
import CustomOptionInput from './CustomOptionInput';
import ScenarioPageInput from './ScenarioPageInput';
import InformationPageInput from './InformationPageInput';
import styles from './styles.css';
export function getDuplicates<T, K extends string | number>(
list: T[],
keySelector: (item: T) => K,
filter: (count: number) => boolean = (count) => count > 1,
) {
const counts = listToMap<T, number, K>(
list,
keySelector,
(_, key, __, acc) => {
const value: number | undefined = acc[key];
return isDefined(value) ? value + 1 : 1;
},
);
return Object.keys(counts).filter((key) => filter(counts[key as K]));
}
type ValidType ='number' | 'string' | 'boolean';
function checkSchema<T extends object>(
obj: T,
schema: Record<string, ValidType | ValidType[]>,
) {
const schemaKeys = Object.keys(schema);
const errors = schemaKeys.map(
(key) => {
const expectedType = schema[key];
const keySafe = key as keyof T;
const currentValue: unknown = obj[keySafe];
const valueType = typeof currentValue;
if (Array.isArray(expectedType)) {
const indexOfType = expectedType.findIndex(
(type) => type === valueType,
);
if (indexOfType === -1) {
return `type of ${key} expected to be one of type ${expectedType.join(', ')}`;
}
} else if (typeof currentValue !== expectedType) {
return `type of ${key} expected to be of ${expectedType}`;
}
return undefined;
},
).filter(isDefined);
return errors;
}
function getGeoJSONError(
tutorialTasks: TutorialTasksGeoJSON,
projectType: ProjectType,
) {
if (isNotDefined(tutorialTasks.features) || !Array.isArray(tutorialTasks.features)) {
return 'GeoJSON does not contain iterable features';
}
// Check properties schema
const projectSchemas: {
[key in ProjectType]: Record<string, ValidType | ValidType[]>;
} = {
[PROJECT_TYPE_FOOTPRINT]: {
id: ['string', 'number'],
reference: 'number',
screen: 'number',
},
[PROJECT_TYPE_CHANGE_DETECTION]: {
reference: 'number',
screen: 'number',
task_id: 'string',
tile_x: 'number',
tile_y: 'number',
tile_z: 'number',
},
[PROJECT_TYPE_BUILD_AREA]: {
reference: 'number',
screen: 'number',
task_id: 'string',
tile_x: 'number',
tile_y: 'number',
tile_z: 'number',
},
[PROJECT_TYPE_COMPLETENESS]: {
reference: 'number',
screen: 'number',
task_id: 'string',
tile_x: 'number',
tile_y: 'number',
tile_z: 'number',
},
[PROJECT_TYPE_STREET]: {
id: ['string', 'number'],
reference: 'number',
screen: 'number',
},
};
const schemaErrors = tutorialTasks.features.map(
(feature) => checkSchema(
feature.properties,
projectSchemas[projectType],
).join(', '),
).filter(isTruthyString);
if (schemaErrors.length > 0) {
return `Invalid GeoJSON for ${projectTypeLabelMap[projectType]}: ${schemaErrors[0]} (${schemaErrors.length} total errors)`;
}
return undefined;
}
function getGeoJSONWarning(
tutorialTasks: TutorialTasksGeoJSON | undefined,
projectType: ProjectType | undefined,
customOptions: number[] | undefined,
maxZoom: number | undefined,
) {
const errors = [];
const screens = tutorialTasks?.features?.map(
(feature) => feature.properties.screen,
) ?? [];
if (screens.length > 0) {
const minScreen = Math.min(...screens);
const maxScreen = Math.max(...screens);
const totalScreen = maxScreen - minScreen + 1;
if (minScreen !== 1) {
errors.push(`Screen in GeoJSON should start from 1. The first screen is ${minScreen}`);
}
const actualScreens = new Set(screens);
const expectedScreens = new Set(Array.from(
{ length: totalScreen },
(_, index) => minScreen + index,
));
const missingScreens = difference(
expectedScreens,
actualScreens,
);
if (missingScreens.size === 1) {
errors.push(`Screen in GeoJSON should be sequential. The missing screen is ${[...missingScreens].sort().join(', ')}`);
} else if (missingScreens.size > 1) {
errors.push(`Screen in GeoJSON should be sequential. The missing screens are ${[...missingScreens].sort().join(', ')}`);
}
if (
projectType === PROJECT_TYPE_BUILD_AREA
|| projectType === PROJECT_TYPE_COMPLETENESS
) {
const dups = getDuplicates(
screens,
(item) => item,
(count) => count !== 6,
);
if (dups.length === 1) {
errors.push(`There should be exactly 6 squares with same screen in GeoJSON. The invalid screen is ${dups.join(', ')}`);
} else if (dups.length > 1) {
errors.push(`There should be exactly 6 squares with same screen in GeoJSON. The invalid screens are ${dups.join(', ')}`);
}
} else if (
projectType === PROJECT_TYPE_CHANGE_DETECTION
|| projectType === PROJECT_TYPE_FOOTPRINT
) {
const dups = getDuplicates(
screens,
(item) => item,
(count) => count > 1,
);
if (dups.length === 1) {
errors.push(`Screen in GeoJSON should not be duplicated. The duplicated screen is ${dups.join(', ')}`);
} else if (dups.length > 1) {
errors.push(`Screen in GeoJSON should not be duplicated. The duplicated screens are ${dups.join(', ')}`);
}
}
}
if (
projectType === PROJECT_TYPE_BUILD_AREA
|| projectType === PROJECT_TYPE_CHANGE_DETECTION
|| projectType === PROJECT_TYPE_COMPLETENESS
) {
type LocalTutorialTasksGeoJSON = GeoJSON.FeatureCollection<
GeoJSON.Geometry,
BuildAreaProperties | ChangeDetectionProperties
>;
const zooms = (tutorialTasks as LocalTutorialTasksGeoJSON)?.features?.map(
(feature) => feature.properties.tile_z,
) ?? [];
const uniqueZooms = new Set(zooms);
if (uniqueZooms.size > 1) {
errors.push(`Zoom in GeoJSON should be all be the same. Zoom should be either ${[...uniqueZooms].sort().join(' or ')}`);
} else if (isDefined(maxZoom) && uniqueZooms.size === 1) {
const zoom = [...uniqueZooms][0];
if (zoom !== maxZoom) {
errors.push(`Zoom in GeoJSON does not match defined zoom level. ${zoom} != ${maxZoom}`);
}
}
}
// Check if options are not available
const references = tutorialTasks?.features?.map(
(feature) => feature.properties.reference,
) ?? [];
const selectedOptionsSet = new Set(references);
const availableOptionsSet = projectType === PROJECT_TYPE_FOOTPRINT
? customOptions ?? []
: [0, 1, 2, 3];
const invalidOptions = difference(new Set(selectedOptionsSet), new Set(availableOptionsSet));
if (invalidOptions.size === 1) {
errors.push(`Reference in GeoJSON should be either ${availableOptionsSet.join(', ')}. The invalid reference is ${[...invalidOptions].join(', ')}`);
} else if (invalidOptions.size > 1) {
errors.push(`Reference in GeoJSON should be either ${availableOptionsSet.join(', ')}. The invalid references are ${[...invalidOptions].sort().join(', ')}`);
}
return errors;
}
type CustomScreen = Omit<TutorialFormType['scenarioPages'][number], 'scenarioId'>;
function sanitizeScreens(scenarioPages: TutorialFormType['scenarioPages']) {
const screens = scenarioPages.reduce<Record<string, CustomScreen>>(
(acc, currentValue) => {
const { scenarioId, ...other } = currentValue;
acc[scenarioId] = {
...other,
};
return acc;
},
{},
);
return screens;
}
const defaultTutorialFormValue: PartialTutorialFormType = {
// projectType: PROJECT_TYPE_BUILD_AREA,
projectType: undefined,
zoomLevel: 18,
tileServer: {
name: TILE_SERVER_BING,
credits: tileServerDefaultCredits[TILE_SERVER_BING],
},
tileServerB: {
name: TILE_SERVER_ESRI,
credits: tileServerDefaultCredits[TILE_SERVER_ESRI],
},
};
type SubmissionStatus = 'started' | 'imageUpload' | 'tutorialSubmit' | 'success' | 'failed';
interface Props {
className?: string;
}
function NewTutorial(props: Props) {
const {
className,
} = props;
const { user } = React.useContext(UserContext);
const mountedRef = useMountedRef();
const popupElementRef = React.useRef<{
setPopupVisibility: React.Dispatch<React.SetStateAction<boolean>>;
}>(null);
const [
tutorialSubmissionStatus,
setTutorialSubmissionStatus,
] = React.useState<SubmissionStatus | undefined>();
const {
setFieldValue,
value,
error: formError,
validate,
setError,
} = useForm(tutorialFormSchema, {
value: defaultTutorialFormValue,
});
const {
setValue: onScenarioFormChange,
} = useFormArray<
'scenarioPages',
ScenarioPagesType
>('scenarioPages', setFieldValue);
const {
setValue: setOptionValue,
removeValue: onOptionRemove,
} = useFormArray<
'customOptions',
CustomOptionType
>('customOptions', setFieldValue);
const {
setValue: setInformationPageValue,
removeValue: onInformationPageRemove,
} = useFormArray<
'informationPages',
InformationPagesType
>('informationPages', setFieldValue);
const handleSubmission = React.useCallback((
finalValuesFromProps: PartialTutorialFormType,
) => {
const userId = user?.id;
const finalValues = finalValuesFromProps as TutorialFormType;
if (!userId) {
// eslint-disable-next-line no-console
console.error('Cannot submit form because user is not defined');
return;
}
setTutorialSubmissionStatus('started');
async function submitToFirebase() {
if (!mountedRef.current) {
return;
}
const {
scenarioPages,
informationPages,
...valuesToCopy
} = finalValues;
const sanitizedScenarioPages = sanitizeScreens(scenarioPages);
/*
try {
await navigator.clipboard.writeText(
JSON.stringify(sanitizedScenarioPages, null, 2),
);
// eslint-disable-next-line no-alert
alert('Tutorial JSON copied to clipboard.');
} catch (err) {
// eslint-disable-next-line no-alert
alert(`Tutorial JSON could not be copied ${err}`);
}
*/
const storage = getStorage();
const timestamp = (new Date()).getTime();
setTutorialSubmissionStatus('imageUpload');
try {
const informationPagesPromises = informationPages.map(async (info, index) => {
const blocksPromise = info.blocks.map(async (block) => {
if (!block.imageFile) {
return block;
}
const {
imageFile,
...otherBlocks
} = block;
const uploadImagesRef = storageRef(
storage,
`tutorialImages/${timestamp}-block-image-${block.blockNumber}-${imageFile?.name}`,
);
const uploadTask = await uploadBytes(uploadImagesRef, block.imageFile);
const downloadUrl = await getDownloadURL(uploadTask.ref);
return {
...otherBlocks,
image: downloadUrl,
};
});
const blocks = await Promise.all(blocksPromise);
return {
...info,
// We are making sure that page number starts with 1
// and is sequential
pageNumber: index + 1,
blocks,
};
});
const sanitizedInformationPages = await Promise.all(informationPagesPromises);
const sanitizedCustomOptions = valuesToCopy.customOptions?.map((option) => {
const optionWithoutId = deleteKey(option, 'optionId');
return {
...optionWithoutId,
subOptions: optionWithoutId.subOptions?.map(
(subOption) => deleteKey(subOption, 'subOptionsId'),
),
};
});
const uploadData = {
...valuesToCopy,
customOptions: sanitizedCustomOptions ?? null,
screens: sanitizedScenarioPages ?? null,
informationPages: sanitizedInformationPages ?? null,
createdBy: userId,
};
const database = getDatabase();
const tutorialDraftsRef = databaseRef(database, 'v2/tutorialDrafts/');
const newTutorialDraftsRef = await pushToDatabase(tutorialDraftsRef);
if (!mountedRef.current) {
return;
}
const newKey = newTutorialDraftsRef.key;
if (newKey) {
setTutorialSubmissionStatus('tutorialSubmit');
const newTutorialRef = databaseRef(database, `v2/tutorialDrafts/${newKey}`);
await setToDatabase(newTutorialRef, uploadData);
if (!mountedRef.current) {
return;
}
setTutorialSubmissionStatus('success');
} else {
setTutorialSubmissionStatus('failed');
}
} catch (submissionError) {
if (!mountedRef.current) {
return;
}
// eslint-disable-next-line no-console
console.error(submissionError);
setTutorialSubmissionStatus('failed');
}
}
submitToFirebase();
}, [user, mountedRef]);
const handleSubmitButtonClick = React.useCallback(
() => {
createSubmitHandler(
validate,
setError,
handleSubmission,
)();
},
[validate, setError, handleSubmission],
);
const handleAddDefineOptions = React.useCallback(
() => {
setFieldValue(
(oldValue: PartialTutorialFormType['customOptions']) => {
const safeOldValues = oldValue ?? [];
const newOptionId = safeOldValues.length > 0
? Math.max(...safeOldValues.map((option) => option.optionId)) + 1
: 1;
const newValue = safeOldValues.length > 0
? Math.max(...safeOldValues.map((option) => option.value ?? 0)) + 1
: 1;
const newDefineOption: CustomOptionType = {
optionId: newOptionId,
value: newValue,
icon: undefined,
title: undefined,
iconColor: undefined,
};
return [...safeOldValues, newDefineOption];
},
'customOptions',
);
},
[
setFieldValue,
],
);
const handleGeoJsonFile = React.useCallback((
geoProps: GeoJSON.GeoJSON | undefined,
) => {
const projectType = value?.projectType;
const tutorialTasks = geoProps as TutorialTasksGeoJSON;
if (!tutorialTasks || !projectType) {
setFieldValue(undefined, 'tutorialTasks');
setFieldValue(undefined, 'scenarioPages');
return;
}
const errors = getGeoJSONError(tutorialTasks, projectType);
if (errors) {
setFieldValue(undefined, 'tutorialTasks');
setFieldValue(undefined, 'scenarioPages');
setError((prevError) => ({
...getErrorObject(prevError),
tutorialTasks: errors,
}));
return;
}
setFieldValue(tutorialTasks, 'tutorialTasks');
const uniqueArray = unique(
tutorialTasks.features,
((geo) => geo?.properties.screen),
);
const sorted = uniqueArray.sort((a, b) => a.properties.screen - b.properties.screen);
const tutorialTaskArray = sorted?.map((geo) => (
{
scenarioId: geo.properties.screen,
hint: {},
instructions: {},
success: {},
}
));
setFieldValue(tutorialTaskArray, 'scenarioPages');
}, [setFieldValue, setError, value?.projectType]);
const handleAddInformationPage = React.useCallback(
(template: InformationPageTemplateKey) => {
setFieldValue(
(oldValue: PartialInformationPagesType) => {
const newOldValue = oldValue ?? [];
const newPage = newOldValue.length > 0
? Math.max(...newOldValue.map((info) => info.pageNumber)) + 1
: 1;
const blocks = infoPageBlocksMap[template];
const newPageInformation: InformationPagesType = {
pageNumber: newPage,
title: undefined,
blocks,
};
return [...newOldValue, newPageInformation];
},
'informationPages',
);
popupElementRef.current?.setPopupVisibility(false);
},
[setFieldValue],
);
const submissionPending = (
tutorialSubmissionStatus === 'started'
|| tutorialSubmissionStatus === 'imageUpload'
|| tutorialSubmissionStatus === 'tutorialSubmit'
);
const tileServerVisible = value.projectType === PROJECT_TYPE_BUILD_AREA
|| value.projectType === PROJECT_TYPE_FOOTPRINT
|| value.projectType === PROJECT_TYPE_COMPLETENESS
|| value.projectType === PROJECT_TYPE_CHANGE_DETECTION;
const tileServerBVisible = value.projectType === PROJECT_TYPE_CHANGE_DETECTION
|| value.projectType === PROJECT_TYPE_COMPLETENESS;
const error = React.useMemo(
() => getErrorObject(formError),
[formError],
);
const scenarioError = React.useMemo(
() => getErrorObject(error?.scenarioPages),
[error?.scenarioPages],
);
const optionsError = React.useMemo(
() => getErrorObject(error?.customOptions),
[error?.customOptions],
);
const informationPagesError = React.useMemo(
() => getErrorObject(error?.informationPages),
[error?.informationPages],
);
const hasErrors = React.useMemo(
() => analyzeErrors(error),
[error],
);
const warning = React.useMemo(
() => {
const options = value?.customOptions?.map((option) => option.value) ?? [];
const subOptions = value?.customOptions?.flatMap(
(option) => option.subOptions?.map((subOption) => subOption.value),
) ?? [];
const selectedValues = [
...options,
...subOptions,
].filter(isDefined);
return getGeoJSONWarning(
value?.tutorialTasks,
value?.projectType,
selectedValues,
value?.zoomLevel,
);
},
[value?.tutorialTasks, value?.projectType, value?.customOptions, value?.zoomLevel],
);
const getTileServerUrl = (val: PartialTutorialFormType['tileServer']) => {
const tileServerName = val?.name;
if (isNotDefined(tileServerName)) {
return undefined;
}
if (tileServerName === 'custom') {
return val?.url;
}
return tileServerUrls[tileServerName];
};
const projectTypeEmpty = isNotDefined(value.projectType);
const {
customOptions,
informationPages,
} = value;
const handleProjectTypeChange = React.useCallback(
(newValue: ProjectType | undefined) => {
setFieldValue(undefined, 'tutorialTasks');
setFieldValue(undefined, 'scenarioPages');
setFieldValue(newValue, 'projectType');
setFieldValue(getDefaultOptions(newValue), 'customOptions');
},
[setFieldValue],
);
return (
<div className={_cs(styles.newTutorial, className)}>
<Heading level={1}>
Create New Tutorial
</Heading>
<div className={styles.container}>
<InputSection
heading="Basic Information"
>
<SegmentInput
name={'projectType' as const}
onChange={handleProjectTypeChange}
value={value.projectType}
label="Project Type"
hint="Select the type of your project."
options={projectTypeOptions}
keySelector={valueSelector}
labelSelector={labelSelector}
error={error?.projectType}
disabled={submissionPending}
/>
<TextInput
name={'name' as const}
value={value.name}
onChange={setFieldValue}
label="Name of the Tutorial"
hint="Provide a clear name for your tutorial. You can select tutorials based on their name later during the project creation."
error={error?.name}
disabled={submissionPending || projectTypeEmpty}
/>
<TextInput
name={'lookFor' as const}
value={value.lookFor}
onChange={setFieldValue}
label="Look For"
hint="What should the users look for (e.g. buildings, cars, trees)?"
error={error?.lookFor}
disabled={submissionPending || projectTypeEmpty}
autoFocus
/>
</InputSection>
{(
value.projectType === PROJECT_TYPE_FOOTPRINT
|| value.projectType === PROJECT_TYPE_STREET
) && (
<InputSection
heading="Custom Options"
actions={(
<Button
name={undefined}
icons={<MdAdd />}
onClick={handleAddDefineOptions}
disabled={
submissionPending
|| projectTypeEmpty
|| (customOptions && customOptions.length >= MAX_OPTIONS)
}
>
Add Option
</Button>
)}
>
<NonFieldError
error={optionsError}
/>
{(customOptions && customOptions.length > 0) ? (
<div className={styles.customOptionContainer}>
<div className={styles.customOptionList}>
{customOptions.map((option, index) => (
<ExpandableContainer
key={option.optionId}
header={option.title || `Option ${index + 1}`}
openByDefault={index === customOptions.length - 1}
actions={(
<Button
name={index}
onClick={onOptionRemove}
variant="action"
title="Delete Option"
disabled={
submissionPending
|| projectTypeEmpty
}
>
<IoIosTrash />
</Button>
)}
>
<CustomOptionInput
key={option.optionId}
value={option}
index={index}
onChange={setOptionValue}
error={optionsError?.[option.optionId]}
disabled={submissionPending || projectTypeEmpty}
/>
</ExpandableContainer>
))}
</div>
<CustomOptionPreview
value={customOptions}
lookFor={value.lookFor}
/>
</div>
) : (
<div>No options</div>
)}
</InputSection>
)}
<InputSection
heading="Information Pages"
actions={(
<PopupButton
componentRef={popupElementRef}
name={undefined}
icons={<MdAdd />}
label="Add Page"
popupContentClassName={styles.newInfoButtonPopup}
disabled={submissionPending || projectTypeEmpty}
>
{infoPageTemplateOptions.map((infoPageTemplate) => (
<Button
className={styles.popupItem}
name={infoPageTemplate.key}
key={infoPageTemplate.key}
onClick={handleAddInformationPage}
variant="transparent"
disabled={(
submissionPending
|| projectTypeEmpty
|| (informationPages
&& informationPages.length >= MAX_INFO_PAGES)
)}
>
{infoPageTemplate.label}
</Button>
))}
</PopupButton>
)}
>
<NonFieldError
error={informationPagesError}
/>
<div className={styles.informationPageList}>
{informationPages?.map((page, i) => (
<ExpandableContainer
key={page.pageNumber}
header={page.title || `Intro ${i + 1}`}
openByDefault={i === informationPages.length - 1}
actions={(
<Button
name={i}
onClick={onInformationPageRemove}
variant="action"
title="Delete page"
disabled={submissionPending || projectTypeEmpty}
>
<IoIosTrash />
</Button>
)}
>
<InformationPageInput
value={page}
onChange={setInformationPageValue}
lookFor={value.lookFor}
index={i}
error={informationPagesError?.[page.pageNumber]}
disabled={submissionPending || projectTypeEmpty}
/>
</ExpandableContainer>
))}
{!(informationPages?.length) && (
<EmptyMessage
title="Start adding Information pages"
description="Add pages selecting templates from “Add page” dropdown"
/>
)}
</div>
</InputSection>
{tileServerVisible && (
<InputSection
heading={tileServerBVisible ? 'Tile Server A' : 'Tile Server'}
>
<TileServerInput
name={'tileServer' as const}
value={value.tileServer}
error={error?.tileServer}
onChange={setFieldValue}
disabled={submissionPending || projectTypeEmpty}
/>
</InputSection>
)}
{tileServerBVisible && (
<InputSection
heading="Tile Server B"
>
<TileServerInput
name={'tileServerB' as const}
value={value.tileServerB}
error={error?.tileServerB}
onChange={setFieldValue}
disabled={submissionPending || projectTypeEmpty}
/>
</InputSection>
)}
{
(value.projectType === PROJECT_TYPE_BUILD_AREA
|| value.projectType === PROJECT_TYPE_CHANGE_DETECTION
|| value.projectType === PROJECT_TYPE_COMPLETENESS)
&& (
<InputSection
heading="Zoom Level"
>
<NumberInput
name={'zoomLevel' as const}
value={value.zoomLevel}
onChange={setFieldValue}
label="Zoom Level"
hint="We use the Tile Map Service zoom levels. Please check for your area which zoom level is available. For example, Bing imagery is available at zoomlevel 18 for most regions. If you use a custom tile server you may be able to use even higher zoom levels."
error={error?.zoomLevel}
disabled={submissionPending || projectTypeEmpty}
/>
</InputSection>
)
}
<InputSection
heading="Scenario Pages"
>
<GeoJsonFileInput
name={'tutorialTasks' as const}
label="Upload Scenarios as GeoJSON"
value={value.tutorialTasks}
onChange={handleGeoJsonFile}
hint="It should end with .geojson or .geo.json"
error={error?.tutorialTasks}
disabled={submissionPending || projectTypeEmpty}
/>
<div className={styles.scenarioList}>
{value.scenarioPages?.map((task, index) => (
<ExpandableContainer
key={task.scenarioId}
// NOTE: only open first scenario by default
openByDefault={index === 0}
header={`Scenario ${task.scenarioId}`}
>
<ScenarioPageInput
key={task.scenarioId}
scenarioId={task.scenarioId}
index={index}
value={task}
projectType={value.projectType}
onChange={onScenarioFormChange}
error={scenarioError?.[task.scenarioId]}
customOptions={customOptions}
geoJson={value.tutorialTasks}
urlA={getTileServerUrl(value.tileServer)}
urlB={getTileServerUrl(value.tileServerB)}
lookFor={value.lookFor}
disabled={submissionPending || projectTypeEmpty}
/>
</ExpandableContainer>
))}
{(value.scenarioPages?.length ?? 0) === 0 && (
<EmptyMessage
title="Upload geojson file first"
description="This section will automatically show scenarios after uploading geojson file"