-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathutil.ts
284 lines (240 loc) · 6.82 KB
/
util.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
import dayjs from "dayjs";
import utc from "dayjs/plugin/utc";
dayjs.extend(utc);
import { REGEX } from "./constants";
import {
AllArtistEntry,
ArtistInterface,
GenreInterface,
ShowInterface,
PastShowSchema,
} from "./types/shared";
dayjs.extend(utc);
interface PageResponse {
data: {
[key: string]: any;
};
}
export const extractPage = <T>(fetchResponse: PageResponse, key: string): T =>
fetchResponse?.data?.[key];
interface CollectionResponse {
data: {
[key: string]: {
items: any[];
};
};
}
export const extractCollection = <T>(
fetchResponse: CollectionResponse,
key: string
): T[] => fetchResponse?.data?.[key]?.items;
interface LinkedFromCollectionResponse<T = any> {
data: {
[key: string]: {
items: {
linkedFrom: {
[key: string]: {
items: T[];
};
};
}[];
};
};
}
export const extractLinkedFromCollection = <T>(
fetchResponse: LinkedFromCollectionResponse<T>,
key: string,
linkedFromKey: string
): T[] =>
fetchResponse.data[key].items.flatMap(
(item) => item.linkedFrom[linkedFromKey].items
);
export const extractCollectionItem = <T>(
fetchResponse: CollectionResponse,
key: string
): T => fetchResponse?.data?.[key]?.items?.[0];
interface GroupedArtists {
alphabet: string;
artists: AllArtistEntry[];
}
export const sortAndGroup = (data: AllArtistEntry[]): GroupedArtists[] => {
const alphaReducer = (
accumulator: {
[key: string]: {
alphabet: string;
artists: AllArtistEntry[];
};
},
current: AllArtistEntry
) => {
/**
* @note Fix for names that have a lowercase letter as the first character as well as those with accents in their names
*/
let alphabet = current.name
.trim()[0]
.toUpperCase()
.normalize("NFD")
.replace(/[\u0300-\u036f]/g, "");
if (REGEX.SPECIAL.test(alphabet) || REGEX.NUMERIC.test(alphabet))
alphabet = "#";
if (!accumulator[alphabet]) {
accumulator[alphabet] = {
alphabet,
artists: [current],
};
} else {
accumulator[alphabet].artists.push(current);
}
return accumulator;
};
const sortHashtagToEnd = (a: GroupedArtists, b: GroupedArtists) => {
if (a.alphabet === "#" || b.alphabet === "#") return -1;
if (a.alphabet < b.alphabet) return -1;
if (a.alphabet > b.alphabet) return 1;
return 0;
};
return Object.values(data.reduce(alphaReducer, {})).sort(sortHashtagToEnd);
};
export const formatArtistNames = (data: ArtistInterface[]) => {
// remove null artists who are unpublished on a published show
const filteredData = data.filter((artist) => artist !== null);
const names = filteredData.map(({ name }) => name);
if (names.length === 1) {
return `with ${names[0]}`;
}
if (names.length === 2) {
return `with ${names[0]} and ${names[1]}`;
}
if (names.length === 3) {
return `with ${names.slice(0, 2).join(", ")} and ${names[2]}`;
}
return `with ${names.slice(0, 2).join(", ")} and others`;
};
export const getMixcloudKey = (url: string) =>
url.replace("https://www.mixcloud.com", "");
export const __SERVER__ = typeof window === "undefined";
/**
* Sorting functions for Arrays
*/
export const sort = {
alpha: (a: string, b: string) =>
a.localeCompare(b, "en", { sensitivity: "base" }),
date_DESC: (
a: ShowInterface | PastShowSchema,
b: ShowInterface | PastShowSchema
) => (dayjs(a.date).isAfter(b.date) ? -1 : 1),
date_ASC: (
a: ShowInterface | PastShowSchema,
b: ShowInterface | PastShowSchema
) => (dayjs(a.date).isBefore(b.date) ? -1 : 1),
};
export const delay = (time = 1500) => {
return new Promise((resolve) => {
setTimeout(() => {
resolve(true);
}, time);
});
};
export const parseGenres = (genresCollection: { items: GenreInterface[] }) =>
genresCollection.items
.filter((genre) => Boolean(genre?.name))
.map((genre) => genre.name);
export const uniq = <T>(arr: T[]) => Array.from(new Set(arr));
export const transformForDropdown = (array) => {
return array.map((item) => ({
value: item.sys.id,
label: item.name,
// to do: remove sys and name from spread to reduce size of object
...item,
}));
};
export const showArtworkURL = (
values,
useExtraArtists = false,
regenerate = false
) => {
const images = encodeURIComponent(
values.image
.map((img) => {
return img.url;
})
.join(",")
);
const title = encodeURIComponent(values.showName);
let formattedArtists = values.artists;
if (!regenerate) {
formattedArtists = values.artists
.map((x) => x.label)
.join(", ")
.replace(/, ([^,]*)$/, " & $1");
if (useExtraArtists && values.hasExtraArtists) {
console.log("using additional artists");
const additionalArtists = values.extraArtists
.map((x) => x.name)
.join(", ")
.replace(/, ([^,]*)$/, " & $1");
formattedArtists = `${formattedArtists}, ${additionalArtists}`.replace(
/, ([^,]*)$/,
" & $1"
);
}
}
formattedArtists = encodeURIComponent(formattedArtists);
const startDate = dayjs(values.datetime).utc();
const endDate = dayjs(values.datetimeEnd).utc();
// adjusted date ensures that shows in the early hours have previous days date and colour
const adjustedDate = startDate.subtract(4, "hours");
const showDate = adjustedDate.format("ddd DD MMM");
const startTime = startDate.format("HH:mm");
const endTime = endDate.format("HH:mm");
// Format the date and time
const uriEncodedDate = encodeURIComponent(
showDate + " / " + startTime + "-" + endTime + " (CET)"
);
const colours = [
"#cd46fd",
"#defc32",
"#ff96ff",
"#f94646",
"#ffedd9",
"#ff9d1d",
"#fffe49",
"#fd339b",
"#b0b02b",
"#32fe95",
"#4ac8f4",
"#ffa2b5",
"#facc7f",
"#99fffc",
"#00cb0d",
"#99e9ff",
"#ab8dff",
"#ffd9f0",
"#fbffb3",
"#ccffd1",
"#fe6301",
"#ccd2ff",
];
// Get the day of the month
const dayOfMonth = adjustedDate.date();
// Get a color from the colours array based on the day of the month
const colour = colours[dayOfMonth % colours.length];
// Determine the base URL based on the environment
const baseUrl =
process.env.NODE_ENV === "development"
? "https://7a2a-194-126-177-76.ngrok-free.app/"
: process.env.NEXT_PUBLIC_SITE_URL;
// Set URL for social image
const url = `${baseUrl}/api/automated-artwork?title=${title}&artists=${formattedArtists}&date=${uriEncodedDate}&images=${images}&colour=${encodeURIComponent(
colour
)}`;
return url;
};
export const placeholderImage = {
sys: { id: "4njwdSvfwFLNoSZ6j1jE2G" },
title: "",
description: "",
url: "https://images.ctfassets.net/taoiy3h84mql/4njwdSvfwFLNoSZ6j1jE2G/210230cc75460ea78c4b242d1cbdb55c/default-image.jpg",
width: 2000,
height: 1340,
};