refactor: more TS code
This commit is contained in:
@@ -11,13 +11,16 @@ import {
|
||||
useLazyGetSMTPConfigQuery,
|
||||
useLazyGetLoginConfigQuery
|
||||
} from "../../app/services/server";
|
||||
|
||||
import { AgoraConfig, FirebaseConfig, LoginConfig, SMTPConfig } from "../../types/server";
|
||||
// config: smtp agora login firebase
|
||||
let originalValue: null | object = null;
|
||||
|
||||
export default function useConfig(config = "smtp") {
|
||||
type ConfigType = "smtp" | "agora" | "login" | "firebase";
|
||||
type ConfigMap = Record<ConfigType, AgoraConfig | FirebaseConfig | LoginConfig | SMTPConfig>;
|
||||
type valuesOf<T> = T[keyof T];
|
||||
let originalValue: valuesOf<ConfigMap> | undefined = undefined;
|
||||
// type valueOf<T,config as ConfigType> = T[config];
|
||||
export default function useConfig(config: keyof ConfigMap = "smtp") {
|
||||
const [changed, setChanged] = useState(false);
|
||||
const [values, setValues] = useState<object>({});
|
||||
const [values, setValues] = useState<valuesOf<ConfigMap> | undefined>(undefined);
|
||||
const [updateLoginConfig, { isSuccess: LoginUpdated }] = useUpdateLoginConfigMutation();
|
||||
const [updateSMTPConfig, { isSuccess: SMTPUpdated }] = useUpdateSMTPConfigMutation();
|
||||
const [getAgoraConfig, { data: agoraConfig }] = useLazyGetAgoraConfigQuery();
|
||||
@@ -27,63 +30,43 @@ export default function useConfig(config = "smtp") {
|
||||
const [getFirebaseConfig, { data: firebaseConfig }] = useLazyGetFirebaseConfigQuery();
|
||||
const [updateFirebaseConfig, { isSuccess: FirebaseUpdated }] = useUpdateFirebaseConfigMutation();
|
||||
|
||||
// const datas = {
|
||||
// login: {},
|
||||
// smtp: {},
|
||||
// agora: {},
|
||||
// firebase: {}
|
||||
// };
|
||||
const updateFns = {
|
||||
login: updateLoginConfig,
|
||||
smtp: updateSMTPConfig,
|
||||
agora: updateAgoraConfig,
|
||||
firebase: updateFirebaseConfig
|
||||
};
|
||||
const refetchs = {
|
||||
const requests = {
|
||||
smtp: getSMTPConfig,
|
||||
agora: getAgoraConfig,
|
||||
firebase: getFirebaseConfig,
|
||||
login: getLoginConfig
|
||||
};
|
||||
const updateds = {
|
||||
const updates = {
|
||||
login: LoginUpdated,
|
||||
smtp: SMTPUpdated,
|
||||
agora: AgoraUpdated,
|
||||
firebase: FirebaseUpdated
|
||||
};
|
||||
// const data = datas[config];
|
||||
const updateConfig = updateFns[config];
|
||||
const refetch = refetchs[config];
|
||||
const updated = updateds[config];
|
||||
const refetch = requests[config];
|
||||
const updated = updates[config];
|
||||
const reset = () => {
|
||||
setValues(originalValue);
|
||||
setValues(undefined);
|
||||
};
|
||||
|
||||
const toggleEnable = () => {
|
||||
setValues((prev) => {
|
||||
return { ...prev, enabled: !prev.enabled };
|
||||
if (prev && "enabled" in prev) {
|
||||
return { ...prev, enabled: !prev.enabled };
|
||||
}
|
||||
return prev;
|
||||
});
|
||||
};
|
||||
|
||||
useEffect(() => {
|
||||
switch (config) {
|
||||
case "firebase":
|
||||
getFirebaseConfig();
|
||||
break;
|
||||
case "agora":
|
||||
getAgoraConfig();
|
||||
break;
|
||||
case "smtp":
|
||||
getSMTPConfig();
|
||||
break;
|
||||
case "login":
|
||||
getLoginConfig();
|
||||
break;
|
||||
|
||||
default:
|
||||
break;
|
||||
}
|
||||
}, [config]);
|
||||
refetch();
|
||||
}, []);
|
||||
|
||||
useEffect(() => {
|
||||
if (updated) {
|
||||
@@ -100,7 +83,7 @@ export default function useConfig(config = "smtp") {
|
||||
}, [smtpConfig, firebaseConfig, loginConfig, agoraConfig]);
|
||||
useEffect(() => {
|
||||
// 空对象
|
||||
if (Object.keys(values).length == 0) return;
|
||||
if (!values || Object.keys(values).length == 0) return;
|
||||
if (!isEqual(originalValue, values)) {
|
||||
setChanged(true);
|
||||
} else {
|
||||
|
||||
@@ -15,7 +15,15 @@ export default function useForwardMessage() {
|
||||
] = useSendChannelMsgMutation();
|
||||
const [sendUserMsg, { isLoading: userSending, isSuccess: userSuccess, isError: userError }] =
|
||||
useSendMsgMutation();
|
||||
const forwardMessage = async ({ mids = [], users = [], channels = [] }) => {
|
||||
const forwardMessage = async ({
|
||||
mids,
|
||||
users,
|
||||
channels
|
||||
}: {
|
||||
mids: number[];
|
||||
users: number[];
|
||||
channels: number[];
|
||||
}) => {
|
||||
setForwarding(true);
|
||||
const { data: archive_id } = await createArchive(mids);
|
||||
if (users.length) {
|
||||
|
||||
@@ -3,14 +3,11 @@ import {
|
||||
useGetGithubAuthConfigQuery,
|
||||
useUpdateGithubAuthConfigMutation
|
||||
} from "../../app/services/server";
|
||||
|
||||
interface GithubAuthConfig {
|
||||
client_id: string;
|
||||
}
|
||||
import { GithubAuthConfig } from "../../types/server";
|
||||
|
||||
export default function useGithubAuthConfig() {
|
||||
const [changed, setChanged] = useState(false);
|
||||
const [config, setConfig] = useState({});
|
||||
const [config, setConfig] = useState<GithubAuthConfig | undefined>(undefined);
|
||||
const { data } = useGetGithubAuthConfigQuery(undefined, {
|
||||
refetchOnMountOrArgChange: true
|
||||
});
|
||||
@@ -24,13 +21,18 @@ export default function useGithubAuthConfig() {
|
||||
useEffect(() => {
|
||||
setChanged(isSuccess ? false : JSON.stringify(data) !== JSON.stringify(config));
|
||||
}, [data, config, isSuccess]);
|
||||
const updateGithubAuthConfig = (obj) => {
|
||||
const updateGithubAuthConfig = (obj: Partial<GithubAuthConfig>) => {
|
||||
setConfig((prev) => {
|
||||
return { ...prev, ...obj };
|
||||
if (prev) {
|
||||
return { ...prev, ...obj };
|
||||
}
|
||||
return obj;
|
||||
});
|
||||
};
|
||||
const updateGithubAuthConfigToServer = async () => {
|
||||
await updateAuthConfig(config);
|
||||
if (config) {
|
||||
await updateAuthConfig(config);
|
||||
}
|
||||
};
|
||||
return {
|
||||
config,
|
||||
|
||||
@@ -24,7 +24,7 @@ interface SendMessageDTO {
|
||||
to: number;
|
||||
}
|
||||
|
||||
const useSendMessage = (props: Props) => {
|
||||
const useSendMessage = (props?: Props) => {
|
||||
const { context = "user", from, to = null } = props || {};
|
||||
const dispatch = useAppDispatch();
|
||||
const stageFiles = useAppSelector((store) => store.ui.uploadFiles[`${context}_${to}`] || []);
|
||||
|
||||
@@ -22,8 +22,8 @@ const useUserOperation = ({ uid, cid }: IProps) => {
|
||||
const { copy } = useCopy();
|
||||
const { user, channel, loginUser } = useAppSelector((store) => {
|
||||
return {
|
||||
user: store.users.byId[uid],
|
||||
channel: store.channels.byId[cid],
|
||||
user: typeof uid !== "undefined" ? store.users.byId[uid] : uid,
|
||||
channel: typeof cid !== "undefined" ? store.channels.byId[cid] : cid,
|
||||
loginUser: store.authData.user
|
||||
};
|
||||
});
|
||||
@@ -62,7 +62,7 @@ const useUserOperation = ({ uid, cid }: IProps) => {
|
||||
const copyEmail = (email: string) => {
|
||||
const isString = typeof email == "string";
|
||||
const finalEmail = isString ? email || user?.email : user?.email;
|
||||
copy(finalEmail);
|
||||
copy(finalEmail || "");
|
||||
hideAll();
|
||||
};
|
||||
|
||||
|
||||
Reference in New Issue
Block a user