-
Notifications
You must be signed in to change notification settings - Fork 176
/
utils.tsx
395 lines (350 loc) · 10.4 KB
/
utils.tsx
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
import { format as fnsFormat, formatDistanceToNowStrict } from 'date-fns';
import { useRouter } from 'next/router';
import { useState, useEffect, useRef } from 'react';
import Convert from 'ansi-to-html';
import type { MutableRefObject } from 'react';
import { dateRangeToString } from './timeQuery';
export function generateSearchUrl({
query,
dateRange,
lineId,
isUTC,
savedSearchId,
}: {
savedSearchId?: string;
query?: string;
dateRange?: [Date, Date];
lineId?: string;
isUTC?: boolean;
}) {
const fromDate = dateRange ? dateRange[0] : new Date();
const toDate = dateRange ? dateRange[1] : new Date();
const qparams = new URLSearchParams({
q: query ?? '',
from: fromDate.getTime().toString(),
to: toDate.getTime().toString(),
tq: dateRangeToString([fromDate, toDate], isUTC ?? false),
...(lineId ? { lid: lineId } : {}),
});
return `/search${
savedSearchId != null ? `/${savedSearchId}` : ''
}?${qparams.toString()}`;
}
export function useFirstNonNullValue<T>(value: T): T {
const [firstNonNullValue, setFirstNonNullValue] = useState<T>(value);
useEffect(() => {
if (value != null) {
setFirstNonNullValue(v => (v == null ? value : v));
}
}, [value]);
return firstNonNullValue;
}
// From: https://usehooks.com/useWindowSize/
export function useWindowSize() {
// Initialize state with undefined width/height so server and client renders match
// Learn more here: https://joshwcomeau.com/react/the-perils-of-rehydration/
const [windowSize, setWindowSize] = useState<{
width: number | undefined;
height: number | undefined;
}>({
width: undefined,
height: undefined,
});
useEffect(() => {
// Handler to call on window resize
function handleResize() {
// Set window width/height to state
setWindowSize({
width: window.innerWidth,
height: window.innerHeight,
});
}
// Add event listener
window.addEventListener('resize', handleResize);
// Call handler right away so state gets updated with initial window size
handleResize();
// Remove event listener on cleanup
return () => window.removeEventListener('resize', handleResize);
}, []); // Empty array ensures that effect is only run on mount
return windowSize;
}
export const isValidUrl = (input: string) => {
try {
new URL(input);
return true;
} catch (err) {
return false;
}
};
export const capitalizeFirstLetter = (input: string) => {
return input.charAt(0).toUpperCase() + input.slice(1);
};
export const getShortUrl = (url: string) => {
try {
const parsedUrl = new URL(url);
const urlSplit = (parsedUrl.pathname || url)
?.split('/')
.filter((v: string) => v.length > 0); // Get rid of empty string
let shortUrl = `/${urlSplit[urlSplit.length - 1] ?? url}`;
for (let i = 3; i > 0; i--) {
const urlSuffix = `/${urlSplit.slice(-1 * i).join('/')}`;
if (urlSuffix.length < 25) {
shortUrl = urlSuffix;
break;
}
}
return shortUrl;
} catch (e) {
return '';
}
};
export const useDebugMode = () => {
const { query } = useRouter();
return Boolean(query.debugMode) || Boolean(query.debug);
};
const returnFalse = () => false;
// From: https://usehooks.com/useDebounce/
export const useDebounce = <T,>(
value: T,
delay: number,
immediate?: (value: T) => boolean,
) => {
// State and setters for debounced value
const [debouncedValue, setDebouncedValue] = useState<T>(value);
const shouldBeImmediate = (immediate ?? returnFalse)(value);
useEffect(
() => {
if (shouldBeImmediate) {
setDebouncedValue(value);
return () => {};
}
// Update debounced value after delay
const handler = setTimeout(() => {
setDebouncedValue(value);
}, delay);
// Cancel the timeout if value changes (also on delay change or unmount)
// This is how we prevent debounced value from updating if value is changed ...
// .. within the delay period. Timeout gets cleared and restarted.
return () => {
clearTimeout(handler);
};
},
[value, delay, shouldBeImmediate], // Only re-call effect if value or delay changes
);
if (shouldBeImmediate) {
return value;
}
return debouncedValue;
};
export function useLocalStorage<T>(key: string, initialValue: T) {
// State to store our value
// Pass initial state function to useState so logic is only executed once
const [storedValue, setStoredValue] = useState<T>(initialValue);
// Fetch the value on client-side to avoid SSR issues
useEffect(() => {
if (typeof window === 'undefined') {
return;
}
try {
// Get from local storage by key
const item = window.localStorage.getItem(key);
// Parse stored json or if none return initialValue
if (item != null) {
setStoredValue(JSON.parse(item));
}
} catch (error) {
// If error also return initialValue
console.log(error);
}
}, [key]);
// Return a wrapped version of useState's setter function that ...
// ... persists the new value to localStorage.
const setValue = (value: T | Function) => {
if (typeof window === 'undefined') {
return;
}
try {
// Allow value to be a function so we have same API as useState
const valueToStore =
value instanceof Function ? value(storedValue) : value;
// Save state
setStoredValue(valueToStore);
// Save to local storage
window.localStorage.setItem(key, JSON.stringify(valueToStore));
} catch (error) {
// A more advanced implementation would handle the error case
console.log(error);
}
};
return [storedValue, setValue] as const;
}
export function truncateText(
text: string,
maxLength: number,
suffix?: string,
endPattern?: RegExp,
) {
const patternIndex = endPattern ? text.search(endPattern) : -1;
// Return truncated text at index
if (patternIndex >= 0 && patternIndex < maxLength) {
return text.substring(0, patternIndex) + '...';
}
if (text.length <= maxLength) {
return text;
}
return text.substring(0, maxLength) + (suffix ?? '...');
}
export function formatDistanceToNowStrictShort(date: Date) {
return formatDistanceToNowStrict(date)
.replace(' month', 'mo.')
.replace(' days', 'd')
.replace(' day', 'd')
.replace(' hours', 'h')
.replace(' hour', 'h')
.replace(' minutes', 'm')
.replace(' minute', 'm')
.replace(' seconds', 's');
}
export function formatHumanReadableDate(date: Date) {
return fnsFormat(date, 'MMMM d, h:mmaaa');
}
export const getLogLevelClass = (lvl: string | undefined) => {
const level = lvl?.toLowerCase();
if (level == null) {
return undefined;
}
return level.startsWith('emerg') ||
level.startsWith('alert') ||
level.startsWith('crit') ||
level.startsWith('err') ||
level.startsWith('fatal')
? 'error'
: level.startsWith('warn')
? 'warn'
: level.startsWith('info') ||
level.startsWith('debug') ||
level.startsWith('ok') ||
level.startsWith('notice') ||
level.startsWith('verbose') ||
level.startsWith('trace')
? 'info'
: undefined;
};
const COLORS = [
'#d5dade', // White
'#20c997', // Green
'#0dcaf0', // Turqoise
'#8250dc', // Light Purple
'#cdad7a', // Tan
'#6610f2', // Purple
'#0d6efd', // Blue
'#fd7e14', // Orange
'#828c95', // Grey
'#ff9382', // Coral
'#39b5ab', // Olive-tealish?
'#ffa600', // Yellow
// '#d63384', // Magenta, too close to red
];
export function hashCode(str: string) {
let hash = 0,
i,
chr;
if (str.length === 0) return hash;
for (i = 0; i < str.length; i++) {
chr = str.charCodeAt(i);
hash = (hash << 5) - hash + chr;
hash |= 0; // Convert to 32bit integer
}
return hash;
}
const keyedColor = (key: string | number | undefined) => {
const num = Math.floor(Math.abs(hashCode(`${key}` ?? '')));
return COLORS[num % COLORS.length];
};
// Try to match log levels to colors
export const semanticKeyedColor = (key: string | number | undefined) => {
const logLevel = getLogLevelClass(`${key}`);
if (logLevel != null) {
return logLevel === 'error'
? '#d63384' // magenta
: logLevel === 'warn'
? '#ffc107' // yellow
: '#20c997'; // green;
}
return keyedColor(key);
};
export const truncateMiddle = (str: string, maxLen = 10) => {
if (str.length <= maxLen) {
return str;
}
return `${str.slice(0, (maxLen - 2) / 2)}..${str.slice(
(-1 * (maxLen - 2)) / 2,
)}`;
};
export const useIsBlog = () => {
const router = useRouter();
return router?.pathname.startsWith('/blog');
};
export const useIsDocs = () => {
const router = useRouter();
return router?.pathname.startsWith('/docs');
};
export const useIsTerms = () => {
const router = useRouter();
return router?.pathname.startsWith('/terms');
};
export const usePrevious = <T,>(value: T): T | undefined => {
const ref = useRef<T>();
useEffect(() => {
ref.current = value;
});
return ref.current;
};
// From https://javascript.plainenglish.io/how-to-make-a-simple-custom-usedrag-react-hook-6b606d45d353
export const useDrag = (
ref: MutableRefObject<HTMLDivElement | null>,
deps = [],
options: {
onDrag?: (e: PointerEvent) => any;
onPointerDown?: (e: PointerEvent) => any;
onPointerUp?: (e: PointerEvent) => any;
onPointerMove?: (e: PointerEvent) => any;
},
) => {
const {
onPointerDown = () => {},
onPointerUp = () => {},
onPointerMove = () => {},
onDrag = () => {},
} = options;
const [isDragging, setIsDragging] = useState(false);
const handlePointerDown = (e: PointerEvent) => {
setIsDragging(true);
onPointerDown(e);
};
const handlePointerUp = (e: PointerEvent) => {
setIsDragging(false);
onPointerUp(e);
};
const handlePointerMove = (e: PointerEvent) => {
onPointerMove(e);
if (isDragging) {
onDrag(e);
}
};
useEffect(() => {
const element = ref.current;
if (element) {
element.addEventListener('pointerdown', handlePointerDown);
element.addEventListener('pointerup', handlePointerUp);
element.addEventListener('pointermove', handlePointerMove);
return () => {
element.removeEventListener('pointerdown', handlePointerDown);
element.removeEventListener('pointerup', handlePointerUp);
element.removeEventListener('pointermove', handlePointerMove);
};
}
return () => {};
}, [...deps, isDragging]);
return { isDragging };
};