From 8fb50df01720acc47849b6db2482e52d4f5a46e2 Mon Sep 17 00:00:00 2001 From: Tom Hacohen Date: Thu, 6 Aug 2020 10:25:23 +0300 Subject: [PATCH] Remove more redundant code. --- src/Collections/CollectionEdit.tsx | 2 +- src/Pim/helpers.tsx | 2 + src/etesync-helpers.ts | 73 --------------- src/journal-processors.test.ts | 25 ------ src/journal-processors.ts | 140 ----------------------------- src/widgets/ColorPicker.tsx | 5 +- 6 files changed, 5 insertions(+), 242 deletions(-) delete mode 100644 src/etesync-helpers.ts delete mode 100644 src/journal-processors.test.ts delete mode 100644 src/journal-processors.ts diff --git a/src/Collections/CollectionEdit.tsx b/src/Collections/CollectionEdit.tsx index 79b44d2..1e231cc 100644 --- a/src/Collections/CollectionEdit.tsx +++ b/src/Collections/CollectionEdit.tsx @@ -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"; diff --git a/src/Pim/helpers.tsx b/src/Pim/helpers.tsx index fa6b984..c6e9556 100644 --- a/src/Pim/helpers.tsx +++ b/src/Pim/helpers.tsx @@ -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; diff --git a/src/etesync-helpers.ts b/src/etesync-helpers.ts deleted file mode 100644 index 0dcc6ee..0000000 --- a/src/etesync-helpers.ts +++ /dev/null @@ -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); -} diff --git a/src/journal-processors.test.ts b/src/journal-processors.test.ts deleted file mode 100644 index 20ef5e2..0000000 --- a/src/journal-processors.test.ts +++ /dev/null @@ -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))); - } -}); - diff --git a/src/journal-processors.ts b/src/journal-processors.ts deleted file mode 100644 index 9e7f314..0000000 --- a/src/journal-processors.ts +++ /dev/null @@ -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, 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( - ItemType: any, - collection: EteSync.CollectionInfo, entries: List, 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, base: {[key: string]: EventType} = {}) { - return syncEntriesToCalendarItemMap(EventType, collection, entries, base); -} - -export function syncEntriesToTaskItemMap( - collection: EteSync.CollectionInfo, entries: List, base: {[key: string]: TaskType} = {}) { - return syncEntriesToCalendarItemMap(TaskType, collection, entries, base); -} diff --git a/src/widgets/ColorPicker.tsx b/src/widgets/ColorPicker.tsx index 6cb8b4e..c56bf84 100644 --- a/src/widgets/ColorPicker.tsx +++ b/src/widgets/ColorPicker.tsx @@ -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) { ); -} \ No newline at end of file +}