0.3.1 UI修复
This commit is contained in:
@@ -1,9 +1,6 @@
|
||||
import { redirect } from "next/navigation";
|
||||
import { createSupabaseServerClient } from "@/lib/supabase/server";
|
||||
import { ensureDefaultWorkspace, resolveActiveWorkspaceId } from "@/lib/workspaces";
|
||||
import { isConvexEnabled } from "@/lib/convex/enabled";
|
||||
import { requireAuthContext } from "@/lib/auth/authContext";
|
||||
import { getConvexHttpClient } from "@/lib/convex/server";
|
||||
import { getAuthedConvexClient } from "@/lib/convex/route";
|
||||
import { api } from "@/lib/convex/api";
|
||||
|
||||
function makeId(): string {
|
||||
@@ -13,80 +10,34 @@ function makeId(): string {
|
||||
}
|
||||
|
||||
export default async function Home() {
|
||||
if (isConvexEnabled()) {
|
||||
const auth = await requireAuthContext();
|
||||
const client = getConvexHttpClient();
|
||||
|
||||
const { activeWorkspaceId } = await client.mutation(api.workspaces.ensureDefaultWorkspace, {
|
||||
userId: auth.userId,
|
||||
fallbackName: auth.name ?? auth.email ?? "我的空间",
|
||||
workspaceIdIfCreate: makeId(),
|
||||
});
|
||||
|
||||
const docs = await client.query(api.documents.listByWorkspace, {
|
||||
userId: auth.userId,
|
||||
workspaceId: activeWorkspaceId,
|
||||
});
|
||||
|
||||
const firstDoc = [...docs].sort((a, b) => (a.created_at ?? "").localeCompare(b.created_at ?? ""))[0];
|
||||
if (firstDoc?.id) {
|
||||
redirect(`/documents/${firstDoc.id}`);
|
||||
}
|
||||
|
||||
const docId = makeId();
|
||||
await client.mutation(api.documents.create, {
|
||||
userId: auth.userId,
|
||||
id: docId,
|
||||
workspaceId: activeWorkspaceId,
|
||||
parentId: null,
|
||||
title: "新页面",
|
||||
accessScope: "private",
|
||||
content: [],
|
||||
});
|
||||
redirect(`/documents/${docId}`);
|
||||
if (!isConvexEnabled()) {
|
||||
redirect("/auth");
|
||||
}
|
||||
|
||||
const supabase = await createSupabaseServerClient();
|
||||
const session = (await supabase.auth.getSession()).data.session;
|
||||
const { auth, client } = await getAuthedConvexClient();
|
||||
|
||||
if (!session) {
|
||||
redirect("/login");
|
||||
}
|
||||
const { activeWorkspaceId } = await client.mutation(api.workspaces.ensureDefaultWorkspace, {
|
||||
fallbackName: auth.name ?? auth.email ?? "我的空间",
|
||||
workspaceIdIfCreate: makeId(),
|
||||
});
|
||||
|
||||
await ensureDefaultWorkspace(supabase, session.user.id, session.user.email ?? "我的空间");
|
||||
const workspaceId = await resolveActiveWorkspaceId(supabase, session.user.id);
|
||||
|
||||
if (!workspaceId) {
|
||||
redirect("/login");
|
||||
}
|
||||
|
||||
const { data: firstDoc } = await supabase
|
||||
.from("documents")
|
||||
.select("id")
|
||||
.eq("user_id", session.user.id)
|
||||
.eq("workspace_id", workspaceId)
|
||||
.order("created_at", { ascending: true })
|
||||
.limit(1)
|
||||
.maybeSingle();
|
||||
const docs = await client.query(api.documents.listByWorkspace, {
|
||||
workspaceId: activeWorkspaceId,
|
||||
});
|
||||
|
||||
const firstDoc = [...docs].sort((a, b) => (a.created_at ?? "").localeCompare(b.created_at ?? ""))[0];
|
||||
if (firstDoc?.id) {
|
||||
redirect(`/documents/${firstDoc.id}`);
|
||||
}
|
||||
|
||||
const { data: createdDoc, error } = await supabase
|
||||
.from("documents")
|
||||
.insert({
|
||||
user_id: session.user.id,
|
||||
workspace_id: workspaceId,
|
||||
title: "新页面",
|
||||
content: {},
|
||||
})
|
||||
.select("id")
|
||||
.single();
|
||||
|
||||
if (error || !createdDoc) {
|
||||
redirect("/login");
|
||||
}
|
||||
|
||||
redirect(`/documents/${createdDoc.id}`);
|
||||
const docId = makeId();
|
||||
await client.mutation(api.documents.create, {
|
||||
id: docId,
|
||||
workspaceId: activeWorkspaceId,
|
||||
parentId: null,
|
||||
title: "新页面",
|
||||
accessScope: "private",
|
||||
content: [],
|
||||
});
|
||||
redirect(`/documents/${docId}`);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user