openCanvas

This commit is contained in:
董宏凯 committed 2026-05-15 23:27:34 +08:00
1 parent 050b1ad78e
commit 1be600155b
18 files changed
+754 -101

No files matched your search

@@ -1,8 +1,9 @@
import { useState } from "react";
import { useNavigate } from "react-router-dom";
import { v4 as uuidv4 } from "uuid";
import { FileText, Code2, Sparkles } from "lucide-react";
import { FileText, Code2, Sparkles, Loader2 } from "lucide-react";
import { Button } from "@/components/ui/button";
import { createCanvasThread } from "../api/threads";
const QUICK_START_ITEMS = [
{
@@ -21,10 +22,30 @@ const QUICK_START_ITEMS = [
export default function CanvasHomePage() {
const navigate = useNavigate();
const [loading, setLoading] = useState(false);
function handleQuickStart(type: "text" | "code") {
const threadId = uuidv4();
navigate(`${threadId}?type=${type}`);
async function handleQuickStart(type: "text" | "code") {
if (loading) return;
setLoading(true);
try {
const threadId = await createCanvasThread();
navigate(`${threadId}?type=${type}`);
} catch (err) {
console.error("[canvas] failed to create thread:", err);
setLoading(false);
}
}
async function handleBlankCanvas() {
if (loading) return;
setLoading(true);
try {
const threadId = await createCanvasThread();
navigate(threadId);
} catch (err) {
console.error("[canvas] failed to create thread:", err);
setLoading(false);
}
}
return (
@@ -44,11 +65,16 @@ export default function CanvasHomePage() {
{QUICK_START_ITEMS.map(({ type, icon: Icon, title, description }) => (
<button
key={type}
onClick={() => handleQuickStart(type)}
className="group flex flex-col gap-3 rounded-xl border bg-card p-5 text-left transition-all hover:border-primary/50 hover:shadow-md hover:bg-accent/30 focus:outline-none focus-visible:ring-2 focus-visible:ring-ring"
onClick={() => void handleQuickStart(type)}
disabled={loading}
className="group flex flex-col gap-3 rounded-xl border bg-card p-5 text-left transition-all hover:border-primary/50 hover:shadow-md hover:bg-accent/30 focus:outline-none focus-visible:ring-2 focus-visible:ring-ring disabled:opacity-50 disabled:cursor-not-allowed"
>
<div className="flex items-center justify-center w-9 h-9 rounded-lg bg-muted group-hover:bg-primary/10 transition-colors">
<Icon className="w-4 h-4 text-muted-foreground group-hover:text-primary transition-colors" />
{loading ? (
<Loader2 className="w-4 h-4 animate-spin text-muted-foreground" />
) : (
<Icon className="w-4 h-4 text-muted-foreground group-hover:text-primary transition-colors" />
)}
</div>
<div className="space-y-1">
<p className="font-medium text-sm">{title}</p>
@@ -65,8 +91,12 @@ export default function CanvasHomePage() {
variant="ghost"
size="sm"
className="text-muted-foreground text-xs"
onClick={() => navigate(uuidv4())}
onClick={() => void handleBlankCanvas()}
disabled={loading}
>
{loading ? (
<Loader2 className="w-3 h-3 animate-spin mr-1" />
) : null}
或直接开始空白 Canvas
</Button>
</div>