-
Notifications
You must be signed in to change notification settings - Fork 9
Expand file tree
/
Copy pathutility.ts
More file actions
223 lines (182 loc) · 6.48 KB
/
utility.ts
File metadata and controls
223 lines (182 loc) · 6.48 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
import { TableCellUser, TableCellValue, TableFormView } from 'mobx-lark';
import { formatDate } from 'web-utility';
import type { HackathonScheduleTone } from './Schedule';
import { i18n, I18nKey } from '../../../models/Translation';
export const AgendaTypeClassMap: Partial<Record<string, HackathonScheduleTone>> = {
workshop: 'formation',
formation: 'formation',
presentation: 'enrollment',
enrollment: 'enrollment',
coding: 'competition',
competition: 'competition',
break: 'break',
ceremony: 'evaluation',
evaluation: 'evaluation',
};
export const buildAgendaTypeLabelMap = ({
t,
}: Pick<typeof i18n, 't'>): Partial<Record<string, string>> => ({
workshop: t('workshop'),
presentation: t('presentation'),
coding: t('coding'),
break: t('break'),
ceremony: t('ceremony'),
enrollment: t('enrollment'),
formation: t('formation'),
competition: t('competition'),
evaluation: t('evaluation'),
});
export const isPublicForm = ({ shared_limit }: TableFormView) =>
['anyone_editable'].includes(shared_limit as string);
type NamedLike = { name?: string | null };
type TextLike = TableCellValue | NamedLike | null | undefined;
type TextListLike = TextLike | TextLike[];
const textOf = (value: TextLike) => {
if (value === null || value === undefined) return '';
if (typeof value === 'boolean') return '';
if (typeof value === 'object' && !Array.isArray(value)) {
const {
name,
text,
value: primitiveValue,
displayName,
display_name,
title,
content,
plainText,
plain_text,
user,
} = value as NamedLike & {
text?: string | null;
value?: string | number | null;
displayName?: string | null;
display_name?: string | null;
title?: string | null;
content?: string | null;
plainText?: string | null;
plain_text?: string | null;
user?: {
name?: string | null;
displayName?: string | null;
display_name?: string | null;
} | null;
};
const candidate = [
name,
text,
primitiveValue,
displayName,
display_name,
title,
content,
plainText,
plain_text,
user?.displayName,
user?.display_name,
user?.name,
].find(item => item !== null && item !== undefined && `${item}`.trim());
return candidate === null || candidate === undefined ? '' : `${candidate}`.trim();
}
const text = value.toString().trim();
return text === '[object Object]' ? '' : text;
};
export const firstTextOf = (value: TextListLike) =>
(Array.isArray(value) ? value.map(textOf).find(Boolean) : textOf(value)) || '';
export const textListOf = (value: TextListLike) =>
(Array.isArray(value) ? value : [value]).map(textOf).filter(Boolean);
export const relationNameOf = (value: TextListLike) => firstTextOf(value);
export const userOf = (value?: TableCellValue | TableCellUser) =>
value && typeof value === 'object' && !Array.isArray(value) && 'name' in value
? (value as TableCellUser)
: undefined;
export const formatMoment = (value?: TableCellValue) => (value ? formatDate(value as string) : '');
export const formatPeriod = (startedAt?: TableCellValue, endedAt?: TableCellValue) =>
[formatMoment(startedAt), formatMoment(endedAt)].filter(Boolean).join(' - ');
export const timeOf = (value?: TableCellValue) => {
if (value instanceof Date) return value.getTime();
if (typeof value === 'number') return Number.isFinite(value) ? value : NaN;
const text = firstTextOf(value as TextListLike);
if (!text) return NaN;
const time = Date.parse(text);
return Number.isFinite(time) ? time : NaN;
};
export interface CountdownWindow {
startedAt?: TableCellValue;
endedAt?: TableCellValue;
}
export const resolveCountdownState = <T extends CountdownWindow>(
items: T[],
referenceTime: number,
startTime?: TableCellValue,
endTime?: TableCellValue,
) => {
const nextItem = items.find(({ startedAt, endedAt }) => {
const started = timeOf(startedAt);
const ended = timeOf(endedAt);
return Number.isFinite(started) && Number.isFinite(ended) && referenceTime <= ended;
});
const nextStartedAt = timeOf(nextItem?.startedAt);
const nextCountdownTarget =
Number.isFinite(nextStartedAt) && nextStartedAt > referenceTime
? nextItem?.startedAt
: nextItem?.endedAt;
const fallbackCountdownTarget = timeOf(startTime) > referenceTime ? startTime : endTime;
const countdownTo =
firstTextOf(nextCountdownTarget as TextListLike) ||
firstTextOf(fallbackCountdownTarget as TextListLike) ||
undefined;
return { nextItem, countdownTo };
};
export const previewText = (items: TableCellValue[], fallback: string) =>
items
.map(item => textOf(item))
.filter(Boolean)
.slice(0, 2)
.join(' · ') || fallback;
export const agendaToneClassOf = (type: TableCellValue, index: number) => {
const normalized = type?.toString().toLowerCase() || '';
const fallbackOrder: HackathonScheduleTone[] = [
'formation',
'enrollment',
'competition',
'break',
'evaluation',
];
return AgendaTypeClassMap[normalized] || fallbackOrder[index % fallbackOrder.length];
};
export const agendaTypeLabelOf = (
type: TableCellValue,
t: (key: I18nKey) => string,
fallback = '-',
) => {
const normalized = type?.toString().toLowerCase() || '';
return buildAgendaTypeLabelMap({ t })[normalized] || type?.toString() || fallback;
};
export const compactSummaryOf = (
text: TableCellValue | string[] | string | undefined,
fallback: string,
limit = 96,
) => {
const source = Array.isArray(text)
? text
.map(item => textOf(item))
.filter(Boolean)
.join(' · ')
: textOf(text);
const normalized = source.replace(/\s+/g, ' ').trim();
if (!normalized) return fallback;
return normalized.length > limit ? `${normalized.slice(0, limit).trim()}...` : normalized;
};
export const dateKeyOf = (value?: TableCellValue) => {
const dateText = formatMoment(value);
return dateText ? dateText.slice(5, 10).replace(/\//g, '-') : '';
};
export const compactDateKeyOf = (value?: TableCellValue) => dateKeyOf(value).replace('-', '.');
export const daysBetween = (startedAt?: TableCellValue, endedAt?: TableCellValue) => {
const start = timeOf(startedAt);
const end = timeOf(endedAt);
if (!Number.isFinite(start) || !Number.isFinite(end) || end < start) return 0;
return Math.max(1, Math.ceil((end - start) / (24 * 60 * 60 * 1000)));
};
export const normalizeAgendaType = (value?: TableCellValue) =>
value?.toString().toLowerCase() || '';