Skip to content

Commit b13c0fe

Browse files
committed
refactor(board/promoted_attributes): reduce duplication
1 parent 3036d18 commit b13c0fe

File tree

1 file changed

+44
-30
lines changed

1 file changed

+44
-30
lines changed

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

Lines changed: 44 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import { useTriliumEvent } from "../react/hooks";
55
import attributes from "../../services/attributes";
66
import { DefinitionObject } from "../../services/promoted_attribute_definition_parser";
77
import { formatDateTime } from "../../utils/formatters";
8-
import { ComponentChildren, CSSProperties } from "preact";
8+
import { ComponentChild, ComponentChildren, CSSProperties } from "preact";
99
import Icon from "../react/Icon";
1010
import NoteLink from "../react/NoteLink";
1111
import { getReadableTextColor } from "../../services/css_class_manager";
@@ -56,39 +56,53 @@ function PromotedAttribute({ attr, children, style }: { attr: AttributeWithDefin
5656
}
5757

5858
function buildPromotedAttribute(attr: AttributeWithDefinitions): ComponentChildren {
59-
if (attr.type === "relation") {
60-
return <PromotedAttribute attr={attr}><strong>{attr.friendlyName}:</strong> <NoteLink notePath={attr.value} showNoteIcon /></PromotedAttribute>
61-
}
59+
const defaultLabel = <><strong>{attr.friendlyName}:</strong>{" "}</>;
60+
let content: ComponentChildren;
61+
let style: CSSProperties | undefined;
6262

63-
let value = attr.value;
64-
switch (attr.def.labelType) {
65-
case "number":
66-
let formattedValue = value;
67-
const numberValue = Number(value);
68-
if (attr.def.numberPrecision) {
69-
formattedValue = numberValue.toFixed(attr.def.numberPrecision);
63+
if (attr.type === "label") {
64+
let value = attr.value;
65+
switch (attr.def.labelType) {
66+
case "number":
67+
let formattedValue = value;
68+
const numberValue = Number(value);
69+
if (!Number.isNaN(numberValue) && attr.def.numberPrecision) formattedValue = numberValue.toFixed(attr.def.numberPrecision);
70+
content = <>{defaultLabel}{formattedValue}</>;
71+
break;
72+
case "date":
73+
case "datetime": {
74+
const date = new Date(value);
75+
const timeFormat = attr.def.labelType !== "date" ? "short" : "none";
76+
const formattedValue = formatDateTime(date, "short", timeFormat);
77+
content = <>{defaultLabel}{formattedValue}</>;
78+
break;
7079
}
71-
return <PromotedAttribute attr={attr}><strong>{attr.friendlyName}:</strong> {formattedValue}</PromotedAttribute>;
72-
case "date":
73-
case "datetime": {
74-
const date = new Date(value);
75-
const timeFormat = attr.def.labelType !== "date" ? "short" : "none";
76-
return <PromotedAttribute attr={attr}><strong>{attr.friendlyName}:</strong> {formatDateTime(date, "short", timeFormat)}</PromotedAttribute>;
77-
}
78-
case "time": {
79-
const date = new Date(`1970-01-01T${value}Z`);
80-
return <PromotedAttribute attr={attr}><strong>{attr.friendlyName}:</strong> {formatDateTime(date, "none", "short")}</PromotedAttribute>;
80+
case "time": {
81+
const date = new Date(`1970-01-01T${value}Z`);
82+
const formattedValue = formatDateTime(date, "none", "short");
83+
content = <>{defaultLabel}{formattedValue}</>;
84+
break;
85+
}
86+
case "boolean":
87+
content = <><Icon icon={value === "true" ? "bx bx-check-square" : "bx bx-square"} />{" "}<strong>{attr.friendlyName}</strong></>;
88+
break;
89+
case "url":
90+
content = <a href={value} target="_blank" rel="noopener noreferrer">{attr.friendlyName}</a>;
91+
break;
92+
case "color":
93+
style = { backgroundColor: value, color: getReadableTextColor(value) };
94+
content = <>{attr.friendlyName}</>;
95+
break;
96+
case "text":
97+
default:
98+
content = <>{defaultLabel}{value}</>;
99+
break;
81100
}
82-
case "boolean":
83-
return <PromotedAttribute attr={attr}><Icon icon={value === "true" ? "bx bx-check-square" : "bx bx-square"} /> <strong>{attr.friendlyName}</strong></PromotedAttribute>;
84-
case "url":
85-
return <PromotedAttribute attr={attr}><a href={value} target="_blank" rel="noopener noreferrer">{attr.friendlyName}</a></PromotedAttribute>;
86-
case "color":
87-
return <PromotedAttribute attr={attr} style={{ backgroundColor: value, color: getReadableTextColor(value) }}>{attr.friendlyName}</PromotedAttribute>;
88-
case "text":
89-
default:
90-
return <PromotedAttribute attr={attr}><strong>{attr.friendlyName}:</strong> {value}</PromotedAttribute>;
101+
} else if (attr.type === "relation") {
102+
content = <>{defaultLabel}<NoteLink notePath={attr.value} showNoteIcon /></>;
91103
}
104+
105+
return <PromotedAttribute attr={attr} style={style}>{content}</PromotedAttribute>
92106
}
93107

94108
function getAttributesWithDefinitions(note: FNote, attributesToIgnore: string[] = []): AttributeWithDefinitions[] {

0 commit comments

Comments
 (0)