mirror of
https://github.com/Myzel394/kleckrelay-website.git
synced 2025-06-20 08:15: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 {ReactElement, useContext} from "react"
|
||||||
import {useAsync} from "react-use"
|
import {useAsync} from "react-use"
|
||||||
import camelcaseKeys from "camelcase-keys"
|
|
||||||
|
|
||||||
import {DecryptedReportContent, Report} from "~/server-types"
|
import {DecryptedReportContent, Report} from "~/server-types"
|
||||||
import AuthContext from "~/AuthContext/AuthContext"
|
import AuthContext from "~/AuthContext/AuthContext"
|
||||||
import parseDecryptedReport from "~/apis/helpers/parse-decrypted-report"
|
import decryptReport from "~/apis/helpers/decrypt-report"
|
||||||
|
|
||||||
interface DecryptReportPropsBase {
|
interface DecryptReportPropsBase {
|
||||||
encryptedContent?: string
|
encryptedContent?: string
|
||||||
@ -38,12 +37,7 @@ export default function DecryptReport({
|
|||||||
const decrypt = async (
|
const decrypt = async (
|
||||||
content: string,
|
content: string,
|
||||||
): Promise<DecryptedReportContent> =>
|
): Promise<DecryptedReportContent> =>
|
||||||
parseDecryptedReport(
|
decryptReport(content, _decryptUsingPrivateKey)
|
||||||
camelcaseKeys(
|
|
||||||
JSON.parse(await _decryptUsingPrivateKey(content)),
|
|
||||||
{deep: true},
|
|
||||||
),
|
|
||||||
)
|
|
||||||
|
|
||||||
if (encryptedContent) {
|
if (encryptedContent) {
|
||||||
return decrypt(encryptedContent)
|
return decrypt(encryptedContent)
|
||||||
|
@ -5,6 +5,7 @@ import {ReactElement, useContext} from "react"
|
|||||||
import {MdEditCalendar} from "react-icons/md"
|
import {MdEditCalendar} from "react-icons/md"
|
||||||
import {RiLinkM, RiStickyNoteFill} from "react-icons/ri"
|
import {RiLinkM, RiStickyNoteFill} from "react-icons/ri"
|
||||||
import {FieldArray, FormikProvider, useFormik} from "formik"
|
import {FieldArray, FormikProvider, useFormik} from "formik"
|
||||||
|
import format from "date-fns/format"
|
||||||
import update from "immutability-helper"
|
import update from "immutability-helper"
|
||||||
|
|
||||||
import {useMutation} from "@tanstack/react-query"
|
import {useMutation} from "@tanstack/react-query"
|
||||||
@ -26,12 +27,12 @@ import {
|
|||||||
} from "@mui/material"
|
} from "@mui/material"
|
||||||
|
|
||||||
import {URL_REGEX} from "~/constants/values"
|
import {URL_REGEX} from "~/constants/values"
|
||||||
import {decryptAliasNotes, parseFastAPIError, whenEnterPressed} from "~/utils"
|
import {parseFastAPIError, whenEnterPressed} from "~/utils"
|
||||||
import {BackupImage, ErrorSnack, SuccessSnack} from "~/components"
|
import {BackupImage, ErrorSnack, SuccessSnack} from "~/components"
|
||||||
import {Alias, AliasNote, DecryptedAlias} from "~/server-types"
|
import {Alias, AliasNote, DecryptedAlias} from "~/server-types"
|
||||||
import {UpdateAliasData, updateAlias} from "~/apis"
|
import {UpdateAliasData, updateAlias} from "~/apis"
|
||||||
import AuthContext from "~/AuthContext/AuthContext"
|
import AuthContext from "~/AuthContext/AuthContext"
|
||||||
import format from "date-fns/format"
|
import decryptAliasNotes from "~/apis/helpers/decrypt-alias-notes"
|
||||||
|
|
||||||
export interface AliasNotesFormProps {
|
export interface AliasNotesFormProps {
|
||||||
id: string
|
id: string
|
||||||
@ -82,7 +83,12 @@ export default function AliasNotesForm({
|
|||||||
UpdateAliasData
|
UpdateAliasData
|
||||||
>(values => updateAlias(id, values), {
|
>(values => updateAlias(id, values), {
|
||||||
onSuccess: newAlias => {
|
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>({
|
const formik = useFormik<Form>({
|
||||||
|
@ -6,9 +6,10 @@ import {useMutation} from "@tanstack/react-query"
|
|||||||
|
|
||||||
import {Alias, DecryptedAlias} from "~/server-types"
|
import {Alias, DecryptedAlias} from "~/server-types"
|
||||||
import {UpdateAliasData, updateAlias} from "~/apis"
|
import {UpdateAliasData, updateAlias} from "~/apis"
|
||||||
import {decryptAliasNotes, parseFastAPIError} from "~/utils"
|
import {parseFastAPIError} from "~/utils"
|
||||||
import {ErrorSnack, SuccessSnack} from "~/components"
|
import {ErrorSnack, SuccessSnack} from "~/components"
|
||||||
import AuthContext, {EncryptionStatus} from "~/AuthContext/AuthContext"
|
import AuthContext, {EncryptionStatus} from "~/AuthContext/AuthContext"
|
||||||
|
import decryptAliasNotes from "~/apis/helpers/decrypt-alias-notes"
|
||||||
|
|
||||||
export interface ChangeAliasActivationStatusSwitchProps {
|
export interface ChangeAliasActivationStatusSwitchProps {
|
||||||
id: string
|
id: string
|
||||||
@ -37,12 +38,13 @@ export default function ChangeAliasActivationStatusSwitch({
|
|||||||
>(values => updateAlias(id, values), {
|
>(values => updateAlias(id, values), {
|
||||||
onSuccess: newAlias => {
|
onSuccess: newAlias => {
|
||||||
if (encryptionStatus === EncryptionStatus.Available) {
|
if (encryptionStatus === EncryptionStatus.Available) {
|
||||||
onChanged(
|
;(newAlias as any as DecryptedAlias).notes = decryptAliasNotes(
|
||||||
decryptAliasNotes(newAlias, _decryptUsingMasterPassword),
|
newAlias.encryptedNotes,
|
||||||
|
_decryptUsingMasterPassword,
|
||||||
)
|
)
|
||||||
} else {
|
|
||||||
onChanged(newAlias)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
onChanged(newAlias)
|
||||||
},
|
},
|
||||||
onError: error =>
|
onError: error =>
|
||||||
setErrorMessage(parseFastAPIError(error).detail as string),
|
setErrorMessage(parseFastAPIError(error).detail as string),
|
||||||
|
@ -7,9 +7,9 @@ import {useQuery} from "@tanstack/react-query"
|
|||||||
import {getAlias} from "~/apis"
|
import {getAlias} from "~/apis"
|
||||||
import {Alias, DecryptedAlias} from "~/server-types"
|
import {Alias, DecryptedAlias} from "~/server-types"
|
||||||
import {QueryResult, SimplePage} from "~/components"
|
import {QueryResult, SimplePage} from "~/components"
|
||||||
import {decryptAliasNotes} from "~/utils"
|
|
||||||
import AliasDetails from "~/route-widgets/AliasDetailRoute/AliasDetails"
|
import AliasDetails from "~/route-widgets/AliasDetailRoute/AliasDetails"
|
||||||
import AuthContext, {EncryptionStatus} from "~/AuthContext/AuthContext"
|
import AuthContext, {EncryptionStatus} from "~/AuthContext/AuthContext"
|
||||||
|
import decryptAliasNotes from "~/apis/helpers/decrypt-alias-notes"
|
||||||
|
|
||||||
export default function AliasDetailRoute(): ReactElement {
|
export default function AliasDetailRoute(): ReactElement {
|
||||||
const params = useParams()
|
const params = useParams()
|
||||||
@ -20,14 +20,16 @@ export default function AliasDetailRoute(): ReactElement {
|
|||||||
const query = useQuery<Alias | DecryptedAlias, AxiosError>(
|
const query = useQuery<Alias | DecryptedAlias, AxiosError>(
|
||||||
["get_alias", params.addressInBase64],
|
["get_alias", params.addressInBase64],
|
||||||
async () => {
|
async () => {
|
||||||
|
const alias = await getAlias(address)
|
||||||
|
|
||||||
if (encryptionStatus === EncryptionStatus.Available) {
|
if (encryptionStatus === EncryptionStatus.Available) {
|
||||||
return decryptAliasNotes(
|
;(alias as any as DecryptedAlias).notes = decryptAliasNotes(
|
||||||
await getAlias(address),
|
alias.encryptedNotes,
|
||||||
_decryptUsingMasterPassword,
|
_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 {default as buildEncryptionPassword} from "./build-encryption-password"
|
||||||
export * from "./decrypt-string"
|
export * from "./decrypt-string"
|
||||||
export {default as decryptString} 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 * from "./when-enter-pressed"
|
||||||
export {default as whenEnterPressed} from "./when-enter-pressed"
|
export {default as whenEnterPressed} from "./when-enter-pressed"
|
||||||
|
Loading…
x
Reference in New Issue
Block a user