0.6 rust重构01
This commit is contained in:
@@ -0,0 +1,189 @@
|
||||
import type { SidebarInitialData } from "@/components/sidebar/types";
|
||||
import type { DocumentRecord } from "@/lib/documents";
|
||||
import type { WorkspaceSummary } from "@/lib/workspaces";
|
||||
import type { MediaAsset } from "@/types/media";
|
||||
|
||||
type MindmapRow = {
|
||||
mindmap_id: string;
|
||||
workspace_id?: string | null;
|
||||
document_id: string;
|
||||
data?: unknown;
|
||||
created_at?: string | null;
|
||||
updated_at?: string | null;
|
||||
deleted_at?: string | null;
|
||||
deleted_by?: string | null;
|
||||
};
|
||||
|
||||
type TableRow = {
|
||||
id: string;
|
||||
workspace_id?: string | null;
|
||||
document_id: string;
|
||||
title?: string | null;
|
||||
created_at?: string | null;
|
||||
updated_at?: string | null;
|
||||
deleted_at?: string | null;
|
||||
deleted_by?: string | null;
|
||||
purged_at?: string | null;
|
||||
is_archived?: boolean | null;
|
||||
};
|
||||
|
||||
type SidebarDatasetInput = {
|
||||
activeWorkspaceId: string;
|
||||
workspaces: WorkspaceSummary[];
|
||||
documents: DocumentRecord[];
|
||||
trashedDocuments: SidebarInitialData["trashedDocuments"];
|
||||
mindmaps: MindmapRow[];
|
||||
mediaAssets?: MediaAsset[] | null;
|
||||
trashedMediaAssets?: MediaAsset[] | null;
|
||||
tables?: TableRow[] | null;
|
||||
};
|
||||
|
||||
function normalizeStringArray(values: Iterable<string>): string[] {
|
||||
return Array.from(new Set(values)).filter((value) => value.trim().length > 0);
|
||||
}
|
||||
|
||||
export function extractMindmapImageAssetIdsFromData(input: unknown): string[] {
|
||||
const root = (() => {
|
||||
if (!input || typeof input !== "object") return input;
|
||||
const record = input as Record<string, unknown>;
|
||||
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 || 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");
|
||||
|
||||
push(get(data, "image"));
|
||||
push(image);
|
||||
push(get(image, "url"));
|
||||
push(get(get(data, "image"), "url"));
|
||||
|
||||
const children = get(node, "children");
|
||||
if (Array.isArray(children)) {
|
||||
children.forEach(walk);
|
||||
}
|
||||
};
|
||||
|
||||
walk(root);
|
||||
return ids;
|
||||
}
|
||||
|
||||
function toMindmapAsset(row: MindmapRow, workspaceId: string): MediaAsset {
|
||||
const isLegacy = row.mindmap_id.startsWith("legacy-");
|
||||
return {
|
||||
id: row.mindmap_id,
|
||||
workspace_id: row.workspace_id ?? workspaceId,
|
||||
document_id: row.document_id,
|
||||
asset_type: "mindmap",
|
||||
file_url: null,
|
||||
thumbnail_url: null,
|
||||
bucket: null,
|
||||
storage_path: null,
|
||||
file_name: isLegacy ? "mindmap.json" : `mindmap-${row.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: row.created_at ?? "",
|
||||
updated_at: row.updated_at ?? "",
|
||||
};
|
||||
}
|
||||
|
||||
function toTrashedMindmapAsset(row: MindmapRow, workspaceId: string): MediaAsset {
|
||||
return {
|
||||
...toMindmapAsset(row, workspaceId),
|
||||
deleted_at: row.deleted_at ?? null,
|
||||
deleted_by: row.deleted_by ?? null,
|
||||
purged_at: null,
|
||||
};
|
||||
}
|
||||
|
||||
function toTableAsset(row: TableRow, workspaceId: string): MediaAsset {
|
||||
const base = String(row.title ?? "未命名表格").trim() || "未命名表格";
|
||||
const fileName = base.toLowerCase().endsWith(".luckysheet") ? base : `${base}.luckysheet`;
|
||||
return {
|
||||
id: row.id,
|
||||
workspace_id: row.workspace_id ?? workspaceId,
|
||||
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 ?? "",
|
||||
};
|
||||
}
|
||||
|
||||
function toTrashedTableAsset(row: TableRow, workspaceId: string): MediaAsset {
|
||||
return {
|
||||
...toTableAsset(row, workspaceId),
|
||||
deleted_at: row.deleted_at ?? row.updated_at ?? null,
|
||||
deleted_by: row.deleted_by ?? null,
|
||||
purged_at: row.purged_at ?? null,
|
||||
};
|
||||
}
|
||||
|
||||
export function buildSidebarInitialData(input: SidebarDatasetInput): SidebarInitialData {
|
||||
const activeMindmaps = input.mindmaps.filter((row) => !row.deleted_at);
|
||||
const trashedMindmaps = input.mindmaps.filter((row) => Boolean(row.deleted_at));
|
||||
const activeTables = (input.tables ?? []).filter((row) => !row.is_archived);
|
||||
const trashedTables = (input.tables ?? []).filter((row) => Boolean(row.is_archived));
|
||||
|
||||
const mindmapAssetChildren: Record<string, string[]> = {};
|
||||
activeMindmaps.forEach((row) => {
|
||||
const ids = extractMindmapImageAssetIdsFromData(row.data);
|
||||
if (ids.length > 0) {
|
||||
mindmapAssetChildren[row.mindmap_id] = ids;
|
||||
}
|
||||
});
|
||||
|
||||
return {
|
||||
activeWorkspaceId: input.activeWorkspaceId,
|
||||
workspaces: input.workspaces,
|
||||
documents: input.documents,
|
||||
trashedDocuments: input.trashedDocuments,
|
||||
trashedMediaAssets: [...(input.trashedMediaAssets ?? [])],
|
||||
trashedMindmapAssets: trashedMindmaps.map((row) =>
|
||||
toTrashedMindmapAsset(row, input.activeWorkspaceId),
|
||||
),
|
||||
trashedTableAssets: trashedTables.map((row) =>
|
||||
toTrashedTableAsset(row, input.activeWorkspaceId),
|
||||
),
|
||||
mindmapDocs: normalizeStringArray(activeMindmaps.map((row) => row.document_id)),
|
||||
mindmapAssets: activeMindmaps.map((row) => toMindmapAsset(row, input.activeWorkspaceId)),
|
||||
mindmapAssetChildren,
|
||||
tableAssets: activeTables.map((row) => toTableAsset(row, input.activeWorkspaceId)),
|
||||
mediaAssets: [...(input.mediaAssets ?? [])],
|
||||
};
|
||||
}
|
||||
Reference in New Issue
Block a user