mirror of
https://github.com/FranP-code/create-better-t-stack.git
synced 2025-10-12 23:52:15 +00:00
add todo, ai template for next
This commit is contained in:
5
.changeset/curvy-jobs-unite.md
Normal file
5
.changeset/curvy-jobs-unite.md
Normal file
@@ -0,0 +1,5 @@
|
|||||||
|
---
|
||||||
|
"create-better-t-stack": patch
|
||||||
|
---
|
||||||
|
|
||||||
|
add todo, ai template for next
|
||||||
@@ -494,6 +494,20 @@ export async function setupExamplesTemplate(
|
|||||||
|
|
||||||
const exampleBaseDir = path.join(PKG_ROOT, `templates/examples/${example}`);
|
const exampleBaseDir = path.join(PKG_ROOT, `templates/examples/${example}`);
|
||||||
|
|
||||||
|
if (example === "ai" && context.backend === "next" && serverAppDirExists) {
|
||||||
|
const aiNextServerSrc = path.join(exampleBaseDir, "server/next");
|
||||||
|
|
||||||
|
if (await fs.pathExists(aiNextServerSrc)) {
|
||||||
|
await processAndCopyFiles(
|
||||||
|
"**/*",
|
||||||
|
aiNextServerSrc,
|
||||||
|
serverAppDir,
|
||||||
|
context,
|
||||||
|
false,
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
if (serverAppDirExists) {
|
if (serverAppDirExists) {
|
||||||
const exampleServerSrc = path.join(exampleBaseDir, "server");
|
const exampleServerSrc = path.join(exampleBaseDir, "server");
|
||||||
if (await fs.pathExists(exampleServerSrc)) {
|
if (await fs.pathExists(exampleServerSrc)) {
|
||||||
|
|||||||
@@ -353,8 +353,6 @@ function processAndValidateFlags(
|
|||||||
);
|
);
|
||||||
if (config.backend !== "convex" && options.examples.includes("none")) {
|
if (config.backend !== "convex" && options.examples.includes("none")) {
|
||||||
config.examples = [];
|
config.examples = [];
|
||||||
} else {
|
|
||||||
config.examples = ["todo"];
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -0,0 +1,15 @@
|
|||||||
|
import { google } from '@ai-sdk/google';
|
||||||
|
import { streamText } from 'ai';
|
||||||
|
|
||||||
|
export const maxDuration = 30;
|
||||||
|
|
||||||
|
export async function POST(req: Request) {
|
||||||
|
const { messages } = await req.json();
|
||||||
|
|
||||||
|
const result = streamText({
|
||||||
|
model: google('gemini-2.0-flash'),
|
||||||
|
messages,
|
||||||
|
});
|
||||||
|
|
||||||
|
return result.toDataStreamResponse();
|
||||||
|
}
|
||||||
@@ -0,0 +1,67 @@
|
|||||||
|
"use client"
|
||||||
|
|
||||||
|
import { useChat } from "@ai-sdk/react";
|
||||||
|
import { Input } from "@/components/ui/input";
|
||||||
|
import { Button } from "@/components/ui/button";
|
||||||
|
import { Send } from "lucide-react";
|
||||||
|
import { useRef, useEffect } from "react";
|
||||||
|
|
||||||
|
|
||||||
|
export default function AIPage() {
|
||||||
|
const { messages, input, handleInputChange, handleSubmit } = useChat({
|
||||||
|
api: `${process.env.NEXT_PUBLIC_SERVER_URL}/ai`,
|
||||||
|
});
|
||||||
|
|
||||||
|
const messagesEndRef = useRef<HTMLDivElement>(null);
|
||||||
|
|
||||||
|
useEffect(() => {
|
||||||
|
messagesEndRef.current?.scrollIntoView({ behavior: "smooth" });
|
||||||
|
}, [messages]);
|
||||||
|
|
||||||
|
return (
|
||||||
|
<div className="grid grid-rows-[1fr_auto] overflow-hidden w-full mx-auto p-4">
|
||||||
|
<div className="overflow-y-auto space-y-4 pb-4">
|
||||||
|
{messages.length === 0 ? (
|
||||||
|
<div className="text-center text-muted-foreground mt-8">
|
||||||
|
Ask me anything to get started!
|
||||||
|
</div>
|
||||||
|
) : (
|
||||||
|
messages.map((message) => (
|
||||||
|
<div
|
||||||
|
key={message.id}
|
||||||
|
className={`p-3 rounded-lg ${
|
||||||
|
message.role === "user"
|
||||||
|
? "bg-primary/10 ml-8"
|
||||||
|
: "bg-secondary/20 mr-8"
|
||||||
|
}`}
|
||||||
|
>
|
||||||
|
<p className="text-sm font-semibold mb-1">
|
||||||
|
{message.role === "user" ? "You" : "AI Assistant"}
|
||||||
|
</p>
|
||||||
|
<div className="whitespace-pre-wrap">{message.content}</div>
|
||||||
|
</div>
|
||||||
|
))
|
||||||
|
)}
|
||||||
|
<div ref={messagesEndRef} />
|
||||||
|
</div>
|
||||||
|
|
||||||
|
<form
|
||||||
|
onSubmit={handleSubmit}
|
||||||
|
className="w-full flex items-center space-x-2 pt-2 border-t"
|
||||||
|
>
|
||||||
|
<Input
|
||||||
|
name="prompt"
|
||||||
|
value={input}
|
||||||
|
onChange={handleInputChange}
|
||||||
|
placeholder="Type your message..."
|
||||||
|
className="flex-1"
|
||||||
|
autoComplete="off"
|
||||||
|
autoFocus
|
||||||
|
/>
|
||||||
|
<Button type="submit" size="icon">
|
||||||
|
<Send size={18} />
|
||||||
|
</Button>
|
||||||
|
</form>
|
||||||
|
</div>
|
||||||
|
);
|
||||||
|
}
|
||||||
@@ -0,0 +1,162 @@
|
|||||||
|
"use client"
|
||||||
|
|
||||||
|
import { Button } from "@/components/ui/button";
|
||||||
|
import {
|
||||||
|
Card,
|
||||||
|
CardContent,
|
||||||
|
CardDescription,
|
||||||
|
CardHeader,
|
||||||
|
CardTitle,
|
||||||
|
} from "@/components/ui/card";
|
||||||
|
import { Checkbox } from "@/components/ui/checkbox";
|
||||||
|
import { Input } from "@/components/ui/input";
|
||||||
|
import { Loader2, Trash2 } from "lucide-react";
|
||||||
|
import { useState } from "react";
|
||||||
|
import { useMutation, useQuery } from "@tanstack/react-query";
|
||||||
|
|
||||||
|
{{#if (eq api "orpc")}}
|
||||||
|
import { orpc } from "@/utils/orpc";
|
||||||
|
{{/if}}
|
||||||
|
{{#if (eq api "trpc")}}
|
||||||
|
import { trpc } from "@/utils/trpc";
|
||||||
|
{{/if}}
|
||||||
|
|
||||||
|
|
||||||
|
export default function TodosPage() {
|
||||||
|
const [newTodoText, setNewTodoText] = useState("");
|
||||||
|
|
||||||
|
{{#if (eq api "orpc")}}
|
||||||
|
const todos = useQuery(orpc.todo.getAll.queryOptions());
|
||||||
|
const createMutation = useMutation(
|
||||||
|
orpc.todo.create.mutationOptions({
|
||||||
|
onSuccess: () => {
|
||||||
|
todos.refetch();
|
||||||
|
setNewTodoText("");
|
||||||
|
},
|
||||||
|
}),
|
||||||
|
);
|
||||||
|
const toggleMutation = useMutation(
|
||||||
|
orpc.todo.toggle.mutationOptions({
|
||||||
|
onSuccess: () => todos.refetch(),
|
||||||
|
}),
|
||||||
|
);
|
||||||
|
const deleteMutation = useMutation(
|
||||||
|
orpc.todo.delete.mutationOptions({
|
||||||
|
onSuccess: () => todos.refetch(),
|
||||||
|
}),
|
||||||
|
);
|
||||||
|
{{/if}}
|
||||||
|
{{#if (eq api "trpc")}}
|
||||||
|
const todos = useQuery(trpc.todo.getAll.queryOptions());
|
||||||
|
const createMutation = useMutation(
|
||||||
|
trpc.todo.create.mutationOptions({
|
||||||
|
onSuccess: () => {
|
||||||
|
todos.refetch();
|
||||||
|
setNewTodoText("");
|
||||||
|
},
|
||||||
|
}),
|
||||||
|
);
|
||||||
|
const toggleMutation = useMutation(
|
||||||
|
trpc.todo.toggle.mutationOptions({
|
||||||
|
onSuccess: () => todos.refetch(),
|
||||||
|
}),
|
||||||
|
);
|
||||||
|
const deleteMutation = useMutation(
|
||||||
|
trpc.todo.delete.mutationOptions({
|
||||||
|
onSuccess: () => todos.refetch(),
|
||||||
|
}),
|
||||||
|
);
|
||||||
|
{{/if}}
|
||||||
|
|
||||||
|
const handleAddTodo = (e: React.FormEvent) => {
|
||||||
|
e.preventDefault();
|
||||||
|
if (newTodoText.trim()) {
|
||||||
|
createMutation.mutate({ text: newTodoText });
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
const handleToggleTodo = (id: number, completed: boolean) => {
|
||||||
|
toggleMutation.mutate({ id, completed: !completed });
|
||||||
|
};
|
||||||
|
|
||||||
|
const handleDeleteTodo = (id: number) => {
|
||||||
|
deleteMutation.mutate({ id });
|
||||||
|
};
|
||||||
|
|
||||||
|
return (
|
||||||
|
<div className="mx-auto w-full max-w-md py-10">
|
||||||
|
<Card>
|
||||||
|
<CardHeader>
|
||||||
|
<CardTitle>Todo List</CardTitle>
|
||||||
|
<CardDescription>Manage your tasks efficiently</CardDescription>
|
||||||
|
</CardHeader>
|
||||||
|
<CardContent>
|
||||||
|
<form
|
||||||
|
onSubmit={handleAddTodo}
|
||||||
|
className="mb-6 flex items-center space-x-2"
|
||||||
|
>
|
||||||
|
<Input
|
||||||
|
value={newTodoText}
|
||||||
|
onChange={(e) => setNewTodoText(e.target.value)}
|
||||||
|
placeholder="Add a new task..."
|
||||||
|
disabled={createMutation.isPending}
|
||||||
|
/>
|
||||||
|
<Button
|
||||||
|
type="submit"
|
||||||
|
disabled={createMutation.isPending || !newTodoText.trim()}
|
||||||
|
>
|
||||||
|
{createMutation.isPending ? (
|
||||||
|
<Loader2 className="h-4 w-4 animate-spin" />
|
||||||
|
) : (
|
||||||
|
"Add"
|
||||||
|
)}
|
||||||
|
</Button>
|
||||||
|
</form>
|
||||||
|
|
||||||
|
{todos.isLoading ? (
|
||||||
|
<div className="flex justify-center py-4">
|
||||||
|
<Loader2 className="h-6 w-6 animate-spin" />
|
||||||
|
</div>
|
||||||
|
) : todos.data?.length === 0 ? (
|
||||||
|
<p className="py-4 text-center">
|
||||||
|
No todos yet. Add one above!
|
||||||
|
</p>
|
||||||
|
) : (
|
||||||
|
<ul className="space-y-2">
|
||||||
|
{todos.data?.map((todo) => (
|
||||||
|
<li
|
||||||
|
key={todo.id}
|
||||||
|
className="flex items-center justify-between rounded-md border p-2"
|
||||||
|
>
|
||||||
|
<div className="flex items-center space-x-2">
|
||||||
|
<Checkbox
|
||||||
|
checked={todo.completed}
|
||||||
|
onCheckedChange={() =>
|
||||||
|
handleToggleTodo(todo.id, todo.completed)
|
||||||
|
}
|
||||||
|
id={`todo-${todo.id}`}
|
||||||
|
/>
|
||||||
|
<label
|
||||||
|
htmlFor={`todo-${todo.id}`}
|
||||||
|
className={`${todo.completed ? "line-through text-muted-foreground" : ""}`}
|
||||||
|
>
|
||||||
|
{todo.text}
|
||||||
|
</label>
|
||||||
|
</div>
|
||||||
|
<Button
|
||||||
|
variant="ghost"
|
||||||
|
size="icon"
|
||||||
|
onClick={() => handleDeleteTodo(todo.id)}
|
||||||
|
aria-label="Delete todo"
|
||||||
|
>
|
||||||
|
<Trash2 className="h-4 w-4" />
|
||||||
|
</Button>
|
||||||
|
</li>
|
||||||
|
))}
|
||||||
|
</ul>
|
||||||
|
)}
|
||||||
|
</CardContent>
|
||||||
|
</Card>
|
||||||
|
</div>
|
||||||
|
);
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user