import {
Querytypes,
useConversationDetails,
useGetUserDetails,
useSendContactRequest,
useUpdateRelationshipState,
} from "../../../controller";
import { $selectedConversation } from "../../../controller/recoil";
import { Dimensions } from "../../../electron/constants";
import { FaWindowClose } from "react-icons/fa";
import { GlobalHotKeys } from "react-hotkeys";
import { RelationshipState } from "@nirvana/core/models/relationship.model";
import SocketChannels from "@nirvana/core/sockets/channels";
import UpdateRelationshipStateRequest from "@nirvana/core/requests/updateRelationshipState.request";
import UserStatusText from "../../../components/User/userStatusText";
import moment from "moment";
import { queryClient } from "../../../nirvanaApp";
import { socket } from "../../../nirvanaApp";
import { useEffect } from "react";
import { useRecoilState } from "recoil";
export default function SelectedConversation() {
const [selectedConvo, setSelectedConvo] = useRecoilState(
$selectedConversation
);
const { data: userDetailsData } = useGetUserDetails();
const { data: convoDetailsResponse, isFetching } =
useConversationDetails(selectedConvo);
const { mutate: sendContactRequest } = useSendContactRequest();
const { mutate: updateRelationshipState } = useUpdateRelationshipState();
useEffect(() => {
// if selected, then change the bounds of this window as well
if (selectedConvo) {
window.electronAPI.window.resizeWindow(Dimensions.selectedConvo);
} else {
// change bounds back
window.electronAPI.window.resizeWindow(Dimensions.default);
}
}, [selectedConvo]);
// if no convo selected, don't show this
if (!selectedConvo) {
return <>>;
}
if (isFetching) {
return Loading conversation details;
}
// todo: add cancel request option
const sendRequest = () => {
const otherUserGoogleId = convoDetailsResponse.contactUser.googleId;
// todo mutation to create a user
sendContactRequest(otherUserGoogleId, {
onSettled: (data, error) => {
return queryClient.invalidateQueries(
Querytypes.GET_CONVERSATION_DETAILS + "/" + otherUserGoogleId
);
},
});
};
const acceptRequest = () => {
const otherUserGoogleId = convoDetailsResponse.contactUser.googleId;
updateRelationshipState(
new UpdateRelationshipStateRequest(
convoDetailsResponse.ourRelationship._id,
RelationshipState.ACTIVE
),
{
onSettled: (data, error) => {
return queryClient.invalidateQueries(
Querytypes.GET_CONVERSATION_DETAILS + "/" + otherUserGoogleId
);
},
}
);
};
if (isFetching) return Please wait;
const renderMainContent = () => {
// if there's no relationship, then don't show messages, show "send request" button
if (!convoDetailsResponse?.ourRelationship) {
return ;
}
// if the relationship is pending and I am the receiver, then I should see an accept or deny button
if (
convoDetailsResponse.ourRelationship.state ===
RelationshipState.PENDING &&
userDetailsData.googleId ===
convoDetailsResponse.ourRelationship.receiverUserId
) {
return (
please accept this request by clicking here:{" "}
);
}
// when i sent a request to this person, just tell me that I have to wait
if (
convoDetailsResponse.ourRelationship.state ===
RelationshipState.PENDING &&
userDetailsData.googleId ===
convoDetailsResponse.ourRelationship.senderUserId
) {
return Waiting on request to be accepted;
}
if (
convoDetailsResponse.ourRelationship.state === RelationshipState.ACTIVE
) {
return this is all of the content;
}
if (
convoDetailsResponse.ourRelationship.state === RelationshipState.DENIED
) {
return this request was denied;
}
};
// emit to room/conversation that someone started speaking
const socketStartedSpeaking = () => {
// send the room name and the update type
socket.emit(SocketChannels.SEND_STARTED_SPEAKING_TO_SERVER, selectedConvo);
};
const socketStopSpeaking = () => {
// send the room name and the update type
socket.emit(SocketChannels.SEND_STOPPED_SPEAKING_TO_SERVER, selectedConvo);
};
// hot keys for closing this window
const handleClose = () => {
setSelectedConvo(null);
};
const keyMap = { CLOSE_SELECTED_CONVO: "esc" };
const handlers = { CLOSE_SELECTED_CONVO: handleClose };
return (
<>