57 lines
1.7 KiB
TypeScript
57 lines
1.7 KiB
TypeScript
import { useEffect } from "react";
|
|
import { GroupStatus, PacketGroupInviteInfo } from "../ProtocolProvider/protocol/packets/packet.group.invite.info";
|
|
import { useSender } from "../ProtocolProvider/useSender";
|
|
import { usePacket } from "../ProtocolProvider/usePacket";
|
|
import { useMemory } from "../MemoryProvider/useMemory";
|
|
|
|
|
|
export function useGroupInviteStatus(groupId: string) : {
|
|
inviteStatus: GroupStatus;
|
|
setInviteStatus: (status: GroupStatus) => void;
|
|
setInviteStatusByGroupId: (groupIdParam: string, status: GroupStatus) => void;
|
|
} {
|
|
const [invitesCache, setInvitesCache] = useMemory("groups_invites_cache", [], true);
|
|
|
|
const send = useSender();
|
|
|
|
useEffect(() => {
|
|
(async () => {
|
|
if(groupId == ''){
|
|
return;
|
|
}
|
|
const packet = new PacketGroupInviteInfo();
|
|
packet.setGroupId(groupId);
|
|
send(packet);
|
|
})();
|
|
}, [groupId]);
|
|
|
|
usePacket(0x13, (packet: PacketGroupInviteInfo) => {
|
|
if(packet.getGroupId() != groupId){
|
|
return;
|
|
}
|
|
setInvitesCache((prev) => ({
|
|
...prev,
|
|
[groupId]: packet.getGroupStatus(),
|
|
}));
|
|
}, [groupId]);
|
|
|
|
const setInviteStatus = (status: GroupStatus) => {
|
|
setInvitesCache((prev) => ({
|
|
...prev,
|
|
[groupId]: status,
|
|
}));
|
|
}
|
|
|
|
const setInviteStatusByGroupId = (groupIdParam: string, status: GroupStatus) => {
|
|
setInvitesCache((prev) => ({
|
|
...prev,
|
|
[groupIdParam]: status,
|
|
}));
|
|
}
|
|
|
|
return {
|
|
inviteStatus: invitesCache[groupId] ?? GroupStatus.NOT_JOINED,
|
|
setInviteStatus,
|
|
setInviteStatusByGroupId,
|
|
};
|
|
} |