Files
ColdBreeze-chat-web/src/common/hook/useLeaveChannel.ts
T
2022-07-04 23:13:09 +08:00

32 lines
1016 B
TypeScript

import { useUpdateChannelMutation, useLazyLeaveChannelQuery } from "../../app/services/channel";
import { useAppSelector } from "../../app/store";
export default function useLeaveChannel(cid: number) {
const { channel, loginUid } = useAppSelector((store) => {
return { channel: store.channels.byId[cid], loginUid: store.authData.user?.uid };
});
const [update, { isLoading: transferring, isSuccess: transferSuccess }] =
useUpdateChannelMutation();
const [leave, { isLoading: leaving, isSuccess: leaveSuccess }] = useLazyLeaveChannelQuery();
const transferOwner = (uid: number) => {
if (!uid) return;
update({ id: cid, owner: uid });
};
const leaveChannel = () => {
if (!cid) return;
leave(cid);
};
const isOwner = loginUid == channel?.owner;
const otherMembers = channel?.members.filter((m) => m != loginUid) || [];
return {
otherMembers,
transferOwner,
leaveChannel,
leaving,
leaveSuccess,
isOwner,
transferring,
transferSuccess
};
}