Skip to content

Commit b11b3ff

Browse files
committed
refactor(board): use hook for obtaining the list of attributes
1 parent e006afc commit b11b3ff

File tree

1 file changed

+29
-12
lines changed

1 file changed

+29
-12
lines changed

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

Lines changed: 29 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -6,22 +6,23 @@ interface PromotedAttributesDisplayProps {
66
ignoredAttributes?: string[];
77
}
88

9-
export default function PromotedAttributesDisplay({ note, ignoredAttributes }: PromotedAttributesDisplayProps) {
10-
const promotedDefinitionAttributes = note.getPromotedDefinitionAttributes();
9+
interface AttributeWithDefinitions {
10+
name: string;
11+
type: string;
12+
friendlyName: string;
13+
value: string;
14+
}
1115

12-
return promotedDefinitionAttributes.length > 0 && (
16+
export default function PromotedAttributesDisplay({ note, ignoredAttributes }: PromotedAttributesDisplayProps) {
17+
const promotedDefinitionAttributes = useNoteAttributesWithDefinitions(note);
18+
return promotedDefinitionAttributes?.length > 0 && (
1319
<div className="promoted-attributes">
14-
{promotedDefinitionAttributes.map((attr) => {
15-
const def = attr.getDefinition();
16-
const [ type, name ] = attr.name.split(":", 2);
17-
const value = note.getLabelValue(name);
18-
const friendlyName = def?.promotedAlias ?? name;
19-
if (!value) return null;
20-
if (ignoredAttributes && ignoredAttributes.includes(name)) return null;
20+
{promotedDefinitionAttributes?.map((attr) => {
21+
if (ignoredAttributes && ignoredAttributes.includes(attr.name)) return null;
2122

2223
return (
23-
<span key={attr.name} className="promoted-attribute">
24-
<strong>{friendlyName}:</strong> {value}
24+
<span key={attr.friendlyName} className="promoted-attribute">
25+
<strong>{attr.friendlyName}:</strong> {attr.value}
2526
</span>
2627
);
2728
}
@@ -30,3 +31,19 @@ export default function PromotedAttributesDisplay({ note, ignoredAttributes }: P
3031
)
3132

3233
}
34+
35+
function useNoteAttributesWithDefinitions(note: FNote) {
36+
const promotedDefinitionAttributes = note.getPromotedDefinitionAttributes();
37+
const result: AttributeWithDefinitions[] = [];
38+
39+
for (const attr of promotedDefinitionAttributes) {
40+
const def = attr.getDefinition();
41+
const [ type, name ] = attr.name.split(":", 2);
42+
const value = note.getLabelValue(name);
43+
const friendlyName = def?.promotedAlias ?? name;
44+
if (!value) continue;
45+
46+
result.push({ name, type, friendlyName, value });
47+
}
48+
return result;
49+
}

0 commit comments

Comments
 (0)