mirror of
https://github.com/StanGirard/quivr.git
synced 2024-12-12 11:26:07 +03:00
efe4e8ccb0
# Description Please include a summary of the changes and the related issue. Please also include relevant motivation and context. ## Checklist before requesting a review fix/brain-management-shared-brain-access - [ ] 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.6 KiB
TypeScript
126 lines
3.6 KiB
TypeScript
/* eslint-disable max-lines */
|
|
import axios, { AxiosResponse } from "axios";
|
|
import { useState } from "react";
|
|
import { useTranslation } from "react-i18next";
|
|
|
|
import { Subscription } from "@/lib/api/brain/brain";
|
|
import { useBrainApi } from "@/lib/api/brain/useBrainApi";
|
|
import { useToast } from "@/lib/hooks";
|
|
|
|
import { BrainRoleAssignation } from "../components/BrainUsers/types";
|
|
import { generateBrainAssignation } from "../components/ShareBrain/utils/generateBrainAssignation";
|
|
|
|
// 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 (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("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,
|
|
};
|
|
};
|