feat(board/promoted_attributes): format number with precision

This commit is contained in:
Elian Doran 2025-11-12 20:31:27 +02:00
parent 3c42577da4
commit 46c2e162f0
No known key found for this signature in database

View File

@ -3,6 +3,7 @@ import FNote from "../../entities/fnote";
import "./PromotedAttributesDisplay.css"; import "./PromotedAttributesDisplay.css";
import { useTriliumEvent } from "../react/hooks"; import { useTriliumEvent } from "../react/hooks";
import attributes from "../../services/attributes"; import attributes from "../../services/attributes";
import { DefinitionObject } from "../../services/promoted_attribute_definition_parser";
interface PromotedAttributesDisplayProps { interface PromotedAttributesDisplayProps {
note: FNote; note: FNote;
@ -10,10 +11,11 @@ interface PromotedAttributesDisplayProps {
} }
interface AttributeWithDefinitions { interface AttributeWithDefinitions {
friendlyName: string;
name: string; name: string;
type: string; type: string;
friendlyName: string;
value: string; value: string;
def: DefinitionObject;
} }
export default function PromotedAttributesDisplay({ note, ignoredAttributes }: PromotedAttributesDisplayProps) { export default function PromotedAttributesDisplay({ note, ignoredAttributes }: PromotedAttributesDisplayProps) {
@ -23,7 +25,7 @@ export default function PromotedAttributesDisplay({ note, ignoredAttributes }: P
{promotedDefinitionAttributes?.map((attr) => { {promotedDefinitionAttributes?.map((attr) => {
return ( return (
<span key={attr.friendlyName} className="promoted-attribute"> <span key={attr.friendlyName} className="promoted-attribute">
<strong>{attr.friendlyName}:</strong> {attr.value} <strong>{attr.friendlyName}:</strong> {formatLabelValue(attr)}
</span> </span>
); );
} }
@ -45,6 +47,22 @@ function useNoteAttributesWithDefinitions(note: FNote, attributesToIgnore: stri
return promotedDefinitionAttributes; return promotedDefinitionAttributes;
} }
function formatLabelValue(attr: AttributeWithDefinitions): string {
let value = attr.value;
switch (attr.def.labelType) {
case "number":
const numberValue = Number(value);
if (attr.def.numberPrecision) {
return numberValue.toFixed(attr.def.numberPrecision);
} else {
return numberValue.toString();
}
case "text":
default:
return value;
}
}
function getAttributesWithDefinitions(note: FNote, attributesToIgnore: string[] = []): AttributeWithDefinitions[] { function getAttributesWithDefinitions(note: FNote, attributesToIgnore: string[] = []): AttributeWithDefinitions[] {
const promotedDefinitionAttributes = note.getPromotedDefinitionAttributes(); const promotedDefinitionAttributes = note.getPromotedDefinitionAttributes();
const result: AttributeWithDefinitions[] = []; const result: AttributeWithDefinitions[] = [];
@ -56,7 +74,7 @@ function getAttributesWithDefinitions(note: FNote, attributesToIgnore: string[]
if (!value) continue; if (!value) continue;
if (attributesToIgnore.includes(name)) continue; if (attributesToIgnore.includes(name)) continue;
result.push({ name, type, friendlyName, value }); result.push({ def, name, type, value, friendlyName });
} }
return result; return result;
} }