Skip to content

Commit 46c2e16

Browse files
committed
feat(board/promoted_attributes): format number with precision
1 parent 3c42577 commit 46c2e16

File tree

1 file changed

+21
-3
lines changed

1 file changed

+21
-3
lines changed

apps/client/src/widgets/attribute_widgets/PromotedAttributesDisplay.tsx

Lines changed: 21 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,17 +3,19 @@ import FNote from "../../entities/fnote";
33
import "./PromotedAttributesDisplay.css";
44
import { useTriliumEvent } from "../react/hooks";
55
import attributes from "../../services/attributes";
6+
import { DefinitionObject } from "../../services/promoted_attribute_definition_parser";
67

78
interface PromotedAttributesDisplayProps {
89
note: FNote;
910
ignoredAttributes?: string[];
1011
}
1112

1213
interface AttributeWithDefinitions {
14+
friendlyName: string;
1315
name: string;
1416
type: string;
15-
friendlyName: string;
1617
value: string;
18+
def: DefinitionObject;
1719
}
1820

1921
export default function PromotedAttributesDisplay({ note, ignoredAttributes }: PromotedAttributesDisplayProps) {
@@ -23,7 +25,7 @@ export default function PromotedAttributesDisplay({ note, ignoredAttributes }: P
2325
{promotedDefinitionAttributes?.map((attr) => {
2426
return (
2527
<span key={attr.friendlyName} className="promoted-attribute">
26-
<strong>{attr.friendlyName}:</strong> {attr.value}
28+
<strong>{attr.friendlyName}:</strong> {formatLabelValue(attr)}
2729
</span>
2830
);
2931
}
@@ -45,6 +47,22 @@ function useNoteAttributesWithDefinitions(note: FNote, attributesToIgnore: stri
4547
return promotedDefinitionAttributes;
4648
}
4749

50+
function formatLabelValue(attr: AttributeWithDefinitions): string {
51+
let value = attr.value;
52+
switch (attr.def.labelType) {
53+
case "number":
54+
const numberValue = Number(value);
55+
if (attr.def.numberPrecision) {
56+
return numberValue.toFixed(attr.def.numberPrecision);
57+
} else {
58+
return numberValue.toString();
59+
}
60+
case "text":
61+
default:
62+
return value;
63+
}
64+
}
65+
4866
function getAttributesWithDefinitions(note: FNote, attributesToIgnore: string[] = []): AttributeWithDefinitions[] {
4967
const promotedDefinitionAttributes = note.getPromotedDefinitionAttributes();
5068
const result: AttributeWithDefinitions[] = [];
@@ -56,7 +74,7 @@ function getAttributesWithDefinitions(note: FNote, attributesToIgnore: string[]
5674
if (!value) continue;
5775
if (attributesToIgnore.includes(name)) continue;
5876

59-
result.push({ name, type, friendlyName, value });
77+
result.push({ def, name, type, value, friendlyName });
6078
}
6179
return result;
6280
}

0 commit comments

Comments
 (0)