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.
214 lines
5.2 KiB
TypeScript
214 lines
5.2 KiB
TypeScript
5 years ago
|
// SPDX-FileCopyrightText: © 2017 EteSync Authors
|
||
|
// SPDX-License-Identifier: AGPL-3.0-only
|
||
|
|
||
4 years ago
|
import * as React from "react";
|
||
|
import * as ICAL from "ical.js";
|
||
|
import moment from "moment";
|
||
|
import { TaskPriorityType } from "./pim-types";
|
||
6 years ago
|
|
||
|
// Generic handling of input changes
|
||
|
export function handleInputChange(self: React.Component, part?: string) {
|
||
|
return (event: React.ChangeEvent<any>) => {
|
||
|
const name = event.target.name;
|
||
|
const value = event.target.value;
|
||
|
|
||
|
let newState;
|
||
|
|
||
4 years ago
|
if (event.target.type === "checkbox") {
|
||
6 years ago
|
newState = {
|
||
|
[name]: event.target.checked,
|
||
|
};
|
||
|
} else {
|
||
|
newState = {
|
||
|
[name]: value,
|
||
|
};
|
||
|
}
|
||
|
|
||
|
if (part === undefined) {
|
||
|
self.setState(newState);
|
||
|
} else {
|
||
|
self.setState({
|
||
|
[part]: {
|
||
|
...self.state[part],
|
||
|
...newState,
|
||
|
},
|
||
|
});
|
||
|
}
|
||
|
};
|
||
|
}
|
||
|
|
||
|
export function insertSorted<T>(array: T[] = [], newItem: T, key: string) {
|
||
|
if (array.length === 0) {
|
||
|
return [newItem];
|
||
|
}
|
||
|
|
||
|
for (let i = 0, len = array.length; i < len; i++) {
|
||
|
if (newItem[key] < array[i][key]) {
|
||
|
array.splice(i, 0, newItem);
|
||
|
return array;
|
||
|
}
|
||
|
}
|
||
|
|
||
|
array.push(newItem);
|
||
|
|
||
|
return array;
|
||
|
}
|
||
6 years ago
|
|
||
4 years ago
|
const allDayFormat = "dddd, LL";
|
||
|
const fullFormat = "LLLL";
|
||
6 years ago
|
|
||
|
export function formatDate(date: ICAL.Time) {
|
||
|
const mDate = moment(date.toJSDate());
|
||
|
if (date.isDate) {
|
||
|
return mDate.format(allDayFormat);
|
||
|
} else {
|
||
|
return mDate.format(fullFormat);
|
||
|
}
|
||
|
}
|
||
|
|
||
6 years ago
|
export function formatDateRange(start: ICAL.Time, end: ICAL.Time) {
|
||
|
const mStart = moment(start.toJSDate());
|
||
|
const mEnd = moment(end.toJSDate());
|
||
|
let strStart;
|
||
|
let strEnd;
|
||
|
|
||
|
// All day
|
||
|
if (start.isDate) {
|
||
4 years ago
|
if (mEnd.diff(mStart, "days", true) === 1) {
|
||
6 years ago
|
return mStart.format(allDayFormat);
|
||
|
} else {
|
||
|
strStart = mStart.format(allDayFormat);
|
||
4 years ago
|
strEnd = mEnd.clone().subtract(1, "day").format(allDayFormat);
|
||
6 years ago
|
}
|
||
4 years ago
|
} else if (mStart.isSame(mEnd, "day")) {
|
||
6 years ago
|
strStart = mStart.format(fullFormat);
|
||
4 years ago
|
strEnd = mEnd.format("LT");
|
||
6 years ago
|
|
||
|
if (mStart.isSame(mEnd)) {
|
||
|
return strStart;
|
||
|
}
|
||
|
} else {
|
||
|
strStart = mStart.format(fullFormat);
|
||
|
strEnd = mEnd.format(fullFormat);
|
||
|
}
|
||
|
|
||
4 years ago
|
return strStart + " - " + strEnd;
|
||
6 years ago
|
}
|
||
5 years ago
|
|
||
|
export function formatOurTimezoneOffset() {
|
||
|
let offset = new Date().getTimezoneOffset();
|
||
4 years ago
|
const prefix = (offset > 0) ? "-" : "+";
|
||
5 years ago
|
offset = Math.abs(offset);
|
||
|
const hours = Math.floor(offset / 60);
|
||
|
const minutes = offset % 60;
|
||
|
|
||
4 years ago
|
return `GMT${prefix}${hours.toString().padStart(2, "0")}:${minutes.toString().padStart(2, "0")}`;
|
||
5 years ago
|
}
|
||
5 years ago
|
|
||
|
export function getCurrentTimezone() {
|
||
|
return Intl.DateTimeFormat().resolvedOptions().timeZone;
|
||
|
}
|
||
5 years ago
|
|
||
|
export function mapPriority(priority: number): TaskPriorityType {
|
||
|
if (priority > 0 && priority < 5) {
|
||
|
return TaskPriorityType.High;
|
||
|
} else if (priority === 5) {
|
||
|
return TaskPriorityType.Medium;
|
||
|
} else if (priority > 5 && priority < 10) {
|
||
|
return TaskPriorityType.Low;
|
||
|
} else {
|
||
|
return TaskPriorityType.Undefined;
|
||
|
}
|
||
4 years ago
|
}
|
||
|
|
||
4 years ago
|
export function* arrayToChunkIterator<T>(arr: T[], size: number) {
|
||
|
for (let i = 0 ; i < arr.length ; i += size) {
|
||
|
yield arr.slice(i, i + size);
|
||
|
}
|
||
|
}
|
||
|
|
||
4 years ago
|
export function isPromise(x: any): x is Promise<any> {
|
||
|
return x && typeof x.then === "function";
|
||
|
}
|
||
|
|
||
|
export function isDefined<T>(x: T | undefined): x is T {
|
||
|
return x !== undefined;
|
||
|
}
|
||
|
|
||
|
export function startTask<T = any>(func: () => Promise<T> | T, delay = 0): Promise<T> {
|
||
|
return new Promise((resolve, reject) => {
|
||
|
setTimeout(
|
||
|
() => {
|
||
|
try {
|
||
|
const ret = func();
|
||
|
if (isPromise(ret)) {
|
||
|
ret.then(resolve)
|
||
|
.catch(reject);
|
||
|
} else {
|
||
|
resolve(ret);
|
||
|
}
|
||
|
} catch (e) {
|
||
|
reject(e);
|
||
|
}
|
||
|
},
|
||
|
delay);
|
||
|
});
|
||
|
}
|
||
|
|
||
4 years ago
|
export function usePromiseMemo<T>(promise: Promise<T> | undefined | null, deps: React.DependencyList, initial: T | undefined = undefined): T | undefined {
|
||
|
const [val, setVal] = React.useState<T>((promise as any)._returnedValue ?? initial);
|
||
|
React.useEffect(() => {
|
||
|
let cancel = false;
|
||
|
if (promise === undefined || promise === null) {
|
||
|
return undefined;
|
||
|
}
|
||
|
promise.then((val) => {
|
||
|
(promise as any)._returnedValue = val;
|
||
|
if (!cancel) {
|
||
|
setVal(val);
|
||
|
}
|
||
|
});
|
||
|
return () => {
|
||
|
cancel = true;
|
||
|
};
|
||
|
}, [...deps, promise]);
|
||
|
return val;
|
||
|
}
|
||
4 years ago
|
|
||
|
export function parseDate(prop: ICAL.Property) {
|
||
|
const value = prop.getFirstValue();
|
||
|
if ((value.day !== null) && (value.day !== undefined)) {
|
||
|
return {
|
||
|
day: value.day,
|
||
|
month: value.month - 1,
|
||
|
year: value.year ?? undefined,
|
||
|
};
|
||
|
} else {
|
||
|
const time = prop.toJSON()[3];
|
||
|
if (time.length === 6 && time.startsWith("--")) {
|
||
|
return {
|
||
|
day: parseInt(time.slice(4, 6)),
|
||
|
month: parseInt(time.slice(2, 4)) - 1,
|
||
|
};
|
||
|
} else if (time.length === 8) {
|
||
|
return {
|
||
|
day: parseInt(time.slice(6, 8)),
|
||
|
month: parseInt(time.slice(4, 6)) - 1,
|
||
|
year: parseInt(time.slice(0, 4)),
|
||
|
};
|
||
|
}
|
||
|
}
|
||
|
|
||
|
return {};
|
||
|
}
|
||
4 years ago
|
|
||
|
export const PASSWORD_MIN_LENGTH = 8;
|
||
|
|
||
|
export function enforcePasswordRules(password: string): string | undefined {
|
||
|
|
||
|
if (password.length < PASSWORD_MIN_LENGTH) {
|
||
|
return `Passwourds should be at least ${PASSWORD_MIN_LENGTH} digits long.`;
|
||
|
}
|
||
|
return undefined;
|
||
|
}
|