refactor(book/table): extract gathering definitions

This commit is contained in:
Elian Doran
2025-06-25 12:03:17 +03:00
parent d9443527ee
commit b6398fdb5d

View File

@@ -6,11 +6,18 @@ type Data = {
title: string; title: string;
} & Record<string, string>; } & Record<string, string>;
interface PromotedAttributeInformation {
name: string;
title?: string;
type?: LabelType;
}
type GridLabelType = 'text' | 'number' | 'boolean' | 'date' | 'dateString' | 'object'; type GridLabelType = 'text' | 'number' | 'boolean' | 'date' | 'dateString' | 'object';
export function buildData(parentNote: FNote, notes: FNote[]) { export function buildData(parentNote: FNote, notes: FNote[]) {
const { columnDefs, expectedLabels } = buildColumnDefinitions(parentNote); const info = getPromotedAttributeInformation(parentNote);
const rowData = buildRowDefinitions(notes, expectedLabels); const columnDefs = buildColumnDefinitions(parentNote, info);
const rowData = buildRowDefinitions(notes, info);
return { return {
rowData, rowData,
@@ -18,15 +25,26 @@ export function buildData(parentNote: FNote, notes: FNote[]) {
} }
} }
export function buildColumnDefinitions(parentNote: FNote) { export function buildColumnDefinitions(parentNote: FNote, info: PromotedAttributeInformation[]) {
const columnDefs: GridOptions<Data>["columnDefs"] = [ const columnDefs: GridOptions<Data>["columnDefs"] = [
{ {
field: "title" field: "title"
} }
]; ];
const expectedLabels: string[] = []; for (const { name, title, type } of info) {
columnDefs.push({
field: name,
headerName: title,
cellDataType: mapDataType(type)
});
}
return columnDefs;
}
function getPromotedAttributeInformation(parentNote: FNote) {
const info: PromotedAttributeInformation[] = [];
for (const promotedAttribute of parentNote.getPromotedDefinitionAttributes()) { for (const promotedAttribute of parentNote.getPromotedDefinitionAttributes()) {
console.log(promotedAttribute); console.log(promotedAttribute);
if (promotedAttribute.type !== "label") { if (promotedAttribute.type !== "label") {
@@ -40,18 +58,13 @@ export function buildColumnDefinitions(parentNote: FNote) {
continue; continue;
} }
const attributeName = promotedAttribute.name.split(":", 2)[1]; info.push({
const title = def.promotedAlias ?? attributeName; name: promotedAttribute.name.split(":", 2)[1],
title: def.promotedAlias,
columnDefs.push({ type: def.labelType
field: attributeName, })
headerName: title,
cellDataType: mapDataType(def.labelType)
});
expectedLabels.push(attributeName);
} }
return info;
return { columnDefs, expectedLabels };
} }
function mapDataType(labelType: LabelType | undefined): GridLabelType { function mapDataType(labelType: LabelType | undefined): GridLabelType {
@@ -60,6 +73,10 @@ function mapDataType(labelType: LabelType | undefined): GridLabelType {
} }
switch (labelType) { switch (labelType) {
case "number":
return "number";
case "boolean":
return "boolean";
case "date": case "date":
return "dateString"; return "dateString";
case "text": case "text":
@@ -68,15 +85,15 @@ function mapDataType(labelType: LabelType | undefined): GridLabelType {
} }
} }
export function buildRowDefinitions(notes: FNote[], expectedLabels: string[]): GridOptions<Data>["rowData"] { export function buildRowDefinitions(notes: FNote[], infos: PromotedAttributeInformation[]): GridOptions<Data>["rowData"] {
const definitions: GridOptions<Data>["rowData"] = []; const definitions: GridOptions<Data>["rowData"] = [];
for (const note of notes) { for (const note of notes) {
const data = { const data = {
title: note.title title: note.title
}; };
for (const expectedLabel of expectedLabels) { for (const info of infos) {
data[expectedLabel] = note.getLabelValue(expectedLabel); data[info.name] = note.getLabelValue(info.name);
} }
definitions.push(data); definitions.push(data);