You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
84 lines
1.8 KiB
TypeScript
84 lines
1.8 KiB
TypeScript
import formatDistance from 'date-fns/formatDistance';
|
|
import timestamp_pb from 'google-protobuf/google/protobuf/timestamp_pb';
|
|
import { toDate } from './Api';
|
|
import { fromResource, lazyObservable } from 'mobx-utils';
|
|
import { toast } from './components/Toast';
|
|
|
|
export function sleep(seconds: number) {
|
|
return new Promise((resolve) => {
|
|
setTimeout(() => {
|
|
resolve();
|
|
}, seconds * 1000);
|
|
});
|
|
}
|
|
|
|
export function lastSeen(timestamp: timestamp_pb.Timestamp.AsObject | undefined): string {
|
|
if (timestamp === undefined) {
|
|
return 'Never';
|
|
}
|
|
return formatDistance(toDate(timestamp), new Date(), {
|
|
addSuffix: true,
|
|
});
|
|
}
|
|
|
|
export function lazy<T>(cb: () => Promise<T>) {
|
|
const resource = lazyObservable<T>(async (sink) => {
|
|
sink(await cb());
|
|
});
|
|
|
|
return {
|
|
get current() {
|
|
return resource.current();
|
|
},
|
|
refresh: async () => {
|
|
resource.refresh();
|
|
},
|
|
};
|
|
}
|
|
|
|
export function autorefresh<T>(seconds: number, cb: () => Promise<T>) {
|
|
let running = false;
|
|
let sink: ((next: T) => void) | undefined;
|
|
|
|
const resource = fromResource<T>(
|
|
async (s) => {
|
|
sink = s;
|
|
running = true;
|
|
while (running) {
|
|
sink(await cb());
|
|
await sleep(seconds);
|
|
}
|
|
},
|
|
() => {
|
|
running = false;
|
|
},
|
|
);
|
|
|
|
return {
|
|
get current() {
|
|
return resource.current();
|
|
},
|
|
refresh: async () => {
|
|
if (sink) {
|
|
sink(await cb());
|
|
}
|
|
},
|
|
dispose: () => {
|
|
resource.dispose();
|
|
},
|
|
};
|
|
}
|
|
|
|
export function setClipboard(text: string) {
|
|
const textarea = document.createElement('textarea');
|
|
textarea.value = text;
|
|
document.body.appendChild(textarea);
|
|
textarea.select();
|
|
document.execCommand('copy');
|
|
document.body.removeChild(textarea);
|
|
toast({
|
|
intent: 'success',
|
|
text: 'Added to clipboard',
|
|
});
|
|
}
|