All files / app/features/profile/hooks useMessageUser.ts

94.73% Statements 18/19
100% Branches 4/4
80% Functions 4/5
94.73% Lines 18/19

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 434x 4x 4x 4x 4x 4x               31x         31x 31x 31x   31x 5x   5x           5x   2x 3x 1x   2x          
import { useMutation } from "@tanstack/react-query";
import { useProfileSheet } from "features/profile/ProfileSheetContext";
import { useRouter } from "next/router";
import { routeToGroupChat } from "routes";
import { service } from "service";
import { useIsNativeEmbed } from "utils/nativeLink";
 
interface UseMessageUserParams {
  userId: number;
  setMutationError: (value: string) => void;
  setIsMessaging: (value: boolean) => void;
}
 
export default function useMessageUser({
  userId,
  setMutationError,
  setIsMessaging,
}: UseMessageUserParams) {
  const router = useRouter();
  const isNativeEmbed = useIsNativeEmbed();
  const { openGroupChat } = useProfileSheet();
 
  return useMutation<number | false, Error>({
    mutationFn: () => service.conversations.getDirectMessage(userId),
    onMutate() {
      setMutationError("");
    },
    onError(e) {
      setMutationError(e.message);
    },
    onSuccess(groupChatId) {
      if (!groupChatId) {
        // no existing thread — open inline compose form
        setIsMessaging(true);
      } else if (isNativeEmbed) {
        openGroupChat(groupChatId);
      } else {
        router.push(routeToGroupChat(groupChatId));
      }
    },
  });
}