Merged develop

This commit is contained in:
Alejandro Celaya
2021-09-12 12:28:01 +02:00
5 changed files with 106 additions and 31 deletions

View File

@@ -7,7 +7,7 @@ type Ref<T> = RefObject<T> | MutableRefObject<T>;
export interface ImportServersBtnProps {
onImport?: () => void;
onImportError?: () => void;
onImportError?: (error: Error) => void;
}
interface ImportServersBtnConnectProps extends ImportServersBtnProps {

View File

@@ -1,29 +1,37 @@
import { CsvJson } from 'csvjson';
import { ServerData } from '../data';
interface CsvFile extends File {
type: 'text/csv' | 'text/comma-separated-values' | 'application/csv';
}
const validateServer = (server: any): server is ServerData =>
typeof server.url === 'string' && typeof server.apiKey === 'string' && typeof server.name === 'string';
const CSV_MIME_TYPES = [ 'text/csv', 'text/comma-separated-values', 'application/csv' ];
const isCsv = (file?: File | null): file is CsvFile => !!file && CSV_MIME_TYPES.includes(file.type);
const validateServers = (servers: any): servers is ServerData[] =>
Array.isArray(servers) && servers.every(validateServer);
export default class ServersImporter {
public constructor(private readonly csvjson: CsvJson, private readonly fileReaderFactory: () => FileReader) {}
public constructor(private readonly csvJson: CsvJson, private readonly fileReaderFactory: () => FileReader) {}
public readonly importServersFromFile = async (file?: File | null): Promise<ServerData[]> => {
if (!isCsv(file)) {
throw new Error('No file provided or file is not a CSV');
if (!file) {
throw new Error('No file provided');
}
const reader = this.fileReaderFactory();
return new Promise((resolve) => {
return new Promise((resolve, reject) => {
reader.addEventListener('loadend', (e: ProgressEvent<FileReader>) => {
const content = e.target?.result?.toString() ?? '';
const servers = this.csvjson.toObject<ServerData>(content);
try {
// TODO Read as stream, otherwise, if the file is too big, this will block the browser tab
const content = e.target?.result?.toString() ?? '';
const servers = this.csvJson.toObject(content);
resolve(servers);
if (!validateServers(servers)) {
throw new Error('Provided file does not have the right format.');
}
resolve(servers);
} catch (e) {
reject(e);
}
});
reader.readAsText(file);
});

View File

@@ -55,6 +55,7 @@ export const rangeOrIntervalToString = (range?: DateRange | DateInterval): strin
};
const startOfDaysAgo = (daysAgo: number) => startOfDay(subDays(new Date(), daysAgo));
const endingToday = (startDate: Date): DateRange => ({ startDate, endDate: endOfDay(new Date()) });
export const intervalToDateRange = (dateInterval?: DateInterval): DateRange => {
if (!dateInterval) {
@@ -63,19 +64,19 @@ export const intervalToDateRange = (dateInterval?: DateInterval): DateRange => {
switch (dateInterval) {
case 'today':
return { startDate: startOfDay(new Date()), endDate: new Date() };
return endingToday(startOfDay(new Date()));
case 'yesterday':
return { startDate: startOfDaysAgo(1), endDate: endOfDay(subDays(new Date(), 1)) };
case 'last7Days':
return { startDate: startOfDaysAgo(7), endDate: new Date() };
return endingToday(startOfDaysAgo(7));
case 'last30Days':
return { startDate: startOfDaysAgo(30), endDate: new Date() };
return endingToday(startOfDaysAgo(30));
case 'last90Days':
return { startDate: startOfDaysAgo(90), endDate: new Date() };
return endingToday(startOfDaysAgo(90));
case 'last180days':
return { startDate: startOfDaysAgo(180), endDate: new Date() };
return endingToday(startOfDaysAgo(180));
case 'last365Days':
return { startDate: startOfDaysAgo(365), endDate: new Date() };
return endingToday(startOfDaysAgo(365));
}
return {};