import * as yup from "yup" import {ReactElement, useContext} from "react" import {BsImage, BsShieldShaded} from "react-icons/bs" import {useFormik} from "formik" import {FaFile} from "react-icons/fa" import {MdCheckCircle, MdTextSnippet} from "react-icons/md" import {AxiosError} from "axios" import {useTranslation} from "react-i18next" import {LoadingButton} from "@mui/lab" import {Box, Collapse, Grid, Typography} from "@mui/material" import {QueryKey, useMutation} from "@tanstack/react-query" import {Alias, DecryptedAlias, ImageProxyFormatType, ProxyUserAgentType} from "~/server-types" import {UpdateAliasData, updateAlias} from "~/apis" import {parseFastAPIError} from "~/utils" import { IMAGE_PROXY_FORMAT_TYPE_NAME_MAP, PROXY_USER_AGENT_TYPE_NAME_MAP, } from "~/constants/enum-mappings" import {useErrorSuccessSnacks} from "~/hooks" import {queryClient} from "~/constants/react-query" import {AuthContext, FormikAutoLockNavigation} from "~/components" import SelectField from "~/route-widgets/AliasDetailRoute/SelectField" import decryptAliasNotes from "~/apis/helpers/decrypt-alias-notes" export interface AliasPreferencesFormProps { alias: Alias | DecryptedAlias queryKey: QueryKey } interface Form { removeTrackers: boolean | null createMailReport: boolean | null proxyImages: boolean | null imageProxyFormat: ImageProxyFormatType | null proxyUserAgent: ProxyUserAgentType | null detail?: string } export default function AliasPreferencesForm({ alias, queryKey, }: AliasPreferencesFormProps): ReactElement { const {t} = useTranslation(["aliases", "common"]) const {showSuccess, showError} = useErrorSuccessSnacks() const {_decryptUsingMasterPassword} = useContext(AuthContext) const schema = yup.object().shape({ removeTrackers: yup .mixed() .oneOf([true, false, null]) .label(t("settings.fields.removeTrackers.label")), createMailReport: yup .mixed() .oneOf([true, false, null]) .label(t("settings.fields.createMailReport.label")), proxyImages: yup .mixed() .oneOf([true, false, null]) .label(t("settings.fields.proxyImages.label")), imageProxyFormat: yup .mixed() .oneOf([null, ...Object.values(ImageProxyFormatType)]) .label(t("settings.fields.imageProxyFormat.label")), proxyUserAgent: yup .mixed() .oneOf([null, ...Object.values(ProxyUserAgentType)]) .label(t("settings.fields.proxyUserAgent.label")), expandUrlShorteners: yup .mixed() .oneOf([true, false, null]) .label(t("settings.fields.expandUrlShorteners.label")), }) const {mutateAsync} = useMutation( data => updateAlias(alias.id, data), { onSuccess: async newAlias => { showSuccess(t("messages.alias.updated", {ns: "common"})) ;(newAlias as any as DecryptedAlias).notes = decryptAliasNotes( newAlias.encryptedNotes, _decryptUsingMasterPassword, ) await queryClient.cancelQueries(queryKey) queryClient.setQueryData( queryKey, newAlias as any as DecryptedAlias, ) }, onError: showError, }, ) const formik = useFormik
({ enableReinitialize: true, initialValues: { removeTrackers: alias.prefRemoveTrackers, createMailReport: alias.prefCreateMailReport, proxyImages: alias.prefProxyImages, imageProxyFormat: alias.prefImageProxyFormat, proxyUserAgent: alias.prefProxyUserAgent, }, validationSchema: schema, onSubmit: async (values, {setErrors}) => { try { await mutateAsync({ prefCreateMailReport: values.createMailReport, prefRemoveTrackers: values.removeTrackers, prefProxyImages: values.proxyImages, prefImagProxyFormat: values.imageProxyFormat, prefProxyUserAgent: values.proxyUserAgent, }) } catch (error) { setErrors(parseFastAPIError(error as AxiosError)) } }, }) return ( <> } name="removeTrackers" /> } name="createMailReport" /> } name="proxyImages" /> } name="imageProxyFormat" valueTextMap={ IMAGE_PROXY_FORMAT_TYPE_NAME_MAP } /> } > {t("settings.continueActionLabel")} {t("settings.description")}
) }