mirror of
https://github.com/zadam/trilium.git
synced 2025-11-11 08:58:58 +01:00
chore(react/type_widget): react to attachment changes
This commit is contained in:
parent
dc73467d34
commit
3171413a18
@ -10,23 +10,30 @@ import FAttachment from "../../entities/fattachment";
|
|||||||
import Alert from "../react/Alert";
|
import Alert from "../react/Alert";
|
||||||
import utils from "../../services/utils";
|
import utils from "../../services/utils";
|
||||||
import content_renderer from "../../services/content_renderer";
|
import content_renderer from "../../services/content_renderer";
|
||||||
|
import { useTriliumEvent } from "../react/hooks";
|
||||||
|
|
||||||
export function AttachmentList({ note }: TypeWidgetProps) {
|
export function AttachmentList({ note }: TypeWidgetProps) {
|
||||||
const [ attachments, setAttachments ] = useState<FAttachment[]>([]);
|
const [ attachments, setAttachments ] = useState<FAttachment[]>([]);
|
||||||
|
|
||||||
function refresh() {
|
function refresh() {
|
||||||
note.getAttachments().then(setAttachments);
|
note.getAttachments().then(attachments => setAttachments(Array.from(attachments)));
|
||||||
}
|
}
|
||||||
|
|
||||||
useEffect(refresh, [ note ]);
|
useEffect(refresh, [ note ]);
|
||||||
|
|
||||||
|
useTriliumEvent("entitiesReloaded", ({ loadResults }) => {
|
||||||
|
if (loadResults.getAttachmentRows().some((att) => att.attachmentId && att.ownerId === note.noteId)) {
|
||||||
|
refresh();
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<div className="attachment-list note-detail-printable">
|
<div className="attachment-list note-detail-printable">
|
||||||
<AttachmentListHeader noteId={note.noteId} />
|
<AttachmentListHeader noteId={note.noteId} />
|
||||||
|
|
||||||
<div className="attachment-list-wrapper">
|
<div className="attachment-list-wrapper">
|
||||||
{attachments.length ? (
|
{attachments.length ? (
|
||||||
attachments.map(attachment => <AttachmentDetail attachment={attachment} />)
|
attachments.map(attachment => <AttachmentDetail key={attachment.attachmentId} attachment={attachment} />)
|
||||||
) : (
|
) : (
|
||||||
<Alert type="info">
|
<Alert type="info">
|
||||||
{t("attachment_list.no_attachments")}
|
{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);
|
const contentWrapper = useRef<HTMLDivElement>(null);
|
||||||
|
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
|
|||||||
@ -42,13 +42,4 @@ export default class AttachmentListTypeWidget extends TypeWidget {
|
|||||||
this.$list.append(attachmentDetailWidget.render());
|
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();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user