-
Notifications
You must be signed in to change notification settings - Fork 17
/
Copy pathload-meeting-data.ts
666 lines (598 loc) · 17.9 KB
/
load-meeting-data.ts
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
import { DateTime, WeekdayNumbers } from 'luxon';
import { flattenAndSortIndexes } from './flatten-and-sort-indexes';
import { formatAddress } from './format-address';
import { formatConferenceProvider } from './format-conference-provider';
import { formatFeedbackEmail } from './format-feedback-email';
import { formatSlug } from './format-slug';
import type { JSONData, JSONDataFlat, State, Meeting, Index } from '../types';
// set up meeting data; this is only run once when the app loads
export function loadMeetingData(
data: JSONData[],
capabilities: State['capabilities'],
settings: TSMLReactConfig,
strings: Translation,
timezone?: string
): [State['meetings'], State['indexes'], State['capabilities']] {
// meetings is a lookup
const meetings: { [index: string]: Meeting } = {};
// indexes start as objects, will be converted to arrays
const indexes: State['indexes'] = {
region: [],
time: [],
type: [],
weekday: [],
distance: [],
};
// loop through each entry
flattenDays(data).forEach(meeting => {
const {
contact_1_email,
contact_1_name,
contact_1_phone,
contact_2_email,
contact_2_name,
contact_2_phone,
contact_3_email,
contact_3_name,
contact_3_phone,
district,
edit_url,
email,
entity,
entity_location,
entity_phone,
entity_url,
group,
group_notes,
location,
location_notes,
name,
notes,
phone,
slug,
website,
} = meeting;
// slug is required and must be unique
if (!slug) {
return warn('no slug', meeting);
} else if (slug in meetings) {
return warn('duplicate slug', meeting);
}
// conference url
const conference_provider = meeting.conference_url
? formatConferenceProvider(meeting.conference_url, settings)
: undefined;
let {
address,
conference_url,
conference_phone,
conference_phone_notes,
conference_url_notes,
feedback_url,
formatted_address,
regions,
} = meeting;
if (meeting.conference_url) {
if (conference_provider) {
conference_url = meeting.conference_url;
} else {
warn('unknown conference_url', meeting);
}
}
if (!conference_url && conference_url_notes) {
conference_url_notes = undefined;
}
conference_phone = validateConferencePhone(meeting);
if (!conference_phone && conference_phone_notes) {
conference_phone_notes = undefined;
}
// creates formatted_address if necessary
if (!formatted_address) {
formatted_address = [
meeting.address,
meeting.city,
[meeting.state, meeting.postal_code].join(' ').trim(),
meeting.country,
]
.map(e => e?.trim())
.filter(e => e)
.join(', ');
if (!formatted_address) {
return warn('no address information', meeting);
}
}
// used in table
if (!address) {
address = formatAddress(formatted_address);
}
// check if approximate
let approximate, coordinates;
if (
meeting.coordinates &&
[1, 3].includes((meeting.coordinates.match(/,/g) || []).length)
) {
coordinates = meeting.coordinates.split(',');
approximate = coordinates.length !== 2;
meeting.latitude = meeting.approximate ? undefined : coordinates[0];
meeting.longitude = meeting.approximate ? undefined : coordinates[1];
} else if (typeof meeting.approximate === 'string') {
approximate = meeting.approximate.toLowerCase() === 'yes';
} else if (typeof meeting.approximate === 'boolean') {
approximate = meeting.approximate;
} else {
approximate = formatted_address.split(',').length < 4;
}
// if approximate is specified, it overrules formatAddress
if (approximate) address = undefined;
//types
let types = Array.isArray(meeting.types)
? meeting.types
.map(type =>
typeof type === 'number' ? type.toString() : type.trim()
)
.filter(type => type in strings.types && type !== 'ONL')
: [];
// add online metattype
const isOnline = !!conference_provider || !!conference_phone;
if (isOnline) types.push('online');
// add in-person metatype
const isTempClosed = types.includes('TC');
const isInPerson = !isTempClosed && !approximate;
if (isInPerson) {
types.push('in-person');
}
// add active / inactive metatypes
const isActive = isOnline || isInPerson;
if (isActive) {
types.push('active');
} else {
capabilities.inactive = true;
types.push('inactive');
}
// if meeting is not in person, remove types that only apply to in-person meetings
if (!isInPerson) {
types = types.filter(type => !settings.in_person_types.includes(type));
}
// if meeting is both speaker and discussion, combine
if (types.includes('SP') && types.includes('D')) {
types.splice(types.indexOf('SP'), 1);
types.splice(types.indexOf('D'), 1);
types.push('SPD');
}
// check location/group capability
if (
!capabilities.location &&
((isOnline && group) || (isInPerson && location))
) {
capabilities.location = true;
}
// format latitude + longitude
let latitude, longitude;
if (meeting.latitude && meeting.longitude) {
if (isInPerson) {
capabilities.coordinates = true;
latitude =
typeof meeting.latitude === 'string'
? parseFloat(meeting.latitude)
: meeting.latitude;
longitude =
typeof meeting.longitude === 'string'
? parseFloat(meeting.longitude)
: meeting.longitude;
}
}
let start: DateTime | undefined;
let end: DateTime | undefined;
let minutes_week: number | undefined;
// handle day and time
if (typeof meeting.day !== 'undefined' && meeting.time) {
//luxon uses iso day
const weekday = meeting.day === 0 ? 7 : (meeting.day as WeekdayNumbers);
const [hour, minute] = meeting.time.split(':').map(num => parseInt(num));
// timezone
if (!meeting.timezone) {
if (timezone) {
meeting.timezone = timezone;
} else {
// either tz setting or meeting tz is required
return warn(`${meeting.slug} has no timezone`, meeting);
}
}
// make start/end datetimes
start = DateTime.fromObject(
{ weekday, hour, minute },
{ zone: meeting.timezone }
);
// check valid start time
if (!start.isValid) {
return warn(`invalid start (${start.invalidExplanation})`, meeting);
}
if (meeting.end_time) {
const endTimeParts = meeting.end_time
.split(':')
.map(num => parseInt(num));
end = DateTime.fromObject(
{ weekday, hour: endTimeParts[0], minute: endTimeParts[1] },
{ zone: meeting.timezone }
);
} else {
end = start.plus({
minutes: settings.duration,
});
}
// check valid end time
if (!end.isValid) {
return warn(`invalid end (${end.invalidExplanation})`, meeting);
}
const duration = end.diff(start, 'minutes').toObject().minutes ?? 0;
if (duration > 120) {
warn(`unusually long (${duration} mins)`, meeting);
}
// normalize timezones
start = start.setZone(timezone ? timezone : 'local');
if (end) {
end = end.setZone(timezone ? timezone : 'local');
}
// day & time indexes
if (isActive) {
const weekday = settings.weekdays[
start?.weekday === 7
? 0
: (start?.weekday as keyof typeof settings.weekdays)
] as keyof typeof strings.days;
// day index
const dayIndex = indexes.weekday.findIndex(
({ key }) => key === weekday
);
if (dayIndex === -1) {
indexes.weekday.push({
key: weekday,
name: strings.days[weekday],
slugs: [slug],
});
} else {
indexes.weekday[dayIndex].slugs.push(slug);
}
// time differences for sorting
const minutes_midnight = start.hour * 60 + start.minute;
minutes_week = minutes_midnight + meeting.day * 1440;
// time index (can be multiple)
const times = [];
if (minutes_midnight >= 240 && minutes_midnight < 720) {
times.push(0); // morning (4am–11:59pm)
}
if (minutes_midnight >= 660 && minutes_midnight < 1020) {
times.push(1); // midday (11am–4:59pm)
}
if (minutes_midnight >= 960 && minutes_midnight < 1260) {
times.push(2); // evening (4pm–8:59pm)
}
if (minutes_midnight >= 1200 || minutes_midnight < 300) {
times.push(3); // night (8pm–4:59am)
}
times.forEach(time => {
const timeIndex = indexes.time.findIndex(
({ key }) => key === settings.times[time]
);
if (timeIndex === -1) {
indexes.time.push({
key: settings.times[time],
name: strings[settings.times[time]],
slugs: [slug],
});
} else {
indexes.time[timeIndex].slugs.push(slug);
}
});
}
}
// parse regions
if (!regions || !Array.isArray(regions)) {
regions = [];
if (meeting.region) {
regions.push(meeting.region);
if (meeting.sub_region) {
regions.push(meeting.sub_region);
if (meeting.sub_sub_region) {
regions.push(meeting.sub_sub_region);
}
}
} else if (meeting.city) {
regions.push(meeting.city);
}
} else {
// defend against weird values, remove empty regions
regions = regions
.filter(e => typeof e === 'string')
.map(e => e.trim())
.filter(e => e.length);
}
// build region index
if (isActive && !!regions.length) {
indexes.region = populateRegionsIndex(regions, 0, indexes.region, slug);
}
// build type index (can be multiple) -- if inactive, only index the 'inactive' type
const typesToIndex: string[] = isActive ? types : ['inactive'];
typesToIndex.forEach(type => {
const typeSlug = formatSlug(
strings.types[type as keyof typeof strings.types]
);
const typeIndex = indexes.type.findIndex(({ key }) => key === typeSlug);
if (typeIndex === -1) {
indexes.type.push({
key: typeSlug,
name: strings.types[type as keyof typeof strings.types],
slugs: [slug],
});
} else {
indexes.type[typeIndex].slugs.push(slug);
}
});
// optional updated date
let updated;
if (meeting.updated) {
updated = DateTime.fromSQL(meeting.updated).setZone(timezone);
if (!updated.isValid) {
warn(`invalid updated (${updated.invalidExplanation})`, meeting);
updated = undefined;
} else {
updated = updated.toLocaleString();
}
}
// optional url
const url =
meeting.url?.startsWith('http://') || meeting.url?.startsWith('https://')
? meeting.url
: undefined;
// build search string
const search = [
district,
formatted_address,
group,
group_notes,
location,
location_notes,
name,
notes,
regions,
]
.flat()
.filter(e => e)
.join('\t')
.toLowerCase();
let feedback_emails = settings.feedback_emails;
if (Array.isArray(meeting.feedback_emails)) {
feedback_emails = meeting.feedback_emails;
}
if ('string' === typeof meeting.feedback_emails) {
feedback_emails = meeting.feedback_emails
.split(',')
.map(e => e.trim())
.filter(e => e);
}
if (!feedback_url && feedback_emails.length) {
feedback_url = formatFeedbackEmail({
feedback_emails,
name,
edit_url,
settings,
strings,
});
}
meetings[slug] = {
address,
approximate,
conference_phone,
conference_phone_notes,
conference_provider,
conference_url,
conference_url_notes,
contact_1_email,
contact_1_name,
contact_1_phone,
contact_2_email,
contact_2_name,
contact_2_phone,
contact_3_email,
contact_3_name,
contact_3_phone,
district,
edit_url,
email,
entity,
entity_location,
entity_phone,
entity_url,
end,
feedback_url,
formatted_address,
group,
group_notes,
isActive,
isInPerson,
isOnline,
isTempClosed,
latitude,
location,
location_notes,
longitude,
minutes_week,
name: name ?? strings.unnamed_meeting,
notes,
paypal: validatePayPal(meeting),
phone,
regions,
search,
slug,
square: validateSquare(meeting),
start,
timezone: meeting.timezone,
types,
updated,
url,
venmo: validateVenmo(meeting),
website,
};
// clean up undefined
Object.keys(meetings[slug]).forEach(
key =>
meetings[slug][key as keyof Meeting] === undefined &&
delete meetings[slug][key as keyof Meeting]
);
});
// convert region to array, sort by name
indexes.region = flattenAndSortIndexes(indexes.region, (a, b) =>
a.name?.localeCompare(b.name)
);
// convert weekday to array and sort by ordinal
indexes.weekday = flattenAndSortIndexes(
indexes.weekday,
(a, b) =>
settings.weekdays.indexOf(a.key) - settings.weekdays.indexOf(b.key)
);
// convert time to array and sort by ordinal
indexes.time = flattenAndSortIndexes(
indexes.time,
// @ts-expect-error TODO
(a, b) => settings.times.indexOf(a.key) - settings.times.indexOf(b.key)
);
// convert type to array and sort by name
indexes.type = flattenAndSortIndexes(indexes.type, (a, b) =>
a.name?.localeCompare(b.name)
);
// determine capabilities (filter out options that apply to every meeting)
const meetingsCount = Object.keys(meetings).length;
['region', 'weekday', 'time', 'type'].forEach(indexKey => {
capabilities[indexKey as keyof typeof capabilities] = !!indexes[
indexKey as keyof typeof indexes
].filter((index: Index) => index.slugs.length !== meetingsCount).length;
});
// remove active type if no inactive meetings
if (!capabilities.inactive) {
// ...from the indexes
indexes.type = indexes.type.filter(type => type.key !== 'active');
// ...from each meeting
Object.keys(meetings).forEach(slug => {
meetings[slug] = {
...meetings[slug],
types: meetings[slug].types?.filter(
type => type !== strings.types.active
),
};
});
}
// determine search modes
capabilities.geolocation =
navigator.geolocation &&
capabilities.coordinates &&
(window.location.protocol === 'https:' ||
window.location.hostname === 'localhost');
// determine sharing
capabilities.sharing = typeof navigator.canShare === 'function';
return [meetings, indexes, capabilities];
}
// look for data with multiple days and make them all single
export function flattenDays(data: JSONData[]): JSONDataFlat[] {
const addMeetings: JSONDataFlat[] = [];
const removeIndexes: number[] = [];
data.forEach((meeting, index) => {
if (Array.isArray(meeting.day)) {
removeIndexes.push(index);
meeting.day.forEach(day => {
addMeetings.push({
...meeting,
day: typeof day === 'string' ? parseInt(day) : day,
slug: `${meeting.slug}-${day}`,
});
});
}
});
removeIndexes.forEach(index => data.splice(index, 1));
return data
.map(({ day, ...rest }) => {
return {
...rest,
day: typeof day === 'string' ? parseInt(day) : day,
} as JSONDataFlat;
})
.concat(addMeetings);
}
// recursive function to build an index of regions from a flat array
function populateRegionsIndex(
regions: string[],
position: number,
index: Index[],
slug: string
) {
const region = regions[position];
const regionSlug = formatSlug(regions.slice(0, position + 1).join(' '));
let regionIndex = index.findIndex(({ key }) => key === regionSlug);
if (regionIndex === -1) {
index.push({
key: regionSlug,
name: region,
slugs: [slug],
children: [],
});
regionIndex = index.length - 1;
} else {
index[regionIndex].slugs.push(slug);
}
if (regions.length > position + 1) {
index[regionIndex].children = populateRegionsIndex(
regions,
position + 1,
index[regionIndex].children ?? [],
slug
);
}
return index;
}
function validateConferencePhone(meeting: JSONDataFlat) {
const { conference_phone } = meeting;
if (conference_phone) {
const conference_phone_clean = conference_phone.replace(/[^\d,+#]/g, '');
if (conference_phone_clean.length >= 10) {
return conference_phone_clean;
}
warn(`invalid conference_phone ${conference_phone}`, meeting);
}
return undefined;
}
function validatePayPal(meeting: JSONDataFlat) {
const { paypal } = meeting;
if (paypal) {
if (/^[a-zA-Z0-9]+$/.test(paypal) && paypal.length < 21) {
return paypal;
}
warn(`invalid paypal ${paypal}`, meeting);
}
return undefined;
}
function validateSquare(meeting: JSONDataFlat) {
const { square } = meeting;
if (square) {
if (square.startsWith('$')) {
return square;
}
warn(`invalid square ${square}`, meeting);
}
return undefined;
}
function validateVenmo(meeting: JSONDataFlat) {
const { venmo } = meeting;
if (venmo) {
if (venmo.startsWith('@')) {
return venmo;
}
warn(`invalid venmo ${venmo}`, meeting);
}
return undefined;
}
// warn about bad data
function warn(issue: string, meeting: JSONDataFlat) {
const { slug, edit_url } = meeting;
console.warn(
`TSML UI ${issue}${edit_url ? `: ${edit_url}` : slug ? `: ${slug}` : ''}`
);
}