Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
71 changes: 53 additions & 18 deletions hooks/useCalculations.ts
Original file line number Diff line number Diff line change
@@ -1,25 +1,43 @@
import { database } from "@/pages/_document";
import { User } from "firebase/auth";
import { get, ref, set, update } from "firebase/database";
import { get, ref, set } from "firebase/database";
import { useEffect, useState } from "react";
import { useCustomAuth } from "./useCustomAuth";

interface Calculation {
export interface Notebook {
id: string;
name: string;
input: string;
output: string;
output: string | null;
color?: string;
}

export const NOTEBOOK_COLORS = [
"#3b82f6",
"#ef4444",
"#22c55e",
"#f59e0b",
"#a855f7",
"#ec4899",
"#14b8a6",
"#f97316",
];

interface NotebooksData {
notebooks: Notebook[];
activeNotebookId: string;
}

interface UseCalculationsResult {
calculations: Calculation | null;
saveCalculations: (input: string, output: string) => void;
getCalculations: () => void;
notebooksData: NotebooksData | null;
saveNotebooks: (notebooks: Notebook[], activeNotebookId: string) => Promise<void>;
isLoading: boolean;
error: Error | null;
}

export const useCalculations = (): UseCalculationsResult => {
const user: User | null = useCustomAuth();
const [calculations, setCalculations] = useState<Calculation | null>(null);
const [notebooksData, setNotebooksData] = useState<NotebooksData | null>(null);
const [isLoading, setIsLoading] = useState(false);
const [error, setError] = useState<Error | null>(null);
const [calculationsRef, setCalculationsRef] = useState<any>(null);
Expand All @@ -33,15 +51,15 @@ export const useCalculations = (): UseCalculationsResult => {

useEffect(() => {
if (!user) {
setCalculations(null);
setNotebooksData(null);
setHasFetchedCalculations(false);
setCalculationsRef(null);
}
}, [user]);

useEffect(() => {
if (!hasFetchedCalculations && calculationsRef) {
getCalculations(); // Pass the reference here
getCalculations();
setHasFetchedCalculations(true);
}
}, [hasFetchedCalculations, calculationsRef]);
Expand All @@ -51,26 +69,43 @@ export const useCalculations = (): UseCalculationsResult => {
setIsLoading(true);
setError(null);
const snapshot = await get(calculationsRef);
const data = snapshot.val() as Calculation;
setCalculations(data);
const data = snapshot.val();

if (!data) {
setNotebooksData(null);
return;
}

if (data.notebooks) {
setNotebooksData(data as NotebooksData);
} else if (data.input !== undefined) {
setNotebooksData({
notebooks: [
{
id: "1",
name: "General Expense",
input: data.input,
output: data.output ?? null,
color: NOTEBOOK_COLORS[0],
},
],
activeNotebookId: "1",
});
}
} catch (error) {
setError(error as Error);
} finally {
setIsLoading(false);
}
};

const saveCalculations = async (input: string, output: string) => {
const saveNotebooks = async (notebooks: Notebook[], activeNotebookId: string) => {
try {
setIsLoading(true);
setError(null);

if (calculationsRef) {
if (!calculations) {
await set(calculationsRef, { input, output });
} else {
await update(calculationsRef, { input, output });
}
await set(calculationsRef, { notebooks, activeNotebookId });
}
} catch (error) {
setError(error as Error);
Expand All @@ -79,5 +114,5 @@ export const useCalculations = (): UseCalculationsResult => {
}
};

return { calculations, saveCalculations, getCalculations, isLoading, error };
return { notebooksData, saveNotebooks, isLoading, error };
};
Loading
Loading