forked from EugenWay/airdrop-dapp
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutils.ts
112 lines (96 loc) · 2.78 KB
/
utils.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
import { AlertContainerFactory } from '@gear-js/react-hooks';
import { HexString } from '@polkadot/util/types';
import { Entries } from './types';
const cx = (...args: unknown[]) =>
args
.filter((arg) => typeof arg === 'string')
.join(' ')
.trim();
const getTypedEntries = <T extends object>(value: T) => Object.entries(value) as Entries<T>;
export { cx, getTypedEntries };
export function formatDate(input: string | number): string {
const date = new Date(input);
return date.toLocaleDateString('en-US', {
month: 'long',
day: 'numeric',
year: 'numeric',
});
}
// Set value in seconds
export const sleep = (s: number) => new Promise((resolve) => setTimeout(resolve, s * 1000));
export const copyToClipboard = async ({
alert,
value,
successfulText,
}: {
alert?: AlertContainerFactory;
value: string;
successfulText?: string;
}) => {
const onSuccess = () => {
if (alert) {
alert.success(successfulText || 'Copied');
}
};
const onError = () => {
if (alert) {
alert.error('Copy error');
}
};
function unsecuredCopyToClipboard(text: string) {
const textArea = document.createElement('textarea');
textArea.value = text;
document.body.appendChild(textArea);
textArea.focus();
textArea.select();
try {
document.execCommand('copy');
onSuccess();
} catch (err) {
console.error('Unable to copy to clipboard', err);
onError();
}
document.body.removeChild(textArea);
}
if (window.isSecureContext && navigator.clipboard) {
navigator.clipboard
.writeText(value)
.then(() => onSuccess())
.catch(() => onError());
} else {
unsecuredCopyToClipboard(value);
}
};
export function prettyDate(
input: number | Date | string,
options: Intl.DateTimeFormatOptions = {
dateStyle: 'long',
timeStyle: 'short',
hourCycle: 'h23',
},
locale: string = 'en-US',
) {
const date = typeof input === 'string' ? new Date(input) : input;
return new Intl.DateTimeFormat(locale, options).format(date);
}
export function trimEndSlash(url: string): string {
return url?.endsWith('/') ? url.slice(0, -1) : url;
}
export const prettyAddress = (address: HexString) => {
return address.slice(0, 6) + '...' + address.slice(-4);
};
export const withoutCommas = (value: string): string => value.replace(/,/g, '');
export function toNumber(value: string) {
return +withoutCommas(value);
}
export const isMobileDevice = /Android|webOS|iPhone|iPad|iPod|BlackBerry|IEMobile|Opera Mini/i.test(
navigator.userAgent,
);
export function base64ToUint8Array(base64: string) {
const raw = window.atob(base64);
const uint8Array = new Uint8Array(new ArrayBuffer(raw.length));
for (let i = 0; i < raw.length; i++) {
uint8Array[i] = raw.charCodeAt(i);
}
return uint8Array;
}