|
| 1 | +import { Field, Form, notifier } from "@/components/Core"; |
| 2 | +import { TextareaField } from "@/components/Core/Form/TextareaField"; |
| 3 | +import { useProxySubscription } from "@/hooks"; |
| 4 | +import { TRANSLATION_KEY } from "@/i18n/locales/key"; |
| 5 | +import { proxiesSlice, type RootState, subscriptionsSlice } from "@/reducers"; |
| 6 | +import { formatError } from "@/utils/error"; |
| 7 | +import { decodeFromUrl } from "@/utils/url"; |
| 8 | +import { Button, Spinner } from "@fluentui/react-components"; |
| 9 | +import axios from "axios"; |
| 10 | +import { |
| 11 | + addProxiesFromSubscriptionUrl, |
| 12 | + Subscription, |
| 13 | + updateSubscription, |
| 14 | +} from "lux-js-sdk"; |
| 15 | +import React, { useState } from "react"; |
| 16 | +import { useTranslation } from "react-i18next"; |
| 17 | +import { useDispatch, useSelector } from "react-redux"; |
| 18 | +import * as Yup from "yup"; |
| 19 | +import styles from "./index.module.css"; |
| 20 | + |
| 21 | +interface SubscriptionModalProps { |
| 22 | + close: () => void; |
| 23 | + initialValue?: Subscription; |
| 24 | + isSelected?: boolean; |
| 25 | +} |
| 26 | + |
| 27 | +const INIT_DATA: Subscription = { |
| 28 | + id: "", |
| 29 | + name: "", |
| 30 | + url: "", |
| 31 | + remark: "", |
| 32 | +}; |
| 33 | + |
| 34 | +function SubscriptionModal(props: Readonly<SubscriptionModalProps>) { |
| 35 | + const { close, initialValue, isSelected } = props; |
| 36 | + const { t } = useTranslation(); |
| 37 | + |
| 38 | + const SubscriptionSchema = Yup.object().shape({ |
| 39 | + url: Yup.string().required(t(TRANSLATION_KEY.REQUIRED)), |
| 40 | + name: Yup.string(), |
| 41 | + remark: Yup.string(), |
| 42 | + }); |
| 43 | + |
| 44 | + const [loading, setLoading] = useState(false); |
| 45 | + const dispatch = useDispatch(); |
| 46 | + |
| 47 | + const isStarted = useSelector<RootState, boolean>( |
| 48 | + (state) => state.manager.isStared, |
| 49 | + ); |
| 50 | + const { update: updateSubscriptionProxies } = useProxySubscription(); |
| 51 | + |
| 52 | + const onSubmit = async (data: Subscription) => { |
| 53 | + try { |
| 54 | + setLoading(true); |
| 55 | + if (!data.url) { |
| 56 | + return; |
| 57 | + } |
| 58 | + |
| 59 | + try { |
| 60 | + if (data.id) { |
| 61 | + await updateSubscription({ subscription: data }); |
| 62 | + dispatch( |
| 63 | + subscriptionsSlice.actions.updateOne({ subscription: data }), |
| 64 | + ); |
| 65 | + if (initialValue?.url !== data.url) { |
| 66 | + await updateSubscriptionProxies(data.id, data.url); |
| 67 | + } |
| 68 | + } else { |
| 69 | + const decodedProxies = await decodeFromUrl(data.url); |
| 70 | + const res = await addProxiesFromSubscriptionUrl({ |
| 71 | + proxies: decodedProxies, |
| 72 | + subscriptionUrl: data.url, |
| 73 | + subscriptionName: data.name, |
| 74 | + subscriptionRemark: data.remark, |
| 75 | + }); |
| 76 | + dispatch(proxiesSlice.actions.received({ proxies: res.proxies })); |
| 77 | + dispatch( |
| 78 | + subscriptionsSlice.actions.received({ |
| 79 | + subscriptions: res.subscriptions, |
| 80 | + }), |
| 81 | + ); |
| 82 | + } |
| 83 | + close(); |
| 84 | + notifier.success(t(TRANSLATION_KEY.UPDATE_SUCCESS)); |
| 85 | + } catch (e) { |
| 86 | + if (!axios.isAxiosError(e)) { |
| 87 | + notifier.error(formatError(e)); |
| 88 | + } |
| 89 | + } |
| 90 | + } finally { |
| 91 | + setLoading(false); |
| 92 | + } |
| 93 | + }; |
| 94 | + return ( |
| 95 | + <Form |
| 96 | + onSubmit={onSubmit} |
| 97 | + initialValues={initialValue ?? INIT_DATA} |
| 98 | + validationSchema={SubscriptionSchema} |
| 99 | + > |
| 100 | + {({ isValid, submitForm }) => { |
| 101 | + return ( |
| 102 | + <> |
| 103 | + <TextareaField<keyof Subscription> |
| 104 | + name="url" |
| 105 | + label={t(TRANSLATION_KEY.URL)} |
| 106 | + /> |
| 107 | + <Field<keyof Subscription> |
| 108 | + name="name" |
| 109 | + label={`${t(TRANSLATION_KEY.FORM_NAME)}(${t( |
| 110 | + TRANSLATION_KEY.FORM_OPTIONAL, |
| 111 | + )})`} |
| 112 | + /> |
| 113 | + <Field<keyof Subscription> |
| 114 | + name="remark" |
| 115 | + label={`${t(TRANSLATION_KEY.REMARK)}(${t( |
| 116 | + TRANSLATION_KEY.FORM_OPTIONAL, |
| 117 | + )})`} |
| 118 | + /> |
| 119 | + <div className={styles.buttonContainer}> |
| 120 | + <Button onClick={close} className={styles.button}> |
| 121 | + {t(TRANSLATION_KEY.FORM_CANCEL)} |
| 122 | + </Button> |
| 123 | + <Button |
| 124 | + className={styles.button} |
| 125 | + disabled={!isValid || (isSelected && isStarted) || loading} |
| 126 | + onClick={submitForm} |
| 127 | + appearance="primary" |
| 128 | + > |
| 129 | + {loading && ( |
| 130 | + <Spinner size="extra-tiny" className={styles.spinner} /> |
| 131 | + )} |
| 132 | + {t(TRANSLATION_KEY.FORM_SAVE)} |
| 133 | + </Button> |
| 134 | + </div> |
| 135 | + </> |
| 136 | + ); |
| 137 | + }} |
| 138 | + </Form> |
| 139 | + ); |
| 140 | +} |
| 141 | + |
| 142 | +export default SubscriptionModal; |
0 commit comments