2023-07-07 13:56:08 +03:00
|
|
|
/* eslint-disable max-lines */
|
|
|
|
import { UUID } from "crypto";
|
2023-09-18 22:28:07 +03:00
|
|
|
import { useCallback, useState } from "react";
|
2023-09-25 15:22:59 +03:00
|
|
|
import { useTranslation } from "react-i18next";
|
2023-07-07 13:56:08 +03:00
|
|
|
|
2023-11-20 20:04:26 +03:00
|
|
|
import { useBrainFetcher } from "@/app/brains-management/[brainId]/components/BrainManagementTabs/hooks/useBrainFetcher";
|
2023-07-26 00:12:46 +03:00
|
|
|
import { CreateBrainInput } from "@/lib/api/brain/types";
|
2023-07-07 13:56:08 +03:00
|
|
|
import { useBrainApi } from "@/lib/api/brain/useBrainApi";
|
2023-08-29 11:50:36 +03:00
|
|
|
import { usePromptApi } from "@/lib/api/prompt/usePromptApi";
|
2023-07-07 13:56:08 +03:00
|
|
|
import { useToast } from "@/lib/hooks";
|
2023-08-29 11:50:36 +03:00
|
|
|
import { Prompt } from "@/lib/types/Prompt";
|
2023-09-18 16:12:50 +03:00
|
|
|
import { useEventTracking } from "@/services/analytics/june/useEventTracking";
|
2023-07-07 13:56:08 +03:00
|
|
|
|
2023-07-18 15:30:19 +03:00
|
|
|
import { MinimalBrainForUser } from "../types";
|
2023-07-07 13:56:08 +03:00
|
|
|
|
|
|
|
// eslint-disable-next-line @typescript-eslint/explicit-module-boundary-types
|
|
|
|
export const useBrainProvider = () => {
|
|
|
|
const { publish } = useToast();
|
|
|
|
const { track } = useEventTracking();
|
2023-07-18 15:30:19 +03:00
|
|
|
const { createBrain, deleteBrain, getBrains, getDefaultBrain } =
|
2023-07-07 13:56:08 +03:00
|
|
|
useBrainApi();
|
2023-08-29 11:50:36 +03:00
|
|
|
const { getPublicPrompts } = usePromptApi();
|
2023-09-25 15:22:59 +03:00
|
|
|
const { t } = useTranslation(["delete_or_unsubscribe_from_brain"]);
|
2023-07-07 13:56:08 +03:00
|
|
|
|
2023-07-18 15:30:19 +03:00
|
|
|
const [allBrains, setAllBrains] = useState<MinimalBrainForUser[]>([]);
|
2023-07-07 13:56:08 +03:00
|
|
|
const [currentBrainId, setCurrentBrainId] = useState<null | UUID>(null);
|
2023-07-26 00:12:46 +03:00
|
|
|
const [defaultBrainId, setDefaultBrainId] = useState<UUID>();
|
2023-12-18 16:23:45 +03:00
|
|
|
const [isFetchingBrains, setIsFetchingBrains] = useState(true);
|
2023-08-29 11:50:36 +03:00
|
|
|
const [publicPrompts, setPublicPrompts] = useState<Prompt[]>([]);
|
|
|
|
const [currentPromptId, setCurrentPromptId] = useState<null | string>(null);
|
2023-07-07 13:56:08 +03:00
|
|
|
|
2023-08-29 11:50:36 +03:00
|
|
|
const currentPrompt = publicPrompts.find(
|
|
|
|
(prompt) => prompt.id === currentPromptId
|
|
|
|
);
|
2023-07-07 13:56:08 +03:00
|
|
|
const currentBrain = allBrains.find((brain) => brain.id === currentBrainId);
|
2023-11-20 20:04:26 +03:00
|
|
|
const { brain: currentBrainDetails } = useBrainFetcher({
|
|
|
|
brainId: currentBrainId ?? undefined,
|
|
|
|
});
|
2023-07-07 13:56:08 +03:00
|
|
|
|
2023-09-26 11:35:52 +03:00
|
|
|
const fetchAllBrains = useCallback(async () => {
|
|
|
|
setIsFetchingBrains(true);
|
|
|
|
try {
|
|
|
|
const brains = await getBrains();
|
|
|
|
setAllBrains(brains);
|
|
|
|
} catch (error) {
|
|
|
|
console.error(error);
|
|
|
|
} finally {
|
|
|
|
setIsFetchingBrains(false);
|
|
|
|
}
|
|
|
|
}, [getBrains]);
|
|
|
|
|
2023-09-18 22:28:07 +03:00
|
|
|
const createBrainHandler = useCallback(
|
|
|
|
async (brain: CreateBrainInput): Promise<UUID | undefined> => {
|
|
|
|
const createdBrain = await createBrain(brain);
|
|
|
|
try {
|
|
|
|
setCurrentBrainId(createdBrain.id);
|
|
|
|
|
2023-11-15 14:42:19 +03:00
|
|
|
void track("BRAIN_CREATED", {
|
|
|
|
brainType: brain.brain_type,
|
|
|
|
});
|
|
|
|
|
2023-09-26 11:35:52 +03:00
|
|
|
void fetchAllBrains();
|
2023-09-18 22:28:07 +03:00
|
|
|
|
|
|
|
return createdBrain.id;
|
|
|
|
} catch {
|
|
|
|
publish({
|
|
|
|
variant: "danger",
|
|
|
|
text: "Error occurred while creating a brain",
|
|
|
|
});
|
|
|
|
}
|
|
|
|
},
|
2023-09-26 11:35:52 +03:00
|
|
|
[createBrain, fetchAllBrains, publish, track]
|
2023-09-18 22:28:07 +03:00
|
|
|
);
|
|
|
|
|
|
|
|
const deleteBrainHandler = useCallback(
|
|
|
|
async (id: UUID) => {
|
|
|
|
await deleteBrain(id);
|
|
|
|
setAllBrains((prevBrains) =>
|
|
|
|
prevBrains.filter((brain) => brain.id !== id)
|
|
|
|
);
|
|
|
|
void track("DELETE_BRAIN");
|
2023-07-07 13:56:08 +03:00
|
|
|
publish({
|
2023-09-18 22:28:07 +03:00
|
|
|
variant: "success",
|
2023-09-25 15:22:59 +03:00
|
|
|
text: t("successfully_deleted"),
|
2023-07-07 13:56:08 +03:00
|
|
|
});
|
2023-09-18 22:28:07 +03:00
|
|
|
},
|
|
|
|
[deleteBrain, publish, track]
|
|
|
|
);
|
2023-07-07 13:56:08 +03:00
|
|
|
|
2023-09-18 22:28:07 +03:00
|
|
|
const fetchDefaultBrain = useCallback(async () => {
|
2023-07-26 00:12:46 +03:00
|
|
|
const userDefaultBrain = await getDefaultBrain();
|
|
|
|
if (userDefaultBrain !== undefined) {
|
2023-09-18 22:28:07 +03:00
|
|
|
setDefaultBrainId(userDefaultBrain.id);
|
2023-07-07 13:56:08 +03:00
|
|
|
}
|
2023-09-18 22:28:07 +03:00
|
|
|
if (currentBrainId === null && userDefaultBrain !== undefined) {
|
|
|
|
setCurrentBrainId(userDefaultBrain.id);
|
2023-07-07 13:56:08 +03:00
|
|
|
}
|
2023-09-18 22:28:07 +03:00
|
|
|
}, [currentBrainId, getDefaultBrain]);
|
2023-08-29 11:50:36 +03:00
|
|
|
|
2023-09-18 22:28:07 +03:00
|
|
|
const fetchPublicPrompts = useCallback(async () => {
|
2023-08-29 11:50:36 +03:00
|
|
|
setPublicPrompts(await getPublicPrompts());
|
2023-09-18 22:28:07 +03:00
|
|
|
}, [getPublicPrompts]);
|
2023-07-26 00:12:46 +03:00
|
|
|
|
2023-07-07 13:56:08 +03:00
|
|
|
return {
|
|
|
|
allBrains,
|
|
|
|
fetchAllBrains,
|
|
|
|
isFetchingBrains,
|
2023-09-18 22:28:07 +03:00
|
|
|
|
|
|
|
currentBrain,
|
2023-11-20 20:04:26 +03:00
|
|
|
currentBrainDetails,
|
2023-09-18 22:28:07 +03:00
|
|
|
currentBrainId,
|
|
|
|
setCurrentBrainId,
|
|
|
|
|
2023-07-26 00:12:46 +03:00
|
|
|
defaultBrainId,
|
2023-07-26 17:54:03 +03:00
|
|
|
fetchDefaultBrain,
|
2023-09-18 22:28:07 +03:00
|
|
|
|
2023-08-29 11:50:36 +03:00
|
|
|
fetchPublicPrompts,
|
|
|
|
publicPrompts,
|
|
|
|
currentPrompt,
|
2023-09-18 22:28:07 +03:00
|
|
|
|
2023-08-29 11:50:36 +03:00
|
|
|
setCurrentPromptId,
|
|
|
|
currentPromptId,
|
2023-09-18 22:28:07 +03:00
|
|
|
|
|
|
|
createBrain: createBrainHandler,
|
|
|
|
|
|
|
|
deleteBrain: deleteBrainHandler,
|
2023-07-07 13:56:08 +03:00
|
|
|
};
|
|
|
|
};
|