chore(react/type_widget): react to attachment changes

This commit is contained in:
Elian Doran 2025-09-21 10:39:33 +03:00
parent dc73467d34
commit 3171413a18
No known key found for this signature in database
2 changed files with 10 additions and 12 deletions

View File

@ -10,23 +10,30 @@ import FAttachment from "../../entities/fattachment";
import Alert from "../react/Alert";
import utils from "../../services/utils";
import content_renderer from "../../services/content_renderer";
import { useTriliumEvent } from "../react/hooks";
export function AttachmentList({ note }: TypeWidgetProps) {
const [ attachments, setAttachments ] = useState<FAttachment[]>([]);
function refresh() {
note.getAttachments().then(setAttachments);
note.getAttachments().then(attachments => setAttachments(Array.from(attachments)));
}
useEffect(refresh, [ note ]);
useTriliumEvent("entitiesReloaded", ({ loadResults }) => {
if (loadResults.getAttachmentRows().some((att) => att.attachmentId && att.ownerId === note.noteId)) {
refresh();
}
});
return (
<div className="attachment-list note-detail-printable">
<AttachmentListHeader noteId={note.noteId} />
<div className="attachment-list-wrapper">
{attachments.length ? (
attachments.map(attachment => <AttachmentDetail attachment={attachment} />)
attachments.map(attachment => <AttachmentDetail key={attachment.attachmentId} attachment={attachment} />)
) : (
<Alert type="info">
{t("attachment_list.no_attachments")}
@ -62,7 +69,7 @@ function AttachmentListHeader({ noteId }: { noteId: string }) {
)
}
function AttachmentDetail({ attachment, isFullDetail }: { attachment: FAttachment, isFullDetail: boolean }) {
function AttachmentDetail({ attachment, isFullDetail }: { attachment: FAttachment, isFullDetail?: boolean }) {
const contentWrapper = useRef<HTMLDivElement>(null);
useEffect(() => {

View File

@ -42,13 +42,4 @@ export default class AttachmentListTypeWidget extends TypeWidget {
this.$list.append(attachmentDetailWidget.render());
}
}
async entitiesReloadedEvent({ loadResults }: EventData<"entitiesReloaded">) {
// updates and deletions are handled by the detail, for new attachments the whole list has to be refreshed
const attachmentsAdded = loadResults.getAttachmentRows().some((att) => att.attachmentId && !this.renderedAttachmentIds.has(att.attachmentId));
if (attachmentsAdded) {
this.refresh();
}
}
}