refactor: useInviteLink

This commit is contained in:
Tristan Yang
2022-06-17 22:18:54 +08:00
parent 9a9fd75e0a
commit b25907f7fc
4 changed files with 16 additions and 22 deletions
+9 -17
View File
@@ -1,44 +1,36 @@
import { useState, useEffect } from "react";
import useCopy from "./useCopy";
import {
useLazyCreateInviteLinkQuery as useCreateServerInviteLinkQuery,
useGetSMTPStatusQuery
} from "../../app/services/server";
import { useGetSMTPStatusQuery } from "../../app/services/server";
import { useLazyCreateInviteLinkQuery as useCreateChannelInviteLinkQuery } from "../../app/services/channel";
export default function useInviteLink(cid = null) {
export default function useInviteLink(cid = "") {
const [finalLink, setFinalLink] = useState("");
const { data: SMTPEnabled, isSuccess: smtpStatusFetchSuccess } = useGetSMTPStatusQuery();
const [generateChannelInviteLink, { data: channelInviteLink, isLoading: generatingChannelLink }] =
useCreateChannelInviteLinkQuery();
const [generateServerInviteLink, { data: serverInviteLink, isLoading: generatingServerLink }] =
useCreateServerInviteLinkQuery();
const { copied, copy } = useCopy({ enableToast: false });
const copyLink = () => {
copy(finalLink);
};
useEffect(() => {
if (cid) {
generateChannelInviteLink(cid);
} else {
generateServerInviteLink();
}
generateChannelInviteLink(cid);
}, [cid]);
useEffect(() => {
const _link = serverInviteLink || channelInviteLink;
console.log("fetching", serverInviteLink, channelInviteLink);
const _link = channelInviteLink;
console.log("fetching", channelInviteLink);
if (_link && smtpStatusFetchSuccess) {
// const tmpURL = new URL(_link);
// tmpURL.searchParams.set("code", SMTPEnabled);
setFinalLink(_link);
}
}, [serverInviteLink, channelInviteLink, smtpStatusFetchSuccess]);
}, [channelInviteLink, smtpStatusFetchSuccess]);
const genServerLink = () => {
generateServerInviteLink();
generateChannelInviteLink();
};
return {
enableSMTP: SMTPEnabled,
generating: cid ? generatingChannelLink : generatingServerLink,
generating: generatingChannelLink,
generateNewLink: cid ? generateChannelInviteLink.bind(null, cid) : genServerLink,
link: finalLink,
linkCopied: copied,