Files
mnote/wolai-frontend/src/app/page.tsx
T

44 lines
1.2 KiB
TypeScript
Raw Normal View History

2025-11-23 10:55:04 +08:00
import { redirect } from "next/navigation";
2026-01-17 10:12:53 +08:00
import { isConvexEnabled } from "@/lib/convex/enabled";
2026-01-18 19:01:31 +08:00
import { getAuthedConvexClient } from "@/lib/convex/route";
2026-01-17 10:12:53 +08:00
import { api } from "@/lib/convex/api";
function makeId(): string {
return typeof crypto.randomUUID === "function"
? crypto.randomUUID()
: `${Date.now()}_${Math.random().toString(16).slice(2)}`;
}
2025-11-23 10:55:04 +08:00
export default async function Home() {
2026-01-18 19:01:31 +08:00
if (!isConvexEnabled()) {
redirect("/auth");
2026-01-17 10:12:53 +08:00
}
2026-01-18 19:01:31 +08:00
const { auth, client } = await getAuthedConvexClient();
2025-11-23 10:55:04 +08:00
2026-01-18 19:01:31 +08:00
const { activeWorkspaceId } = await client.mutation(api.workspaces.ensureDefaultWorkspace, {
fallbackName: auth.name ?? auth.email ?? "我的空间",
workspaceIdIfCreate: makeId(),
});
2025-11-23 10:55:04 +08:00
2026-01-18 19:01:31 +08:00
const docs = await client.query(api.documents.listByWorkspace, {
workspaceId: activeWorkspaceId,
});
2025-11-23 10:55:04 +08:00
2026-01-18 19:01:31 +08:00
const firstDoc = [...docs].sort((a, b) => (a.created_at ?? "").localeCompare(b.created_at ?? ""))[0];
2025-11-23 10:55:04 +08:00
if (firstDoc?.id) {
redirect(`/documents/${firstDoc.id}`);
}
2026-01-18 19:01:31 +08:00
const docId = makeId();
await client.mutation(api.documents.create, {
id: docId,
workspaceId: activeWorkspaceId,
parentId: null,
title: "新页面",
accessScope: "private",
content: [],
});
redirect(`/documents/${docId}`);
2025-11-23 10:55:04 +08:00
}