Skip to content

Commit

Permalink
Add handleSubmitAuth function to manage authentication and image proc…
Browse files Browse the repository at this point in the history
…essing in Home component
  • Loading branch information
eraykeskinmac committed Nov 29, 2024
1 parent 53612bc commit 53e8ac9
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 1 deletion.
23 changes: 22 additions & 1 deletion app/page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import modelsList from "@/lib/models.json";
import templates, { TemplateId } from "@/lib/templates";
import { ChatSettings } from "@/components/chat-settings";
import { LLMModelConfig } from "@/lib/models";
import { Message, toAISDKMessages } from "@/lib/messsages";
import { Message, toAISDKMessages, toMessageImage } from "@/lib/messsages";
import { DeepPartial } from "ai";
import { CapsuleSchema, capsuleSchema as schema } from "@/lib/schema";
import { experimental_useObject as useObject } from "ai/react";
Expand Down Expand Up @@ -143,6 +143,27 @@ export default function Home() {
setChatInput(e.target.value);
}

async function handleSubmitAuth(e: React.FormEvent<HTMLFormElement>) {
e.preventDefault();

if (session) {
return setAuthDialog(false);
}

if (isLoading) {
stop();
}

const content: Message["content"] = [{ type: "text", text: chatInput }];
const images = await toMessageImage(files);

if (images.length > 0) {
images.forEach((image) => {
content.push({ type: "image", image });
});
}
}

return (
<main className="flex min-h-screen max-h-screen">
{supabase && (
Expand Down
13 changes: 13 additions & 0 deletions lib/messsages.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -38,3 +38,16 @@ export function toAISDKMessages(messages: Message[]) {
});
});
}

export async function toMessageImage(files: File[]) {
if (files.length === 0) {
return [];
}

return Promise.all(
files.map(async (file) => {
const base64 = Buffer.from(await file.arrayBuffer()).toString("base64");
return `data:${file.type};base64,${base64}`;
})
);
}

0 comments on commit 53e8ac9

Please sign in to comment.