Files
ColdBreeze-chat-web/src/hooks/useLeaveChannel.ts
T
2023-08-29 11:33:23 +08:00

32 lines
1.0 KiB
TypeScript

import { useLazyLeaveChannelQuery, useUpdateChannelMutation } from "@/app/services/channel";
import { useAppSelector } from "@/app/store";
import { shallowEqual } from "react-redux";
export default function useLeaveChannel(cid: number) {
const loginUid = useAppSelector((store) => store.authData.user?.uid, shallowEqual);
const channel = useAppSelector((store) => store.channels.byId[cid], shallowEqual);
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
};
}