feat(board/promoted_attributes): react to changes

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

View File

@ -1,5 +1,8 @@
import { useState } from "preact/hooks";
import FNote from "../../entities/fnote"; import FNote from "../../entities/fnote";
import "./PromotedAttributesDisplay.css"; import "./PromotedAttributesDisplay.css";
import { useTriliumEvent } from "../react/hooks";
import attributes from "../../services/attributes";
interface PromotedAttributesDisplayProps { interface PromotedAttributesDisplayProps {
note: FNote; note: FNote;
@ -31,9 +34,20 @@ export default function PromotedAttributesDisplay({ note, ignoredAttributes }: P
} }
function useNoteAttributesWithDefinitions(note: FNote, attributesToIgnore: string[] = []): AttributeWithDefinitions[] { function useNoteAttributesWithDefinitions(note: FNote, attributesToIgnore: string[] = []): AttributeWithDefinitions[] {
const [ promotedDefinitionAttributes, setPromotedDefinitionAttributes ] = useState<AttributeWithDefinitions[]>(getAttributesWithDefinitions(note, attributesToIgnore));
useTriliumEvent("entitiesReloaded", ({ loadResults }) => {
if (loadResults.getAttributeRows().some(attr => attributes.isAffecting(attr, note))) {
setPromotedDefinitionAttributes(getAttributesWithDefinitions(note, attributesToIgnore));
}
});
return promotedDefinitionAttributes;
}
function getAttributesWithDefinitions(note: FNote, attributesToIgnore: string[] = []): AttributeWithDefinitions[] {
const promotedDefinitionAttributes = note.getPromotedDefinitionAttributes(); const promotedDefinitionAttributes = note.getPromotedDefinitionAttributes();
const result: AttributeWithDefinitions[] = []; const result: AttributeWithDefinitions[] = [];
for (const attr of promotedDefinitionAttributes) { for (const attr of promotedDefinitionAttributes) {
const def = attr.getDefinition(); const def = attr.getDefinition();
const [ type, name ] = attr.name.split(":", 2); const [ type, name ] = attr.name.split(":", 2);