mirror of
https://github.com/StanGirard/quivr.git
synced 2024-12-24 11:52:45 +03:00
1ec736b357
* feat(brainSettings): add feed process instrcution to knowledge tab * feat: prevent default brain auto fetch * feat: prevent chat submision on submit button click * feat: remove unrelevant toast * feat: remove duplicated GA initialization * feat: add brain name in notifications * fix(test): update analytics import path * refactor: move ChatsList utils to ChatsList directory * fix(test): update chatlist tests
140 lines
4.0 KiB
TypeScript
140 lines
4.0 KiB
TypeScript
/* eslint-disable max-lines */
|
|
"use client";
|
|
import axios, { AxiosResponse } from "axios";
|
|
import { UUID } from "crypto";
|
|
import { useParams, useRouter } from "next/navigation";
|
|
import { useEffect, useState } from "react";
|
|
import { useTranslation } from "react-i18next";
|
|
|
|
import { useSubscriptionApi } from "@/lib/api/subscription/useSubscriptionApi";
|
|
import { BrainRoleType } from "@/lib/components/BrainUsers/types";
|
|
import { useBrainContext } from "@/lib/context/BrainProvider/hooks/useBrainContext";
|
|
import { useToast } from "@/lib/hooks";
|
|
import { useEventTracking } from "@/services/analytics/june/useEventTracking";
|
|
|
|
// eslint-disable-next-line @typescript-eslint/explicit-module-boundary-types
|
|
export const useInvitation = () => {
|
|
const { t } = useTranslation(["brain", "invitation"]);
|
|
const params = useParams();
|
|
const brainId = params?.brainId as UUID | undefined;
|
|
const [isLoading, setIsLoading] = useState(false);
|
|
const [brainName, setBrainName] = useState<string>("");
|
|
const [role, setRole] = useState<BrainRoleType | undefined>();
|
|
const [isProcessingRequest, setIsProcessingRequest] = useState(false);
|
|
|
|
const { publish } = useToast();
|
|
const { track } = useEventTracking();
|
|
const { getInvitation, acceptInvitation, declineInvitation } =
|
|
useSubscriptionApi();
|
|
|
|
if (brainId === undefined) {
|
|
throw new Error(t("brainUndefined", { ns: "brain" }));
|
|
}
|
|
|
|
const { fetchAllBrains, setCurrentBrainId } = useBrainContext();
|
|
const router = useRouter();
|
|
|
|
useEffect(() => {
|
|
setIsLoading(true);
|
|
|
|
const checkInvitationValidity = async () => {
|
|
try {
|
|
const { name, role: assignedRole } = await getInvitation(brainId);
|
|
setBrainName(name);
|
|
setRole(assignedRole);
|
|
} catch (error) {
|
|
if (axios.isAxiosError(error) && error.response?.status === 404) {
|
|
publish({
|
|
variant: "warning",
|
|
text: t("invitationNotFound", { ns: "invitation" }),
|
|
});
|
|
} else {
|
|
publish({
|
|
variant: "danger",
|
|
text: t("errorCheckingInvitation", { ns: "invitation" }),
|
|
});
|
|
}
|
|
router.push("/");
|
|
} finally {
|
|
setIsLoading(false);
|
|
}
|
|
};
|
|
void checkInvitationValidity();
|
|
}, [brainId]);
|
|
|
|
const handleAccept = async () => {
|
|
setIsProcessingRequest(true);
|
|
try {
|
|
await acceptInvitation(brainId);
|
|
void track("INVITATION_ACCEPTED");
|
|
|
|
await fetchAllBrains();
|
|
publish({
|
|
variant: "success",
|
|
text: t("accept", { ns: "invitation" }),
|
|
});
|
|
setCurrentBrainId(brainId);
|
|
} catch (error) {
|
|
if (axios.isAxiosError(error) && error.response?.data !== undefined) {
|
|
publish({
|
|
variant: "danger",
|
|
text: (
|
|
error.response as AxiosResponse<{
|
|
detail: string;
|
|
}>
|
|
).data.detail,
|
|
});
|
|
} else {
|
|
console.error("Error calling the API:", error);
|
|
publish({
|
|
variant: "danger",
|
|
text: t("errorAccepting", { ns: "invitation" }),
|
|
});
|
|
}
|
|
} finally {
|
|
setIsProcessingRequest(false);
|
|
void router.push("/chat");
|
|
}
|
|
};
|
|
|
|
const handleDecline = async () => {
|
|
setIsProcessingRequest(true);
|
|
try {
|
|
await declineInvitation(brainId);
|
|
publish({
|
|
variant: "success",
|
|
text: t("declined", { ns: "invitation" }),
|
|
});
|
|
void track("INVITATION_DECLINED");
|
|
} catch (error) {
|
|
if (axios.isAxiosError(error) && error.response?.data !== undefined) {
|
|
publish({
|
|
variant: "danger",
|
|
text: (
|
|
error.response as AxiosResponse<{
|
|
detail: string;
|
|
}>
|
|
).data.detail,
|
|
});
|
|
} else {
|
|
publish({
|
|
variant: "danger",
|
|
text: t("errorDeclining", { ns: "invitation" }),
|
|
});
|
|
}
|
|
} finally {
|
|
setIsProcessingRequest(false);
|
|
void router.push("/chat");
|
|
}
|
|
};
|
|
|
|
return {
|
|
handleAccept,
|
|
handleDecline,
|
|
brainName,
|
|
role,
|
|
isLoading,
|
|
isProcessingRequest,
|
|
};
|
|
};
|