2023-06-20 17:17:13 +03:00
|
|
|
/* eslint-disable max-lines */
|
|
|
|
import { UUID } from "crypto";
|
|
|
|
import { useCallback, useEffect, useState } from "react";
|
|
|
|
|
2023-06-28 20:39:27 +03:00
|
|
|
import {
|
|
|
|
createBrainFromBackend,
|
|
|
|
deleteBrainFromBE,
|
|
|
|
getAllUserBrainsFromBE,
|
|
|
|
getBrainFromBE,
|
|
|
|
getUserDefaultBrainFromBackend,
|
|
|
|
} from "@/lib/api";
|
2023-06-20 17:17:13 +03:00
|
|
|
import { useAxios, useToast } from "@/lib/hooks";
|
|
|
|
|
2023-06-28 20:39:27 +03:00
|
|
|
import {
|
|
|
|
getBrainFromLocalStorage,
|
|
|
|
saveBrainInLocalStorage,
|
|
|
|
} from "../helpers/brainLocalStorage";
|
2023-06-20 17:17:13 +03:00
|
|
|
import { Brain } from "../types";
|
|
|
|
|
|
|
|
export interface BrainStateProps {
|
|
|
|
currentBrain: Brain | undefined;
|
2023-06-28 20:39:27 +03:00
|
|
|
currentBrainId: UUID | null;
|
2023-06-20 17:17:13 +03:00
|
|
|
allBrains: Brain[];
|
2023-06-28 20:39:27 +03:00
|
|
|
createBrain: (name: string) => Promise<UUID | undefined>;
|
2023-06-20 17:17:13 +03:00
|
|
|
deleteBrain: (id: UUID) => Promise<void>;
|
|
|
|
setActiveBrain: (id: UUID) => void;
|
|
|
|
getBrainWithId: (brainId: UUID) => Promise<Brain>;
|
|
|
|
fetchAllBrains: () => Promise<void>;
|
2023-06-28 20:39:27 +03:00
|
|
|
setDefaultBrain: () => Promise<void>;
|
2023-06-20 17:17:13 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
export const useBrainState = (): BrainStateProps => {
|
|
|
|
const { publish } = useToast();
|
|
|
|
|
|
|
|
const [allBrains, setAllBrains] = useState<Brain[]>([]);
|
|
|
|
const [currentBrainId, setCurrentBrainId] = useState<null | UUID>(null);
|
|
|
|
const { axiosInstance } = useAxios();
|
|
|
|
|
|
|
|
const currentBrain = allBrains.find((brain) => brain.id === currentBrainId);
|
|
|
|
|
|
|
|
// options: Record<string, string | unknown>;
|
|
|
|
|
2023-06-28 20:39:27 +03:00
|
|
|
const createBrain = async (name: string): Promise<UUID | undefined> => {
|
2023-06-20 17:17:13 +03:00
|
|
|
const createdBrain = await createBrainFromBackend(axiosInstance, name);
|
|
|
|
if (createdBrain !== undefined) {
|
|
|
|
setAllBrains((prevBrains) => [...prevBrains, createdBrain]);
|
2023-06-28 20:39:27 +03:00
|
|
|
saveBrainInLocalStorage(createdBrain);
|
|
|
|
|
|
|
|
return createdBrain.id;
|
2023-06-20 17:17:13 +03:00
|
|
|
} else {
|
|
|
|
publish({
|
|
|
|
variant: "danger",
|
|
|
|
text: "Error occured while creating a brain",
|
|
|
|
});
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
const deleteBrain = async (id: UUID) => {
|
|
|
|
await deleteBrainFromBE(axiosInstance, id);
|
|
|
|
setAllBrains((prevBrains) => prevBrains.filter((brain) => brain.id !== id));
|
|
|
|
};
|
|
|
|
|
|
|
|
const getBrainWithId = async (brainId: UUID): Promise<Brain> => {
|
|
|
|
const brain =
|
|
|
|
allBrains.find(({ id }) => id === brainId) ??
|
|
|
|
(await getBrainFromBE(axiosInstance, brainId));
|
|
|
|
|
|
|
|
if (brain === undefined) {
|
|
|
|
throw new Error(`Error finding brain ${brainId}`);
|
|
|
|
}
|
|
|
|
|
|
|
|
return brain;
|
|
|
|
};
|
|
|
|
|
|
|
|
const fetchAllBrains = useCallback(async () => {
|
|
|
|
try {
|
|
|
|
console.log("Fetching all brains for a user");
|
|
|
|
const brains = await getAllUserBrainsFromBE(axiosInstance);
|
|
|
|
console.log(brains);
|
|
|
|
setAllBrains(brains ?? []);
|
|
|
|
console.log("Fetched all brains for user");
|
|
|
|
} catch (error) {
|
|
|
|
console.error(error);
|
|
|
|
}
|
|
|
|
}, [axiosInstance]);
|
|
|
|
|
2023-06-28 20:39:27 +03:00
|
|
|
const setActiveBrain = useCallback((id: UUID) => {
|
|
|
|
//get brain with id from BE ?
|
|
|
|
|
|
|
|
const newActiveBrain = { id, name: "Default Brain" };
|
|
|
|
// if (newActiveBrain) {
|
|
|
|
console.log("newActiveBrain", newActiveBrain);
|
|
|
|
saveBrainInLocalStorage(newActiveBrain);
|
|
|
|
setCurrentBrainId(id);
|
|
|
|
console.log("Setting active brain", newActiveBrain);
|
|
|
|
// } else {
|
|
|
|
// console.warn(`No brain found with ID ${id}`);
|
|
|
|
// }
|
|
|
|
}, []);
|
|
|
|
|
|
|
|
const setDefaultBrain = useCallback(async () => {
|
|
|
|
console.log("Setting default brain");
|
|
|
|
const defaultBrain = await getUserDefaultBrainFromBackend(axiosInstance);
|
|
|
|
console.log("defaultBrain", defaultBrain);
|
|
|
|
if (defaultBrain) {
|
|
|
|
saveBrainInLocalStorage(defaultBrain);
|
|
|
|
setActiveBrain(defaultBrain.id);
|
|
|
|
} else {
|
|
|
|
console.warn("No brains found");
|
|
|
|
}
|
|
|
|
}, [axiosInstance, setActiveBrain]);
|
|
|
|
|
|
|
|
const fetchAndSetActiveBrain = useCallback(async () => {
|
|
|
|
console.log(
|
|
|
|
"Fetching and setting active brain use effect in useBrainState"
|
|
|
|
);
|
|
|
|
const storedBrain = getBrainFromLocalStorage();
|
|
|
|
if (storedBrain?.id !== undefined) {
|
|
|
|
console.log("Setting active brain from local storage");
|
|
|
|
console.log("storedBrain", storedBrain);
|
|
|
|
setActiveBrain(storedBrain.id);
|
|
|
|
} else {
|
|
|
|
console.log("Setting default brain for first time");
|
|
|
|
await setDefaultBrain();
|
|
|
|
}
|
|
|
|
}, [setDefaultBrain, setActiveBrain]);
|
|
|
|
|
2023-06-20 17:17:13 +03:00
|
|
|
useEffect(() => {
|
|
|
|
void fetchAllBrains();
|
2023-06-28 20:39:27 +03:00
|
|
|
|
|
|
|
console.log("brainId", currentBrainId);
|
|
|
|
void fetchAndSetActiveBrain();
|
|
|
|
}, [fetchAllBrains, fetchAndSetActiveBrain, currentBrainId]);
|
2023-06-20 17:17:13 +03:00
|
|
|
|
|
|
|
return {
|
|
|
|
currentBrain,
|
2023-06-28 20:39:27 +03:00
|
|
|
currentBrainId,
|
2023-06-20 17:17:13 +03:00
|
|
|
allBrains,
|
|
|
|
createBrain,
|
|
|
|
deleteBrain,
|
|
|
|
setActiveBrain,
|
|
|
|
getBrainWithId,
|
|
|
|
fetchAllBrains,
|
2023-06-28 20:39:27 +03:00
|
|
|
setDefaultBrain,
|
2023-06-20 17:17:13 +03:00
|
|
|
};
|
|
|
|
};
|