2023-07-07 13:56:08 +03:00
|
|
|
/* eslint-disable max-lines */
|
|
|
|
import { UUID } from "crypto";
|
|
|
|
import { useCallback, useState } from "react";
|
|
|
|
|
2023-07-25 13:08:08 +03:00
|
|
|
import { CreateBrainInput } from "@/lib/api/brain/brain";
|
2023-07-07 13:56:08 +03:00
|
|
|
import { useBrainApi } from "@/lib/api/brain/useBrainApi";
|
|
|
|
import { useToast } from "@/lib/hooks";
|
|
|
|
import { useEventTracking } from "@/services/analytics/useEventTracking";
|
|
|
|
|
|
|
|
import {
|
|
|
|
getBrainFromLocalStorage,
|
|
|
|
saveBrainInLocalStorage,
|
|
|
|
} from "../helpers/brainLocalStorage";
|
2023-07-18 15:30:19 +03:00
|
|
|
import { MinimalBrainForUser } from "../types";
|
2023-07-07 13:56:08 +03:00
|
|
|
|
|
|
|
// CAUTION: This hook should be use in BrainProvider only. You may be need `useBrainContext` instead.
|
|
|
|
|
|
|
|
// 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-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);
|
|
|
|
const [isFetchingBrains, setIsFetchingBrains] = useState(false);
|
|
|
|
|
|
|
|
const currentBrain = allBrains.find((brain) => brain.id === currentBrainId);
|
|
|
|
|
|
|
|
const createBrainHandler = async (
|
2023-07-25 13:08:08 +03:00
|
|
|
brain: CreateBrainInput
|
2023-07-07 13:56:08 +03:00
|
|
|
): Promise<UUID | undefined> => {
|
2023-07-25 13:08:08 +03:00
|
|
|
const createdBrain = await createBrain(brain);
|
2023-07-07 13:56:08 +03:00
|
|
|
try {
|
|
|
|
setAllBrains((prevBrains) => [...prevBrains, createdBrain]);
|
|
|
|
saveBrainInLocalStorage(createdBrain);
|
|
|
|
void track("BRAIN_CREATED");
|
|
|
|
|
|
|
|
return createdBrain.id;
|
|
|
|
} catch {
|
|
|
|
publish({
|
|
|
|
variant: "danger",
|
2023-07-16 00:21:24 +03:00
|
|
|
text: "Error occurred while creating a brain",
|
2023-07-07 13:56:08 +03:00
|
|
|
});
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
const deleteBrainHandler = async (id: UUID) => {
|
|
|
|
await deleteBrain(id);
|
|
|
|
setAllBrains((prevBrains) => prevBrains.filter((brain) => brain.id !== id));
|
|
|
|
void track("DELETE_BRAIN");
|
2023-07-17 15:38:34 +03:00
|
|
|
publish({
|
|
|
|
variant: "success",
|
|
|
|
text: "Brain deleted",
|
|
|
|
});
|
2023-07-07 13:56:08 +03:00
|
|
|
};
|
|
|
|
|
|
|
|
const fetchAllBrains = useCallback(async () => {
|
|
|
|
setIsFetchingBrains(true);
|
|
|
|
try {
|
|
|
|
const brains = await getBrains();
|
|
|
|
setAllBrains(brains);
|
|
|
|
} catch (error) {
|
|
|
|
console.error(error);
|
|
|
|
} finally {
|
|
|
|
setIsFetchingBrains(false);
|
|
|
|
}
|
|
|
|
}, []);
|
|
|
|
|
|
|
|
const setActiveBrain = useCallback(
|
|
|
|
({ id, name }: { id: UUID; name: string }) => {
|
|
|
|
const newActiveBrain = { id, name };
|
|
|
|
saveBrainInLocalStorage(newActiveBrain);
|
|
|
|
setCurrentBrainId(id);
|
|
|
|
void track("CHANGE_BRAIN");
|
|
|
|
},
|
|
|
|
[]
|
|
|
|
);
|
|
|
|
|
|
|
|
const setDefaultBrain = useCallback(async () => {
|
|
|
|
const defaultBrain = await getDefaultBrain();
|
|
|
|
if (defaultBrain !== undefined) {
|
|
|
|
saveBrainInLocalStorage(defaultBrain);
|
|
|
|
setActiveBrain({ ...defaultBrain });
|
|
|
|
} else {
|
|
|
|
console.warn("No brains found");
|
|
|
|
}
|
|
|
|
}, [setActiveBrain]);
|
|
|
|
|
|
|
|
const fetchAndSetActiveBrain = useCallback(async () => {
|
|
|
|
const storedBrain = getBrainFromLocalStorage();
|
|
|
|
if (storedBrain?.id !== undefined) {
|
|
|
|
setActiveBrain({ ...storedBrain });
|
|
|
|
} else {
|
|
|
|
await setDefaultBrain();
|
|
|
|
}
|
|
|
|
}, [setDefaultBrain, setActiveBrain]);
|
|
|
|
|
|
|
|
return {
|
|
|
|
currentBrain,
|
|
|
|
currentBrainId,
|
|
|
|
allBrains,
|
|
|
|
createBrain: createBrainHandler,
|
|
|
|
deleteBrain: deleteBrainHandler,
|
|
|
|
setActiveBrain,
|
|
|
|
fetchAllBrains,
|
|
|
|
setDefaultBrain,
|
|
|
|
fetchAndSetActiveBrain,
|
|
|
|
isFetchingBrains,
|
|
|
|
};
|
|
|
|
};
|