Files
mnote/wolai-frontend/src/app/api/luckysheet/load/route.ts
T

49 lines
1.5 KiB
TypeScript
Raw Normal View History

2025-11-29 05:16:23 +08:00
"use server";
import { NextResponse } from "next/server";
import { createDefaultTableSnapshot } from "@/lib/online-table";
import { createSupabaseRouteClient } from "@/lib/supabase/server";
import type { DocumentTableSnapshot, TableSchema } from "@/types/online-table";
const fallbackSchema: TableSchema = {
columns: [],
frozenRowCount: 0,
frozenColCount: 0,
};
export async function POST(request: Request) {
const url = new URL(request.url);
const gridKey = url.searchParams.get("gridKey");
if (!gridKey) {
return NextResponse.json({ code: 400, msg: "gridKey 参数缺失" }, { status: 400 });
}
const supabase = await createSupabaseRouteClient();
const { data, error } = await supabase
.from("document_tables")
.select("schema,snapshot")
.eq("grid_key", gridKey)
.single();
if (error || !data) {
return NextResponse.json({ code: 404, msg: "未查询到相关数据" }, { status: 404 });
}
const snapshot = (data.snapshot as DocumentTableSnapshot | null) ?? null;
const schema = (data.schema as TableSchema | null) ?? fallbackSchema;
let payload: unknown[] = [];
if (snapshot?.luckysheet && Array.isArray(snapshot.luckysheet)) {
payload = snapshot.luckysheet;
} else {
const defaultSnapshot = createDefaultTableSnapshot(schema);
payload = defaultSnapshot.luckysheet ?? [];
}
const body = JSON.stringify(payload);
return new NextResponse(body, {
headers: { "Content-Type": "text/plain; charset=utf-8" },
});
}