From fff3399069fa19809acd2604b349075dcebeffd8 Mon Sep 17 00:00:00 2001 From: Myzel394 <50424412+Myzel394@users.noreply.github.com> Date: Sun, 30 Oct 2022 19:43:03 +0100 Subject: [PATCH] improvements --- src/apis/helpers/decrypt-alias-notes.ts | 22 ++++++++++++ src/apis/helpers/decrypt-report.ts | 36 +++++++++++++++++++ src/apis/helpers/parse-decrypted-report.ts | 25 ------------- src/components/DecryptReport.tsx | 10 ++---- .../AliasDetailRoute/AliasNotesForm.tsx | 12 +++++-- .../ChangeAliasActivationStatusSwitch.tsx | 12 ++++--- src/routes/AliasDetailRoute.tsx | 12 ++++--- src/utils/decrypt-alias-notes.ts | 32 ----------------- src/utils/index.ts | 2 -- 9 files changed, 83 insertions(+), 80 deletions(-) create mode 100644 src/apis/helpers/decrypt-alias-notes.ts create mode 100644 src/apis/helpers/decrypt-report.ts delete mode 100644 src/apis/helpers/parse-decrypted-report.ts delete mode 100644 src/utils/decrypt-alias-notes.ts diff --git a/src/apis/helpers/decrypt-alias-notes.ts b/src/apis/helpers/decrypt-alias-notes.ts new file mode 100644 index 0000000..2aed5b2 --- /dev/null +++ b/src/apis/helpers/decrypt-alias-notes.ts @@ -0,0 +1,22 @@ +import update from "immutability-helper" + +import {AliasNote} from "~/server-types" +import {AuthContextType} from "~/AuthContext/AuthContext" +import {DEFAULT_ALIAS_NOTE} from "~/constants/values" + +export default function decryptAliasNotes( + encryptedNotes: string, + decryptContent: AuthContextType["_decryptUsingMasterPassword"], +): AliasNote { + if (!encryptedNotes) { + return update(DEFAULT_ALIAS_NOTE, {}) + } + + return update(JSON.parse(decryptContent(encryptedNotes)), { + data: { + createdAt: { + $apply: createdAt => (createdAt ? new Date(createdAt) : null), + }, + }, + }) +} diff --git a/src/apis/helpers/decrypt-report.ts b/src/apis/helpers/decrypt-report.ts new file mode 100644 index 0000000..22e81e7 --- /dev/null +++ b/src/apis/helpers/decrypt-report.ts @@ -0,0 +1,36 @@ +import camelcaseKeys from "camelcase-keys" +import update from "immutability-helper" + +import {DecryptedReportContent} from "~/server-types" +import {AuthContextType} from "~/AuthContext/AuthContext" + +export default async function decryptReport( + encryptedContent: string, + decryptContent: AuthContextType["_decryptUsingPrivateKey"], +): Promise { + return update( + camelcaseKeys(JSON.parse(await decryptContent(encryptedContent)), { + deep: true, + }), + { + messageDetails: { + meta: { + createdAt: { + $apply: createdAt => new Date(createdAt), + }, + }, + content: { + proxiedImages: { + $apply: ( + proxiedImages: DecryptedReportContent["messageDetails"]["content"]["proxiedImages"], + ) => + proxiedImages.map(image => ({ + ...image, + createdAt: new Date(image.createdAt), + })), + }, + }, + }, + }, + ) +} diff --git a/src/apis/helpers/parse-decrypted-report.ts b/src/apis/helpers/parse-decrypted-report.ts deleted file mode 100644 index 3c1abb5..0000000 --- a/src/apis/helpers/parse-decrypted-report.ts +++ /dev/null @@ -1,25 +0,0 @@ -import {DecryptedReportContent} from "~/server-types" - -export default function parseDecryptedReport( - report: any, -): DecryptedReportContent { - return { - ...report, - messageDetails: { - ...report.messageDetails, - meta: { - ...report.messageDetails.meta, - createdAt: new Date(report.messageDetails.meta.createdAt), - }, - content: { - ...report.messageDetails.content, - proxiedImages: report.messageDetails.content.proxiedImages.map( - image => ({ - ...image, - createdAt: new Date(image.createdAt), - }), - ), - }, - }, - } -} diff --git a/src/components/DecryptReport.tsx b/src/components/DecryptReport.tsx index 3a91938..e064757 100644 --- a/src/components/DecryptReport.tsx +++ b/src/components/DecryptReport.tsx @@ -1,10 +1,9 @@ import {ReactElement, useContext} from "react" import {useAsync} from "react-use" -import camelcaseKeys from "camelcase-keys" import {DecryptedReportContent, Report} from "~/server-types" import AuthContext from "~/AuthContext/AuthContext" -import parseDecryptedReport from "~/apis/helpers/parse-decrypted-report" +import decryptReport from "~/apis/helpers/decrypt-report" interface DecryptReportPropsBase { encryptedContent?: string @@ -38,12 +37,7 @@ export default function DecryptReport({ const decrypt = async ( content: string, ): Promise => - parseDecryptedReport( - camelcaseKeys( - JSON.parse(await _decryptUsingPrivateKey(content)), - {deep: true}, - ), - ) + decryptReport(content, _decryptUsingPrivateKey) if (encryptedContent) { return decrypt(encryptedContent) diff --git a/src/route-widgets/AliasDetailRoute/AliasNotesForm.tsx b/src/route-widgets/AliasDetailRoute/AliasNotesForm.tsx index cfc60e5..451f62e 100644 --- a/src/route-widgets/AliasDetailRoute/AliasNotesForm.tsx +++ b/src/route-widgets/AliasDetailRoute/AliasNotesForm.tsx @@ -5,6 +5,7 @@ import {ReactElement, useContext} from "react" import {MdEditCalendar} from "react-icons/md" import {RiLinkM, RiStickyNoteFill} from "react-icons/ri" import {FieldArray, FormikProvider, useFormik} from "formik" +import format from "date-fns/format" import update from "immutability-helper" import {useMutation} from "@tanstack/react-query" @@ -26,12 +27,12 @@ import { } from "@mui/material" import {URL_REGEX} from "~/constants/values" -import {decryptAliasNotes, parseFastAPIError, whenEnterPressed} from "~/utils" +import {parseFastAPIError, whenEnterPressed} from "~/utils" import {BackupImage, ErrorSnack, SuccessSnack} from "~/components" import {Alias, AliasNote, DecryptedAlias} from "~/server-types" import {UpdateAliasData, updateAlias} from "~/apis" import AuthContext from "~/AuthContext/AuthContext" -import format from "date-fns/format" +import decryptAliasNotes from "~/apis/helpers/decrypt-alias-notes" export interface AliasNotesFormProps { id: string @@ -82,7 +83,12 @@ export default function AliasNotesForm({ UpdateAliasData >(values => updateAlias(id, values), { onSuccess: newAlias => { - onChanged(decryptAliasNotes(newAlias, _decryptUsingMasterPassword)) + ;(newAlias as any as DecryptedAlias).notes = decryptAliasNotes( + newAlias.encryptedNotes, + _decryptUsingMasterPassword, + ) + + onChanged(newAlias as any as DecryptedAlias) }, }) const formik = useFormik
({ diff --git a/src/route-widgets/AliasDetailRoute/ChangeAliasActivationStatusSwitch.tsx b/src/route-widgets/AliasDetailRoute/ChangeAliasActivationStatusSwitch.tsx index d60b8ed..7f02033 100644 --- a/src/route-widgets/AliasDetailRoute/ChangeAliasActivationStatusSwitch.tsx +++ b/src/route-widgets/AliasDetailRoute/ChangeAliasActivationStatusSwitch.tsx @@ -6,9 +6,10 @@ import {useMutation} from "@tanstack/react-query" import {Alias, DecryptedAlias} from "~/server-types" import {UpdateAliasData, updateAlias} from "~/apis" -import {decryptAliasNotes, parseFastAPIError} from "~/utils" +import {parseFastAPIError} from "~/utils" import {ErrorSnack, SuccessSnack} from "~/components" import AuthContext, {EncryptionStatus} from "~/AuthContext/AuthContext" +import decryptAliasNotes from "~/apis/helpers/decrypt-alias-notes" export interface ChangeAliasActivationStatusSwitchProps { id: string @@ -37,12 +38,13 @@ export default function ChangeAliasActivationStatusSwitch({ >(values => updateAlias(id, values), { onSuccess: newAlias => { if (encryptionStatus === EncryptionStatus.Available) { - onChanged( - decryptAliasNotes(newAlias, _decryptUsingMasterPassword), + ;(newAlias as any as DecryptedAlias).notes = decryptAliasNotes( + newAlias.encryptedNotes, + _decryptUsingMasterPassword, ) - } else { - onChanged(newAlias) } + + onChanged(newAlias) }, onError: error => setErrorMessage(parseFastAPIError(error).detail as string), diff --git a/src/routes/AliasDetailRoute.tsx b/src/routes/AliasDetailRoute.tsx index 4a28a57..3419fcd 100644 --- a/src/routes/AliasDetailRoute.tsx +++ b/src/routes/AliasDetailRoute.tsx @@ -7,9 +7,9 @@ import {useQuery} from "@tanstack/react-query" import {getAlias} from "~/apis" import {Alias, DecryptedAlias} from "~/server-types" import {QueryResult, SimplePage} from "~/components" -import {decryptAliasNotes} from "~/utils" import AliasDetails from "~/route-widgets/AliasDetailRoute/AliasDetails" import AuthContext, {EncryptionStatus} from "~/AuthContext/AuthContext" +import decryptAliasNotes from "~/apis/helpers/decrypt-alias-notes" export default function AliasDetailRoute(): ReactElement { const params = useParams() @@ -20,14 +20,16 @@ export default function AliasDetailRoute(): ReactElement { const query = useQuery( ["get_alias", params.addressInBase64], async () => { + const alias = await getAlias(address) + if (encryptionStatus === EncryptionStatus.Available) { - return decryptAliasNotes( - await getAlias(address), + ;(alias as any as DecryptedAlias).notes = decryptAliasNotes( + alias.encryptedNotes, _decryptUsingMasterPassword, ) - } else { - return getAlias(address) } + + return alias }, ) diff --git a/src/utils/decrypt-alias-notes.ts b/src/utils/decrypt-alias-notes.ts deleted file mode 100644 index a336150..0000000 --- a/src/utils/decrypt-alias-notes.ts +++ /dev/null @@ -1,32 +0,0 @@ -import update from "immutability-helper" - -import {Alias, AliasNote, DecryptedAlias} from "~/server-types" -import {AuthContextType} from "~/AuthContext/AuthContext" -import {DEFAULT_ALIAS_NOTE} from "~/constants/values" - -export default function decryptAliasNotes( - alias: Alias, - decryptContent: AuthContextType["_decryptUsingMasterPassword"], -): DecryptedAlias { - if (!alias.encryptedNotes) { - return { - ...alias, - notes: update(DEFAULT_ALIAS_NOTE, {}), - } - } - - return { - ...alias, - notes: update( - JSON.parse(decryptContent(alias.encryptedNotes)), - { - data: { - createdAt: { - $apply: createdAt => - createdAt ? new Date(createdAt) : null, - }, - }, - }, - ), - } -} diff --git a/src/utils/index.ts b/src/utils/index.ts index ca7bce8..7fb2424 100644 --- a/src/utils/index.ts +++ b/src/utils/index.ts @@ -10,7 +10,5 @@ export * from "./build-encryption-password" export {default as buildEncryptionPassword} from "./build-encryption-password" export * from "./decrypt-string" export {default as decryptString} from "./decrypt-string" -export * from "./decrypt-alias-notes" -export {default as decryptAliasNotes} from "./decrypt-alias-notes" export * from "./when-enter-pressed" export {default as whenEnterPressed} from "./when-enter-pressed"