mirror of
https://github.com/StanGirard/quivr.git
synced 2024-12-19 00:22:14 +03:00
e3925bcbc0
Issue: https://github.com/StanGirard/quivr/issues/1435 - feat(knowledgeTab): update structure - refactor: change AddKnowledge structure - feat: change AddKnowledge component structure - feat: rework sources logic - feat: change knowledge tab upload process - fix: change knowledge tab fetch, create, update logic - feat: improve added knowledge ui - style: improve responsivity Fix: - https://github.com/StanGirard/quivr/issues/1516 - https://github.com/StanGirard/quivr/issues/1336 - https://github.com/StanGirard/quivr/issues/1204 https://github.com/StanGirard/quivr/assets/63923024/f2917bf3-4ff8-42c6-8149-0b36287441b4
99 lines
2.9 KiB
TypeScript
99 lines
2.9 KiB
TypeScript
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 { useBrainContext } from "@/lib/context/BrainProvider/hooks/useBrainContext";
|
|
import { useToast } from "@/lib/hooks";
|
|
import { useEventTracking } from "@/services/analytics/june/useEventTracking";
|
|
|
|
import { BrainManagementTab } from "../types";
|
|
import { getBrainPermissions } from "../utils/getBrainPermissions";
|
|
import { getTargetedTab } from "../utils/getTargetedTab";
|
|
|
|
// eslint-disable-next-line @typescript-eslint/explicit-module-boundary-types
|
|
export const useBrainManagementTabs = () => {
|
|
const [selectedTab, setSelectedTab] =
|
|
useState<BrainManagementTab>("settings");
|
|
const { allBrains } = useBrainContext();
|
|
const [
|
|
isDeleteOrUnsubscribeRequestPending,
|
|
setIsDeleteOrUnsubscribeRequestPending,
|
|
] = useState(false);
|
|
|
|
useEffect(() => {
|
|
const targetedTab = getTargetedTab();
|
|
if (targetedTab !== undefined) {
|
|
setSelectedTab(targetedTab);
|
|
}
|
|
}, []);
|
|
|
|
const { track } = useEventTracking();
|
|
const { publish } = useToast();
|
|
|
|
const { unsubscribeFromBrain } = useSubscriptionApi();
|
|
const { deleteBrain, setCurrentBrainId, fetchAllBrains } = useBrainContext();
|
|
const [
|
|
isDeleteOrUnsubscribeModalOpened,
|
|
setIsDeleteOrUnsubscribeModalOpened,
|
|
] = useState(false);
|
|
const router = useRouter();
|
|
|
|
const params = useParams();
|
|
const { t } = useTranslation(["delete_or_unsubscribe_from_brain"]);
|
|
const brainId = params?.brainId as UUID | undefined;
|
|
|
|
const { hasEditRights, isOwnedByCurrentUser } = getBrainPermissions({
|
|
brainId,
|
|
userAccessibleBrains: allBrains,
|
|
});
|
|
|
|
const handleUnSubscription = async () => {
|
|
if (brainId === undefined) {
|
|
return;
|
|
}
|
|
await unsubscribeFromBrain(brainId);
|
|
|
|
void track("UNSUBSCRIBE_FROM_BRAIN");
|
|
publish({
|
|
variant: "success",
|
|
text: t("successfully_unsubscribed"),
|
|
});
|
|
};
|
|
|
|
const handleUnsubscribeOrDeleteBrain = async () => {
|
|
if (brainId === undefined) {
|
|
return;
|
|
}
|
|
setIsDeleteOrUnsubscribeRequestPending(true);
|
|
try {
|
|
if (!isOwnedByCurrentUser) {
|
|
await handleUnSubscription();
|
|
} else {
|
|
await deleteBrain(brainId);
|
|
}
|
|
setCurrentBrainId(null);
|
|
setIsDeleteOrUnsubscribeModalOpened(false);
|
|
void fetchAllBrains();
|
|
router.push("/brains-management");
|
|
} catch (error) {
|
|
console.error("Error deleting brain: ", error);
|
|
} finally {
|
|
setIsDeleteOrUnsubscribeRequestPending(false);
|
|
}
|
|
};
|
|
|
|
return {
|
|
selectedTab,
|
|
setSelectedTab,
|
|
brainId,
|
|
handleUnsubscribeOrDeleteBrain,
|
|
isDeleteOrUnsubscribeModalOpened,
|
|
setIsDeleteOrUnsubscribeModalOpened,
|
|
hasEditRights,
|
|
isOwnedByCurrentUser,
|
|
isDeleteOrUnsubscribeRequestPending,
|
|
};
|
|
};
|