2025-11-23 10:55:04 +08:00
|
|
|
import { NextResponse } from "next/server";
|
|
|
|
|
import type { SidebarInitialData } from "@/components/sidebar/types";
|
2026-01-17 10:12:53 +08:00
|
|
|
import { isConvexEnabled } from "@/lib/convex/enabled";
|
|
|
|
|
import { api } from "@/lib/convex/api";
|
2026-01-18 19:01:31 +08:00
|
|
|
import { getAuthedConvexClient } from "@/lib/convex/route";
|
2026-01-17 10:12:53 +08:00
|
|
|
import { randomUUID } from "crypto";
|
2026-01-08 06:28:14 +08:00
|
|
|
import type { MediaAsset } from "@/types/media";
|
2025-11-23 10:55:04 +08:00
|
|
|
|
|
|
|
|
export const dynamic = "force-dynamic";
|
|
|
|
|
|
2026-01-17 10:12:53 +08:00
|
|
|
function extractMindmapImageAssetIdsFromData(input: unknown): string[] {
|
|
|
|
|
const root = (() => {
|
|
|
|
|
if (!input || typeof input !== "object") return input;
|
|
|
|
|
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
|
|
|
const record = input as any;
|
|
|
|
|
// 兼容:某些导图结构为 { root: ... }
|
|
|
|
|
return record && typeof record === "object" && "root" in record ? record.root : input;
|
|
|
|
|
})();
|
|
|
|
|
|
|
|
|
|
const ids: string[] = [];
|
|
|
|
|
const seen = new Set<string>();
|
|
|
|
|
|
|
|
|
|
const push = (value: unknown) => {
|
|
|
|
|
if (typeof value !== "string") return;
|
|
|
|
|
if (!value.startsWith("asset:")) return;
|
|
|
|
|
const id = value.slice("asset:".length).trim();
|
|
|
|
|
if (!id) return;
|
|
|
|
|
if (seen.has(id)) return;
|
|
|
|
|
seen.add(id);
|
|
|
|
|
ids.push(id);
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
const get = (obj: unknown, key: string): unknown => {
|
|
|
|
|
if (!obj || typeof obj !== "object") return undefined;
|
|
|
|
|
return (obj as Record<string, unknown>)[key];
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
const walk = (node: unknown) => {
|
|
|
|
|
if (!node || typeof node !== "object") return;
|
|
|
|
|
|
|
|
|
|
const data = get(node, "data");
|
|
|
|
|
const image = get(node, "image");
|
|
|
|
|
|
|
|
|
|
// 常见:node.data.image = "asset:xxx"
|
|
|
|
|
push(get(data, "image"));
|
|
|
|
|
// 兼容:node.image = "asset:xxx"
|
|
|
|
|
push(image);
|
|
|
|
|
// 兼容:node.image.url = "asset:xxx"
|
|
|
|
|
push(get(image, "url"));
|
|
|
|
|
// 兼容:node.data.image.url = "asset:xxx"
|
|
|
|
|
push(get(get(data, "image"), "url"));
|
|
|
|
|
|
|
|
|
|
const children = get(node, "children");
|
|
|
|
|
if (Array.isArray(children)) {
|
|
|
|
|
children.forEach(walk);
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
walk(root);
|
|
|
|
|
return ids;
|
|
|
|
|
}
|
|
|
|
|
|
2025-11-23 10:55:04 +08:00
|
|
|
export async function GET(request: Request) {
|
2026-01-17 10:12:53 +08:00
|
|
|
if (isConvexEnabled()) {
|
2026-01-18 19:01:31 +08:00
|
|
|
const { auth, client } = await getAuthedConvexClient();
|
2026-01-17 10:12:53 +08:00
|
|
|
|
|
|
|
|
const url = new URL(request.url);
|
|
|
|
|
const workspaceIdParam = url.searchParams.get("workspaceId");
|
|
|
|
|
|
|
|
|
|
const bootstrap = await client.mutation(api.workspaces.ensureDefaultWorkspace, {
|
|
|
|
|
fallbackName: auth.email ?? auth.name ?? "我的空间",
|
|
|
|
|
workspaceIdIfCreate: randomUUID(),
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
const summaries = await client.query(api.workspaces.fetchWorkspaceSummaries, {
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
const workspaces = summaries.workspaces.length > 0 ? summaries.workspaces : bootstrap.workspaces;
|
|
|
|
|
const activeWorkspaceId = summaries.activeWorkspaceId || bootstrap.activeWorkspaceId;
|
|
|
|
|
const targetWorkspaceId = workspaceIdParam || activeWorkspaceId;
|
|
|
|
|
|
|
|
|
|
if (!targetWorkspaceId) {
|
|
|
|
|
return NextResponse.json({ error: "暂无可用工作空间" }, { status: 404 });
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
try {
|
|
|
|
|
const documents = await client.query(api.documents.listByWorkspace, {
|
|
|
|
|
workspaceId: targetWorkspaceId,
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
const trashedDocuments = await client.query(api.documents.listTrashedByWorkspace, {
|
|
|
|
|
workspaceId: targetWorkspaceId,
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
const mindmapRows = await client.query(api.mindmaps.listByWorkspace, {
|
|
|
|
|
workspaceId: targetWorkspaceId,
|
|
|
|
|
includeDeleted: true,
|
|
|
|
|
});
|
|
|
|
|
|
2026-01-20 07:24:12 +08:00
|
|
|
const mediaAssets = await client.query(api.mediaAssets.listByWorkspace, {
|
|
|
|
|
userId: auth.userId,
|
|
|
|
|
workspaceId: targetWorkspaceId,
|
|
|
|
|
limit: 200,
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
const trashedMediaAssets = await client.query(api.mediaAssets.listDeletedByWorkspace, {
|
|
|
|
|
userId: auth.userId,
|
|
|
|
|
workspaceId: targetWorkspaceId,
|
|
|
|
|
limit: 2000,
|
|
|
|
|
});
|
|
|
|
|
|
2026-02-01 08:47:40 +08:00
|
|
|
const tables = await client.query(api.tables.listByWorkspaceForSearch, {
|
|
|
|
|
userId: auth.userId,
|
|
|
|
|
workspaceId: targetWorkspaceId,
|
|
|
|
|
includeArchived: true,
|
|
|
|
|
limit: 3000,
|
|
|
|
|
});
|
|
|
|
|
|
2026-01-17 10:12:53 +08:00
|
|
|
const activeMindmaps = (mindmapRows ?? []).filter((r) => !r.deleted_at);
|
|
|
|
|
const trashedMindmaps = (mindmapRows ?? []).filter((r) => !!r.deleted_at);
|
|
|
|
|
|
|
|
|
|
const mindmapDocs = Array.from(new Set(activeMindmaps.map((r) => r.document_id)));
|
|
|
|
|
|
|
|
|
|
const mindmapAssetChildren: Record<string, string[]> = {};
|
|
|
|
|
activeMindmaps.forEach((r) => {
|
|
|
|
|
const ids = extractMindmapImageAssetIdsFromData(r.data);
|
|
|
|
|
if (ids.length > 0) {
|
|
|
|
|
mindmapAssetChildren[r.mindmap_id] = ids;
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
const mindmapAssets: MediaAsset[] = activeMindmaps.map((r) => {
|
|
|
|
|
const isLegacy = r.mindmap_id.startsWith("legacy-");
|
|
|
|
|
return {
|
|
|
|
|
id: r.mindmap_id,
|
|
|
|
|
workspace_id: r.workspace_id ?? targetWorkspaceId,
|
|
|
|
|
document_id: r.document_id,
|
|
|
|
|
asset_type: "mindmap",
|
|
|
|
|
file_url: null,
|
|
|
|
|
thumbnail_url: null,
|
|
|
|
|
bucket: null,
|
|
|
|
|
storage_path: null,
|
|
|
|
|
file_name: isLegacy ? "mindmap.json" : `mindmap-${r.mindmap_id}.json`,
|
|
|
|
|
file_size: null,
|
|
|
|
|
mime_type: "application/json",
|
|
|
|
|
ocr_payload: undefined,
|
|
|
|
|
ocr_strategy: null,
|
|
|
|
|
ocr_text: null,
|
|
|
|
|
ocr_status: null,
|
|
|
|
|
signed_url: null,
|
|
|
|
|
created_at: r.created_at ?? "",
|
|
|
|
|
updated_at: r.updated_at ?? "",
|
|
|
|
|
};
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
const trashedMindmapAssets: MediaAsset[] = trashedMindmaps.map((r) => {
|
|
|
|
|
const isLegacy = r.mindmap_id.startsWith("legacy-");
|
|
|
|
|
return {
|
|
|
|
|
id: r.mindmap_id,
|
|
|
|
|
workspace_id: r.workspace_id ?? targetWorkspaceId,
|
|
|
|
|
document_id: r.document_id,
|
|
|
|
|
asset_type: "mindmap",
|
|
|
|
|
file_url: null,
|
|
|
|
|
thumbnail_url: null,
|
|
|
|
|
bucket: null,
|
|
|
|
|
storage_path: null,
|
|
|
|
|
file_name: isLegacy ? "mindmap.json" : `mindmap-${r.mindmap_id}.json`,
|
|
|
|
|
file_size: null,
|
|
|
|
|
mime_type: "application/json",
|
|
|
|
|
ocr_payload: undefined,
|
|
|
|
|
ocr_strategy: null,
|
|
|
|
|
ocr_text: null,
|
|
|
|
|
ocr_status: null,
|
|
|
|
|
deleted_at: r.deleted_at ?? null,
|
|
|
|
|
deleted_by: r.deleted_by ?? null,
|
|
|
|
|
purged_at: null,
|
|
|
|
|
signed_url: null,
|
|
|
|
|
created_at: r.created_at ?? "",
|
|
|
|
|
updated_at: r.updated_at ?? "",
|
|
|
|
|
};
|
|
|
|
|
});
|
|
|
|
|
|
2026-02-01 08:47:40 +08:00
|
|
|
const tableAssets: MediaAsset[] = (tables ?? [])
|
|
|
|
|
.filter((row) => !row.is_archived)
|
|
|
|
|
.map((row) => {
|
|
|
|
|
const base = String(row.title ?? "未命名表格").trim() || "未命名表格";
|
|
|
|
|
const fileName = base.toLowerCase().endsWith(".luckysheet") ? base : `${base}.luckysheet`;
|
|
|
|
|
return {
|
|
|
|
|
id: row.id,
|
|
|
|
|
workspace_id: row.workspace_id ?? targetWorkspaceId,
|
|
|
|
|
document_id: row.document_id,
|
|
|
|
|
asset_type: "luckysheet",
|
|
|
|
|
file_url: null,
|
|
|
|
|
thumbnail_url: null,
|
|
|
|
|
bucket: null,
|
|
|
|
|
storage_path: null,
|
|
|
|
|
file_name: fileName,
|
|
|
|
|
file_size: null,
|
|
|
|
|
mime_type: "application/json",
|
|
|
|
|
ocr_payload: undefined,
|
|
|
|
|
ocr_strategy: null,
|
|
|
|
|
ocr_text: null,
|
|
|
|
|
ocr_status: null,
|
|
|
|
|
signed_url: null,
|
|
|
|
|
created_at: row.created_at ?? "",
|
|
|
|
|
updated_at: row.updated_at ?? "",
|
|
|
|
|
};
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
const trashedTableAssets: MediaAsset[] = (tables ?? [])
|
|
|
|
|
.filter((row) => Boolean(row.is_archived))
|
|
|
|
|
.map((row) => {
|
|
|
|
|
const base = String(row.title ?? "未命名表格").trim() || "未命名表格";
|
|
|
|
|
const fileName = base.toLowerCase().endsWith(".luckysheet") ? base : `${base}.luckysheet`;
|
|
|
|
|
return {
|
|
|
|
|
id: row.id,
|
|
|
|
|
workspace_id: row.workspace_id ?? targetWorkspaceId,
|
|
|
|
|
document_id: row.document_id,
|
|
|
|
|
asset_type: "luckysheet",
|
|
|
|
|
file_url: null,
|
|
|
|
|
thumbnail_url: null,
|
|
|
|
|
bucket: null,
|
|
|
|
|
storage_path: null,
|
|
|
|
|
file_name: fileName,
|
|
|
|
|
file_size: null,
|
|
|
|
|
mime_type: "application/json",
|
|
|
|
|
ocr_payload: undefined,
|
|
|
|
|
ocr_strategy: null,
|
|
|
|
|
ocr_text: null,
|
|
|
|
|
ocr_status: null,
|
|
|
|
|
deleted_at: row.deleted_at ?? row.updated_at ?? null,
|
|
|
|
|
deleted_by: row.deleted_by ?? null,
|
|
|
|
|
purged_at: row.purged_at ?? null,
|
|
|
|
|
signed_url: null,
|
|
|
|
|
created_at: row.created_at ?? "",
|
|
|
|
|
updated_at: row.updated_at ?? "",
|
|
|
|
|
};
|
|
|
|
|
});
|
|
|
|
|
|
2026-01-17 10:12:53 +08:00
|
|
|
const payload: SidebarInitialData = {
|
|
|
|
|
activeWorkspaceId: targetWorkspaceId,
|
|
|
|
|
workspaces,
|
|
|
|
|
documents,
|
|
|
|
|
trashedDocuments,
|
2026-01-20 07:24:12 +08:00
|
|
|
trashedMediaAssets: (trashedMediaAssets ?? []) as MediaAsset[],
|
2026-01-17 10:12:53 +08:00
|
|
|
trashedMindmapAssets,
|
2026-02-01 08:47:40 +08:00
|
|
|
trashedTableAssets,
|
2026-01-17 10:12:53 +08:00
|
|
|
mindmapDocs,
|
|
|
|
|
mindmapAssets,
|
|
|
|
|
mindmapAssetChildren,
|
2026-02-01 08:47:40 +08:00
|
|
|
tableAssets,
|
2026-01-20 07:24:12 +08:00
|
|
|
mediaAssets: (mediaAssets ?? []) as MediaAsset[],
|
2026-01-17 10:12:53 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
return NextResponse.json(payload);
|
|
|
|
|
} catch (error) {
|
|
|
|
|
return NextResponse.json(
|
|
|
|
|
{ error: error instanceof Error ? error.message : "拉取侧边栏数据失败" },
|
|
|
|
|
{ status: 500 },
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2026-01-18 19:01:31 +08:00
|
|
|
return NextResponse.json({ error: "当前仅支持 Convex 模式" }, { status: 501 });
|
|
|
|
|
|
|
|
|
|
/*
|
2025-11-23 10:55:04 +08:00
|
|
|
const supabase = await createSupabaseRouteClient();
|
|
|
|
|
const {
|
|
|
|
|
data: { session },
|
|
|
|
|
} = await supabase.auth.getSession();
|
2026-04-13 19:21:42 +08:00
|
|
|
|
|
|
|
|
if (!session) {
|
|
|
|
|
return NextResponse.json({ error: "未登录" }, { status: 401 });
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const url = new URL(request.url);
|
|
|
|
|
const workspaceIdParam = url.searchParams.get("workspaceId");
|
|
|
|
|
|
|
|
|
|
await ensureDefaultWorkspace(supabase, session.user.id, session.user.email ?? "我的空间");
|
|
|
|
|
const { workspaces, activeWorkspaceId } = await fetchWorkspaceSummaries(supabase, session.user.id);
|
|
|
|
|
const targetWorkspaceId = workspaceIdParam || activeWorkspaceId;
|
|
|
|
|
|
|
|
|
|
if (!targetWorkspaceId) {
|
|
|
|
|
return NextResponse.json({ error: "暂无可用工作空间" }, { status: 404 });
|
|
|
|
|
}
|
|
|
|
|
|
2025-11-23 10:55:04 +08:00
|
|
|
try {
|
2026-01-10 10:35:21 +08:00
|
|
|
const dataset = await fetchSidebarDataset(supabase, targetWorkspaceId);
|
2026-01-08 06:28:14 +08:00
|
|
|
const docIds = dataset.documents.map((d) => d.id);
|
|
|
|
|
const localMindmapFiles = await detectLocalMindmapFiles(docIds);
|
2026-01-11 12:35:53 +08:00
|
|
|
const mindmapAssetChildren =
|
|
|
|
|
await detectLocalMindmapImageAssetIdsByMindmapId(localMindmapFiles);
|
2026-01-08 06:28:14 +08:00
|
|
|
const mindmapDocs = Array.from(new Set([...(dataset.mindmapDocs ?? []), ...(await detectLocalMindmapDocs(docIds))]));
|
2026-01-10 10:35:21 +08:00
|
|
|
const trashedMindmapAssets = await detectLocalTrashedMindmapAssets(targetWorkspaceId, docIds);
|
2026-01-08 06:28:14 +08:00
|
|
|
const docById = new Map(dataset.documents.map((d) => [d.id, d]));
|
2026-01-10 10:35:21 +08:00
|
|
|
const mindmapAssets: MediaAsset[] = localMindmapFiles.map((item) => {
|
2026-01-08 06:28:14 +08:00
|
|
|
const doc = docById.get(item.documentId);
|
|
|
|
|
const workspaceId = doc?.workspace_id ?? targetWorkspaceId;
|
|
|
|
|
const fileUrlBase = item.source === "legacy" ? `/mindmaps/${item.documentId}` : `/documents/${item.documentId}`;
|
|
|
|
|
return {
|
|
|
|
|
id: item.mindmapId,
|
|
|
|
|
workspace_id: workspaceId,
|
|
|
|
|
document_id: item.documentId,
|
|
|
|
|
asset_type: "mindmap",
|
|
|
|
|
file_url: `${fileUrlBase}/${item.fileName}`,
|
|
|
|
|
thumbnail_url: null,
|
|
|
|
|
bucket: null,
|
|
|
|
|
storage_path: null,
|
|
|
|
|
file_name: item.fileName,
|
|
|
|
|
file_size: null,
|
|
|
|
|
mime_type: "application/json",
|
|
|
|
|
ocr_payload: undefined,
|
|
|
|
|
ocr_strategy: null,
|
|
|
|
|
ocr_text: null,
|
|
|
|
|
ocr_status: null,
|
|
|
|
|
signed_url: null,
|
|
|
|
|
created_at: "",
|
|
|
|
|
updated_at: "",
|
|
|
|
|
};
|
|
|
|
|
});
|
2025-11-23 10:55:04 +08:00
|
|
|
|
2026-01-10 10:35:21 +08:00
|
|
|
const tableAssets: MediaAsset[] = (dataset.tables ?? []).map((row) => {
|
|
|
|
|
const base = (row.title ?? "未命名表格").trim() || "未命名表格";
|
|
|
|
|
const fileName = base.toLowerCase().endsWith(".luckysheet") ? base : `${base}.luckysheet`;
|
|
|
|
|
return {
|
|
|
|
|
id: row.id,
|
|
|
|
|
workspace_id: row.workspace_id ?? targetWorkspaceId,
|
|
|
|
|
document_id: row.document_id,
|
|
|
|
|
asset_type: "luckysheet",
|
|
|
|
|
file_url: `/tables/${row.id}/view`,
|
|
|
|
|
thumbnail_url: null,
|
|
|
|
|
bucket: null,
|
|
|
|
|
storage_path: null,
|
|
|
|
|
file_name: fileName,
|
|
|
|
|
file_size: null,
|
|
|
|
|
mime_type: "application/json",
|
|
|
|
|
ocr_payload: undefined,
|
|
|
|
|
ocr_strategy: null,
|
|
|
|
|
ocr_text: null,
|
|
|
|
|
ocr_status: null,
|
|
|
|
|
signed_url: null,
|
|
|
|
|
created_at: row.created_at ?? "",
|
|
|
|
|
updated_at: row.updated_at ?? "",
|
|
|
|
|
};
|
|
|
|
|
});
|
|
|
|
|
|
2025-11-23 10:55:04 +08:00
|
|
|
const payload: SidebarInitialData = {
|
|
|
|
|
activeWorkspaceId: targetWorkspaceId,
|
|
|
|
|
workspaces,
|
|
|
|
|
documents: dataset.documents,
|
|
|
|
|
trashedDocuments: dataset.trashedDocuments,
|
2026-01-10 10:35:21 +08:00
|
|
|
trashedMediaAssets: dataset.trashedMediaAssets ?? [],
|
|
|
|
|
trashedMindmapAssets,
|
2026-01-02 07:25:50 +08:00
|
|
|
mindmapDocs,
|
2026-01-08 06:28:14 +08:00
|
|
|
mindmapAssets,
|
2026-01-11 12:35:53 +08:00
|
|
|
mindmapAssetChildren,
|
2026-01-10 10:35:21 +08:00
|
|
|
tableAssets,
|
2026-01-02 07:25:50 +08:00
|
|
|
mediaAssets: dataset.mediaAssets ?? [],
|
2025-11-23 10:55:04 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
return NextResponse.json(payload);
|
|
|
|
|
} catch (error) {
|
2026-04-13 19:21:42 +08:00
|
|
|
return NextResponse.json(
|
|
|
|
|
{ error: error instanceof Error ? error.message : "拉取侧边栏数据失败" },
|
|
|
|
|
{ status: 500 },
|
|
|
|
|
);
|
|
|
|
|
}
|
2026-01-18 19:01:31 +08:00
|
|
|
*/
|
2025-11-23 10:55:04 +08:00
|
|
|
}
|