mirror of
https://github.com/QuivrHQ/quivr.git
synced 2024-12-15 17:43:03 +03:00
61bb7b2e02
# Description Please include a summary of the changes and the related issue. Please also include relevant motivation and context. ## Checklist before requesting a review Please delete options that are not relevant. - [ ] My code follows the style guidelines of this project - [ ] I have performed a self-review of my code - [ ] I have commented hard-to-understand areas - [ ] I have ideally added tests that prove my fix is effective or that my feature works - [ ] New and existing unit tests pass locally with my changes - [ ] Any dependent changes have been merged ## Screenshots (if appropriate):
126 lines
3.7 KiB
TypeScript
126 lines
3.7 KiB
TypeScript
/* eslint-disable max-lines */
|
|
import { AxiosResponse, isAxiosError } from "axios";
|
|
import { useState } from "react";
|
|
import { useTranslation } from "react-i18next";
|
|
|
|
import { generateBrainAssignation } from "@/app/studio/[brainId]/BrainManagementTabs/components/PeopleTab/BrainUsers/utils/generateBrainAssignation";
|
|
import { Subscription } from "@/lib/api/brain/brain";
|
|
import { useBrainApi } from "@/lib/api/brain/useBrainApi";
|
|
import { useToast } from "@/lib/hooks";
|
|
|
|
import { BrainRoleAssignation } from "../../app/studio/[brainId]/BrainManagementTabs/components/PeopleTab/BrainUsers/types";
|
|
|
|
// eslint-disable-next-line @typescript-eslint/explicit-module-boundary-types
|
|
export const useShareBrain = (brainId: string) => {
|
|
const [sendingInvitation, setSendingInvitation] = useState(false);
|
|
const [isShareModalOpen, setIsShareModalOpen] = useState(false);
|
|
const [roleAssignations, setRoleAssignation] = useState<
|
|
BrainRoleAssignation[]
|
|
>([generateBrainAssignation()]);
|
|
const { t } = useTranslation(["brain"]);
|
|
|
|
const baseUrl = window.location.origin;
|
|
const brainShareLink = `${baseUrl}/invitation/${brainId}`;
|
|
|
|
const { publish } = useToast();
|
|
const { addBrainSubscriptions } = useBrainApi();
|
|
|
|
const handleCopyInvitationLink = async () => {
|
|
await navigator.clipboard.writeText(brainShareLink);
|
|
publish({
|
|
variant: "success",
|
|
text: t("copiedToClipboard", { ns: "brain" }),
|
|
});
|
|
};
|
|
|
|
const removeRoleAssignation = (assignationIndex: number) => () => {
|
|
if (roleAssignations.length === 1) {
|
|
return;
|
|
}
|
|
setRoleAssignation(
|
|
roleAssignations.filter((_, index) => index !== assignationIndex)
|
|
);
|
|
};
|
|
|
|
const updateRoleAssignation =
|
|
(rowIndex: number) => (data: BrainRoleAssignation) => {
|
|
const concernedRow = roleAssignations[rowIndex];
|
|
|
|
if (concernedRow !== undefined) {
|
|
setRoleAssignation(
|
|
roleAssignations.map((row, index) => {
|
|
if (index === rowIndex) {
|
|
return data;
|
|
}
|
|
|
|
return row;
|
|
})
|
|
);
|
|
} else {
|
|
setRoleAssignation([...roleAssignations, data]);
|
|
}
|
|
};
|
|
|
|
const inviteUsers = async (): Promise<void> => {
|
|
setSendingInvitation(true);
|
|
try {
|
|
const inviteUsersPayload: Subscription[] = roleAssignations
|
|
.filter(({ email }) => email !== "")
|
|
.map((assignation) => ({
|
|
email: assignation.email,
|
|
role: assignation.role,
|
|
}));
|
|
|
|
await addBrainSubscriptions(brainId, inviteUsersPayload);
|
|
|
|
publish({
|
|
variant: "success",
|
|
text: t("usersInvited", { ns: "brain" }),
|
|
});
|
|
|
|
setIsShareModalOpen(false);
|
|
setRoleAssignation([generateBrainAssignation()]);
|
|
} catch (error) {
|
|
if (isAxiosError(error) && error.response?.data !== undefined) {
|
|
publish({
|
|
variant: "danger",
|
|
text: (
|
|
error.response as AxiosResponse<{
|
|
detail: string;
|
|
}>
|
|
).data.detail,
|
|
});
|
|
} else {
|
|
publish({
|
|
variant: "danger",
|
|
text: t("errorSendingInvitation"),
|
|
});
|
|
}
|
|
} finally {
|
|
setSendingInvitation(false);
|
|
}
|
|
};
|
|
|
|
const addNewRoleAssignationRole = () => {
|
|
setRoleAssignation([...roleAssignations, generateBrainAssignation()]);
|
|
};
|
|
const canAddNewRow =
|
|
roleAssignations.length === 0 ||
|
|
roleAssignations.filter((invitingUser) => invitingUser.email === "")
|
|
.length === 0;
|
|
|
|
return {
|
|
roleAssignations,
|
|
brainShareLink,
|
|
handleCopyInvitationLink,
|
|
updateRoleAssignation,
|
|
removeRoleAssignation,
|
|
inviteUsers,
|
|
addNewRoleAssignationRole,
|
|
sendingInvitation,
|
|
setIsShareModalOpen,
|
|
isShareModalOpen,
|
|
canAddNewRow,
|
|
};
|
|
};
|