mirror of
https://github.com/Myzel394/kleckrelay-website.git
synced 2025-06-20 00:05:26 +02:00
improvements
This commit is contained in:
parent
15b1d68fcb
commit
fff3399069
22
src/apis/helpers/decrypt-alias-notes.ts
Normal file
22
src/apis/helpers/decrypt-alias-notes.ts
Normal file
@ -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<AliasNote>(JSON.parse(decryptContent(encryptedNotes)), {
|
||||
data: {
|
||||
createdAt: {
|
||||
$apply: createdAt => (createdAt ? new Date(createdAt) : null),
|
||||
},
|
||||
},
|
||||
})
|
||||
}
|
36
src/apis/helpers/decrypt-report.ts
Normal file
36
src/apis/helpers/decrypt-report.ts
Normal file
@ -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<DecryptedReportContent> {
|
||||
return update<DecryptedReportContent>(
|
||||
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),
|
||||
})),
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
)
|
||||
}
|
@ -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),
|
||||
}),
|
||||
),
|
||||
},
|
||||
},
|
||||
}
|
||||
}
|
@ -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<DecryptedReportContent> =>
|
||||
parseDecryptedReport(
|
||||
camelcaseKeys(
|
||||
JSON.parse(await _decryptUsingPrivateKey(content)),
|
||||
{deep: true},
|
||||
),
|
||||
)
|
||||
decryptReport(content, _decryptUsingPrivateKey)
|
||||
|
||||
if (encryptedContent) {
|
||||
return decrypt(encryptedContent)
|
||||
|
@ -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<Form>({
|
||||
|
@ -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),
|
||||
|
@ -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<Alias | DecryptedAlias, AxiosError>(
|
||||
["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
|
||||
},
|
||||
)
|
||||
|
||||
|
@ -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<AliasNote>(
|
||||
JSON.parse(decryptContent(alias.encryptedNotes)),
|
||||
{
|
||||
data: {
|
||||
createdAt: {
|
||||
$apply: createdAt =>
|
||||
createdAt ? new Date(createdAt) : null,
|
||||
},
|
||||
},
|
||||
},
|
||||
),
|
||||
}
|
||||
}
|
@ -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"
|
||||
|
Loading…
x
Reference in New Issue
Block a user