Remove more redundant code.

master
Tom Hacohen 4 years ago
parent 0343c80fb9
commit 8fb50df017

@ -20,7 +20,7 @@ import ConfirmationDialog from "../widgets/ConfirmationDialog";
import * as Etebase from "etebase";
import ColorPicker from "../widgets/ColorPicker";
import { defaultColor } from "../journal-processors";
import { defaultColor } from "../Pim/helpers";
import { CachedCollection } from "../Pim/helpers";
import { useCredentials } from "../credentials";
import { getCollectionManager } from "../etebase-helpers";

@ -11,6 +11,8 @@ import * as Etebase from "etebase";
import { PimType } from "../pim-types";
export const defaultColor = "#8BC34A";
export interface CachedCollection {
collection: Etebase.Collection;
metadata: Etebase.CollectionMetadata;

@ -1,73 +0,0 @@
// SPDX-FileCopyrightText: © 2017 EteSync Authors
// SPDX-License-Identifier: AGPL-3.0-only
import * as EteSync from "etesync";
import { CredentialsData, UserInfoData } from "./store";
import { addEntries } from "./store/actions";
export function createJournalEntry(
etesync: CredentialsData,
userInfo: UserInfoData,
journal: EteSync.Journal,
prevUid: string | null,
action: EteSync.SyncEntryAction,
content: string) {
const syncEntry = new EteSync.SyncEntry();
syncEntry.action = action;
syncEntry.content = content;
return createJournalEntryFromSyncEntry(etesync, userInfo, journal, prevUid, syncEntry);
}
export function createJournalEntryFromSyncEntry(
etesync: CredentialsData,
userInfo: UserInfoData,
journal: EteSync.Journal,
prevUid: string | null,
syncEntry: EteSync.SyncEntry) {
const derived = etesync.encryptionKey;
const keyPair = userInfo.getKeyPair(userInfo.getCryptoManager(derived));
const cryptoManager = journal.getCryptoManager(derived, keyPair);
const entry = new EteSync.Entry();
entry.setSyncEntry(cryptoManager, syncEntry, prevUid);
return entry;
}
export function addJournalEntry(
etesync: CredentialsData,
userInfo: UserInfoData,
journal: EteSync.Journal,
prevUid: string | null,
action: EteSync.SyncEntryAction,
content: string) {
const entry = createJournalEntry(etesync, userInfo, journal, prevUid, action, content);
return addEntries(etesync, journal.uid, [entry], prevUid);
}
/**
* Adds multiple journal entries and uploads them all at once
* @param updates list of tuples with shape (action, content)
*/
export function addJournalEntries(
etesync: CredentialsData,
userInfo: UserInfoData,
journal: EteSync.Journal,
lastUid: string | null,
updates: [EteSync.SyncEntryAction, string][]) {
let prevUid = lastUid;
const entries = updates.map(([action, content]) => {
const entry = createJournalEntry(etesync, userInfo, journal, prevUid, action, content);
prevUid = entry.uid;
return entry;
});
return addEntries(etesync, journal.uid, entries, lastUid);
}

@ -1,25 +0,0 @@
// SPDX-FileCopyrightText: © 2017 EteSync Authors
// SPDX-License-Identifier: AGPL-3.0-only
import { colorHtmlToInt, colorIntToHtml } from "./journal-processors";
it("Color conversion", () => {
const testColors = [
"#aaaaaaaa",
"#00aaaaaa",
"#0000aaaa",
"#000000aa",
"#00000000",
"#bb00bbbb",
"#bb0000bb",
"#bb000000",
"#11110011",
"#11110000",
"#11111100",
];
for (const color of testColors) {
expect(color).toEqual(colorIntToHtml(colorHtmlToInt(color)));
}
});

@ -1,140 +0,0 @@
// SPDX-FileCopyrightText: © 2017 EteSync Authors
// SPDX-License-Identifier: AGPL-3.0-only
import { List } from "immutable";
import { EventType, ContactType, TaskType } from "./pim-types";
import { store } from "./store";
import { appendError } from "./store/actions";
import * as EteSync from "etesync";
export function syncEntriesToItemMap(
collection: EteSync.CollectionInfo, entries: List<EteSync.SyncEntry>, base: {[key: string]: ContactType} = {}) {
const items = base;
const errors: Error[] = [];
entries.forEach((syncEntry) => {
// FIXME: this is a terrible hack to handle parsing errors
let comp;
try {
comp = ContactType.parse(syncEntry.content);
} catch (e) {
e.message = `${e.message}\nWhile processing: ${syncEntry.content}`;
errors.push(e);
return;
}
// FIXME:Hack
(comp as any).journalUid = collection.uid;
const uid = `${collection.uid}|${comp.uid}`;
if ((syncEntry.action === EteSync.SyncEntryAction.Add) ||
(syncEntry.action === EteSync.SyncEntryAction.Change)) {
items[uid] = comp;
} else if (syncEntry.action === EteSync.SyncEntryAction.Delete) {
delete items[uid];
}
});
if (errors.length > 0) {
store.dispatch(appendError(undefined as any, errors));
}
return items;
}
export const defaultColor = "#8BC34A";
export function colorIntToHtml(color?: number) {
if (color === undefined) {
return defaultColor;
}
// tslint:disable:no-bitwise
const blue = color & 0xFF;
const green = (color >> 8) & 0xFF;
const red = (color >> 16) & 0xFF;
const alpha = (color >> 24) & 0xFF;
// tslint:enable
function toHex(num: number) {
const ret = num.toString(16);
return (ret.length === 1) ? "0" + ret : ret;
}
return "#" + toHex(red) + toHex(green) + toHex(blue) + toHex(alpha);
}
export function colorHtmlToInt(color?: string) {
if (!color) {
color = defaultColor;
}
const match = color.match(/^#([0-9a-f]{2})([0-9a-f]{2})([0-9a-f]{2})([0-9a-f]{2})?$/i);
if (!match) {
return undefined;
}
const r = parseInt(match[1], 16);
const g = parseInt(match[2], 16);
const b = parseInt(match[3], 16);
const a = (match[4]) ? parseInt(match[4], 16) : 0xFF;
return (b | (g << 8) | (r << 16) | (a << 24));
}
function syncEntriesToCalendarItemMap<T extends EventType>(
ItemType: any,
collection: EteSync.CollectionInfo, entries: List<EteSync.SyncEntry>, base: {[key: string]: T} = {}) {
const items = base;
const color = colorIntToHtml(collection.color);
const errors: Error[] = [];
entries.forEach((syncEntry) => {
// FIXME: this is a terrible hack to handle parsing errors
let comp;
try {
comp = ItemType.parse(syncEntry.content);
} catch (e) {
e.message = `${e.message}\nWhile processing: ${syncEntry.content}`;
errors.push(e);
return;
}
if (comp === null) {
return;
}
comp.color = color;
// FIXME:Hack
(comp as any).journalUid = collection.uid;
const uid = `${collection.uid}|${comp.uid}`;
if ((syncEntry.action === EteSync.SyncEntryAction.Add) ||
(syncEntry.action === EteSync.SyncEntryAction.Change)) {
items[uid] = comp;
} else if (syncEntry.action === EteSync.SyncEntryAction.Delete) {
delete items[uid];
}
});
if (errors.length > 0) {
store.dispatch(appendError(undefined as any, errors));
}
return items;
}
export function syncEntriesToEventItemMap(
collection: EteSync.CollectionInfo, entries: List<EteSync.SyncEntry>, base: {[key: string]: EventType} = {}) {
return syncEntriesToCalendarItemMap<EventType>(EventType, collection, entries, base);
}
export function syncEntriesToTaskItemMap(
collection: EteSync.CollectionInfo, entries: List<EteSync.SyncEntry>, base: {[key: string]: TaskType} = {}) {
return syncEntriesToCalendarItemMap<TaskType>(TaskType, collection, entries, base);
}

@ -4,7 +4,6 @@
import * as React from "react";
import ColorBox from "./ColorBox";
import { colorHtmlToInt } from "../journal-processors";
import { TextField, ButtonBase } from "@material-ui/core";
interface PropsType {
@ -55,7 +54,7 @@ export default function ColorPicker(props: PropsType) {
<ColorBox
style={{ display: "inline-block" }}
size={36}
color={(color && colorHtmlToInt(color)) ? color : props.defaultColor}
color={color}
/>
<TextField
style={{ marginLeft: 10, flex: 1 }}
@ -69,4 +68,4 @@ export default function ColorPicker(props: PropsType) {
</div>
</div>
);
}
}

Loading…
Cancel
Save