Skip to content

Commit

Permalink
Merge pull request #83 from jphacks/feat/ai-button
Browse files Browse the repository at this point in the history
AI生成のボタンを実装
  • Loading branch information
gohan5858 authored Oct 27, 2024
2 parents 9565380 + 56e17e7 commit c1a5b1c
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 2 deletions.
7 changes: 7 additions & 0 deletions task_yell/src/app/home/actions.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
"use server";

import { splitWantToDo } from "@/lib/ai";

export async function generateStickyNoteServer(note: string) {
return await splitWantToDo({ title: note, id: "" });
}
24 changes: 24 additions & 0 deletions task_yell/src/app/home/page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ import { Switch } from "@/components/ui/switch";
import { Textarea } from "@/components/ui/textarea";
import { signOut } from "@/firebase/auth";
import { auth, db } from "@/firebase/client-app";
import { splitWantToDo } from "@/lib/ai";
import { createEvent, readEvents } from "@/lib/events";
import { createNotification } from "@/lib/notifications";
import { subscribeNotification } from "@/lib/push-notification";
Expand Down Expand Up @@ -80,10 +81,12 @@ import {
Users,
Download,
LogOut,
Brain,
PhoneCall,
} from "lucide-react";
import { useRouter } from "next/navigation";
import { useEffect, useMemo, useRef, useState } from "react";
import { generateStickyNoteServer } from "./actions";

type Todo = {
id: string;
Expand Down Expand Up @@ -524,6 +527,20 @@ export default function Home() {
setEditingStickyNote(note);
};

const generateStickyNote = async (note: StickyNote) => {
if (auth.currentUser) {
const uid = auth.currentUser.uid;
const generated = await generateStickyNoteServer(note.title);
// Firestoreに追加
if (generated) {
const items = await Promise.all(
generated.map(async (item) => ({ id: await createWantTodo(uid, item), title: item.title })),
);
setStickyNotes([...stickyNotes, ...items]);
}
}
};

const updateStickyNote = async (updatedNote: StickyNote) => {
const updatedNotes = stickyNotes.map((note) =>
note.id === updatedNote.id ? updatedNote : note,
Expand Down Expand Up @@ -742,6 +759,13 @@ export default function Home() {
>
<h3 className="font-semibold mb-2">{note.title}</h3>
<div className="flex justify-end space-x-2">
<Button
variant="ghost"
size="icon"
onClick={() => generateStickyNote(note)}
>
<Brain className="h-4 w-4" />
</Button>
<Button
variant="ghost"
size="icon"
Expand Down
4 changes: 2 additions & 2 deletions task_yell/src/lib/want-todo.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,8 @@ const COLLECTION_NAME = "want-todos";
export async function createWantTodo(
userId: string,
wantTodo: WantTodo,
): Promise<void> {
await createData<WantTodo>(`users/${userId}/${COLLECTION_NAME}`, wantTodo);
): Promise<string> {
return await createData<WantTodo>(`users/${userId}/${COLLECTION_NAME}`, wantTodo);
}

/**
Expand Down

0 comments on commit c1a5b1c

Please sign in to comment.