fix(promoted_attributes): value sometimes empty when reopening note

This commit is contained in:
Elian Doran 2025-12-04 10:38:07 +02:00
parent 7cc20600e7
commit 093d7d783b
No known key found for this signature in database

View File

@ -14,7 +14,6 @@ import ws from "../services/ws";
import { UpdateAttributeResponse } from "@triliumnext/commons"; import { UpdateAttributeResponse } from "@triliumnext/commons";
import attributes from "../services/attributes"; import attributes from "../services/attributes";
import debounce from "../services/debounce"; import debounce from "../services/debounce";
import { randomString } from "../services/utils";
interface Cell { interface Cell {
uniqueId: string; uniqueId: string;
@ -30,7 +29,7 @@ interface CellProps {
cell: Cell, cell: Cell,
cells: Cell[], cells: Cell[],
shouldFocus: boolean; shouldFocus: boolean;
setCells(cells: Cell[]): void; setCells: Dispatch<StateUpdater<Cell[] | undefined>>;
setCellToFocus(cell: Cell): void; setCellToFocus(cell: Cell): void;
} }
@ -98,7 +97,7 @@ function usePromotedAttributeData(note: FNote | null | undefined, componentId: s
valueAttrs = valueAttrs.slice(0, 1); valueAttrs = valueAttrs.slice(0, 1);
} }
for (const valueAttr of valueAttrs) { for (const [ i, valueAttr ] of valueAttrs.entries()) {
const definition = definitionAttr.getDefinition(); const definition = definitionAttr.getDefinition();
// if not owned, we'll force creation of a new attribute instead of updating the inherited one // if not owned, we'll force creation of a new attribute instead of updating the inherited one
@ -106,7 +105,7 @@ function usePromotedAttributeData(note: FNote | null | undefined, componentId: s
valueAttr.attributeId = ""; valueAttr.attributeId = "";
} }
const uniqueId = randomString(10); const uniqueId = `${note.noteId}-${valueAttr.name}-${i}`;
cells.push({ definitionAttr, definition, valueAttr, valueName, uniqueId }); cells.push({ definitionAttr, definition, valueAttr, valueName, uniqueId });
} }
} }
@ -292,8 +291,8 @@ function RelationInput({ inputId, ...props }: CellProps & { inputId: string }) {
id={inputId} id={inputId}
noteId={props.cell.valueAttr.value} noteId={props.cell.valueAttr.value}
noteIdChanged={async (value) => { noteIdChanged={async (value) => {
const { note, cell, componentId } = props; const { note, cell, componentId, setCells } = props;
cell.valueAttr.attributeId = (await updateAttribute(note, cell, componentId, value)).attributeId; await updateAttribute(note, cell, componentId, value, setCells);
}} }}
/> />
) )
@ -423,7 +422,7 @@ function setupTextLabelAutocomplete(el: HTMLInputElement, valueAttr: Attribute,
}); });
} }
function buildPromotedAttributeLabelChangedListener({ note, cell, componentId, ...props }: CellProps): OnChangeListener { function buildPromotedAttributeLabelChangedListener({ note, cell, componentId, setCells }: CellProps): OnChangeListener {
async function onChange(e: OnChangeEventData) { async function onChange(e: OnChangeEventData) {
const inputEl = e.target as HTMLInputElement; const inputEl = e.target as HTMLInputElement;
let value: string; let value: string;
@ -434,14 +433,14 @@ function buildPromotedAttributeLabelChangedListener({ note, cell, componentId, .
value = inputEl.value; value = inputEl.value;
} }
cell.valueAttr.attributeId = (await updateAttribute(note, cell, componentId, value)).attributeId; await updateAttribute(note, cell, componentId, value, setCells);
} }
return debounce(onChange, 250); return debounce(onChange, 250);
} }
function updateAttribute(note: FNote, cell: Cell, componentId: string, value: string | undefined) { async function updateAttribute(note: FNote, cell: Cell, componentId: string, value: string | undefined, setCells: Dispatch<StateUpdater<Cell[] | undefined>>) {
return server.put<UpdateAttributeResponse>( const { attributeId } = await server.put<UpdateAttributeResponse>(
`notes/${note.noteId}/attribute`, `notes/${note.noteId}/attribute`,
{ {
attributeId: cell.valueAttr.attributeId, attributeId: cell.valueAttr.attributeId,
@ -451,4 +450,15 @@ function updateAttribute(note: FNote, cell: Cell, componentId: string, value: st
}, },
componentId componentId
); );
setCells(prev =>
prev?.map(c =>
c.uniqueId === cell.uniqueId
? { ...c, valueAttr: {
...c.valueAttr,
attributeId,
value
} }
: c
)
);
} }