added localStorage functionality to the entire project, LLM keeps getting overloaded

This commit is contained in:
Ivan Rudik
2025-11-09 09:24:47 +00:00
parent 6421160279
commit 64be41f894
10 changed files with 447 additions and 702 deletions
+19 -3
View File
@@ -4,15 +4,16 @@ import { ChatHistory } from "@/app/exportType/types";
import { User, Bot } from "lucide-react"
interface MessageWindowProps {
history: ChatHistory
history: ChatHistory;
isLoading: boolean;
}
export default function MessageWindow({ history }: MessageWindowProps) {
export default function MessageWindow({ history, isLoading }: MessageWindowProps) {
const messageEndRef = useRef<HTMLDivElement>(null);
useEffect(() => {
messageEndRef.current?.scrollIntoView({ behavior: "smooth" });
}, [history]);
}, [history, isLoading]);
return (
<div className="flex-1 p-3 overflow-y-auto">
@@ -51,6 +52,21 @@ export default function MessageWindow({ history }: MessageWindowProps) {
</div>
);
})}
{isLoading && (
<div className="flex justify-start mb-4">
<div className="mr-2">
<div className="flex items-center justify-center w-8 h-8 rounded-full bg-gray-200">
<Bot size={15} className="text-gray-700" />
</div>
</div>
<div className="px-4 py-2 shadow-sm rounded-lg bg-white text-gray-800 rounded-bl-none max-w-xs sm:max-w-md">
<div className="whitespace-pre-wrap break-words">
{/* Simple loading dots animation */}
<span className="animate-pulse">...</span>
</div>
</div>
</div>
)}
{/* Invisible element to help scroll to bottom */}
<div ref={messageEndRef} />
+3 -3
View File
@@ -16,7 +16,7 @@ export async function chatToGemini(
): Promise<string> {
const model = genAI.getGenerativeModel({
model: settings.model || "gemini-2.5-flash",
systemInstruction: settings.sysTemInstructions || "you are going to create a project idea, explain it in summery first before going to detail",
systemInstruction: settings.systemInstructions || "you are going to create a project idea, explain it in summery first before going to detail",
});
const generationConfig: GenerateConfig = {
@@ -36,7 +36,7 @@ export async function chatToGemini(
return result.response.text();
}
catch (error) {
console.error(error)
throw error
console.error("Gemini API Error:", error);
throw error;
}
}
+94
View File
@@ -0,0 +1,94 @@
import { FormData } from "@/app/formfile/formTypes";
import { ChatHistory } from "@/app/exportType/types";
// 1. Define the data structures for storage
export interface StoredProject extends FormData {
id: string;
title: string;
}
export interface StoredChatSession {
id: string;
projectId: string;
projectTitle: string;
history: ChatHistory;
}
export interface UserData {
projects: StoredProject[];
chatSessions: StoredChatSession[];
}
const STORAGE_KEY = "hackSparkUserData";
function safeGetLocalStorage(): UserData {
if (typeof window === 'undefined') {
return { projects: [], chatSessions: [] };
}
try {
const data = localStorage.getItem(STORAGE_KEY);
return data ? JSON.parse(data) : { projects: [], chatSessions: [] };
} catch (error) {
console.error("Failed to parse localStorage data:", error);
return { projects: [], chatSessions: [] };
}
}
//Retrieves all stored projects.
export function getStoredProjects(): StoredProject[] {
return safeGetLocalStorage().projects;
}
//Retrieves all stored chat sessions.
export function getStoredChatSessions(): StoredChatSession[] {
return safeGetLocalStorage().chatSessions;
}
//Finds a specific chat session by its project ID.
export function getChatSessionByProjectId(projectId: string): StoredChatSession | undefined {
if (typeof window === 'undefined') return undefined;
const data = safeGetLocalStorage();
return data.chatSessions.find(session => session.projectId === projectId);
}
export function saveProject(formData: FormData): StoredProject {
if (typeof window === 'undefined') throw new Error("localStorage is not available.");
const data = safeGetLocalStorage();
const newProject: StoredProject = {
...formData,
id: new Date().toISOString(),
title: formData.Hackathon || "Untitled Project",
};
data.projects.push(newProject);
localStorage.setItem(STORAGE_KEY, JSON.stringify(data));
return newProject;
}
export function saveChatSession(projectId: string, projectTitle: string, history: ChatHistory) {
if (typeof window === 'undefined') return;
const data = safeGetLocalStorage();
const existingSessionIndex = data.chatSessions.findIndex(session => session.projectId === projectId);
if (existingSessionIndex > -1) {
data.chatSessions[existingSessionIndex].history = history;
} else {
const newSession: StoredChatSession = {
id: new Date().toISOString(),
projectId,
projectTitle,
history,
};
data.chatSessions.push(newSession);
}
localStorage.setItem(STORAGE_KEY, JSON.stringify(data));
}
export function setProjectForEditing(project: StoredProject) {
if (typeof window === 'undefined') return;
localStorage.setItem('formData', JSON.stringify(project));
}