Skip to content

Build your personal knowledge base with Trilium Notes

refactor(react/dialogs): solve some type errors

Elian Doranedd18b53d067
diff --git a/apps/client/src/widgets/dialogs/revisions.tsx b/apps/client/src/widgets/dialogs/revisions.tsx
index d66a04fda5..a906743ad6 100644
--- a/apps/client/src/widgets/dialogs/revisions.tsx
+++ b/apps/client/src/widgets/dialogs/revisions.tsx
@@ -1,4 +1,4 @@
-import type { FullRevision, RevisionItem } from "@triliumnext/commons";
+import type { RevisionPojo, RevisionItem } from "@triliumnext/commons";
import appContext, { EventData } from "../../components/app_context";
import FNote from "../../entities/fnote";
import dialog, { closeActiveDialog, openDialog } from "../../services/dialog";
@@ -91,20 +91,20 @@ function RevisionsList({ revisions, onSelect }: { revisions: RevisionItem[], onS
title={t("revisions.revision_last_edited", { date: item.dateLastEdited })}
value={item.revisionId}
>
- {item.dateLastEdited.substr(0, 16)} ({utils.formatSize(item.contentLength)})
+ {item.dateLastEdited && item.dateLastEdited.substr(0, 16)} ({item.contentLength && utils.formatSize(item.contentLength)})
</FormListItem>
)}
</FormList>);
}
function RevisionPreview({ revisionItem }: { revisionItem?: RevisionItem}) {
- const [ fullRevision, setFullRevision ] = useState<FullRevision>();
+ const [ fullRevision, setFullRevision ] = useState<RevisionPojo>();
const [ needsRefresh, setNeedsRefresh ] = useState<boolean>();
useEffect(() => {
setNeedsRefresh(false);
if (revisionItem) {
- server.get<FullRevision>(`revisions/${revisionItem.revisionId}`).then(setFullRevision);
+ server.get<RevisionPojo>(`revisions/${revisionItem.revisionId}`).then(setFullRevision);
} else {
setFullRevision(undefined);
}
@@ -143,7 +143,11 @@ function RevisionPreview({ revisionItem }: { revisionItem?: RevisionItem}) {
primary
icon="bx bx-download"
text={t("revisions.download_button")}
- onClick={() => open.downloadRevision(revisionItem.noteId, revisionItem.revisionId)} />
+ onClick={() => {
+ if (revisionItem.revisionId) {
+ open.downloadRevision(revisionItem.noteId, revisionItem.revisionId)}
+ }
+ }/>
</>
}
</div>)}
@@ -167,12 +171,12 @@ const CODE_STYLE: CSSProperties = {
whiteSpace: "pre-wrap"
};
-function RevisionContent({ revisionItem, fullRevision }: { revisionItem?: RevisionItem, fullRevision?: FullRevision }) {
- if (!revisionItem || !fullRevision) {
+function RevisionContent({ revisionItem, fullRevision }: { revisionItem?: RevisionItem, fullRevision?: RevisionPojo }) {
+ const content = fullRevision?.content;
+ if (!revisionItem || !content) {
return <></>;
}
- const content = fullRevision.content;
switch (revisionItem.type) {
case "text": {
@@ -182,7 +186,7 @@ function RevisionContent({ revisionItem, fullRevision }: { revisionItem?: Revisi
renderMathInElement(contentRef.current, { trust: true });
}
});
- return <div ref={contentRef} className="ck-content" dangerouslySetInnerHTML={{ __html: content }}></div>
+ return <div ref={contentRef} className="ck-content" dangerouslySetInnerHTML={{ __html: content as string }}></div>
}
case "code":
return <pre style={CODE_STYLE}>{content}</pre>;
@@ -190,7 +194,7 @@ function RevisionContent({ revisionItem, fullRevision }: { revisionItem?: Revisi
switch (revisionItem.mime) {
case "image/svg+xml": {
//Base64 of other format images may be embedded in svg
- const encodedSVG = encodeURIComponent(content);
+ const encodedSVG = encodeURIComponent(content as string);
return <img
src={`data:${fullRevision.mime};utf8,${encodedSVG}`}
style={IMAGE_STYLE} />;
@@ -211,7 +215,7 @@ function RevisionContent({ revisionItem, fullRevision }: { revisionItem?: Revisi
</tr>
<tr>
<th>{t("revisions.file_size")}</th>
- <td>{utils.formatSize(revisionItem.contentLength)}</td>
+ <td>{revisionItem.contentLength && utils.formatSize(revisionItem.contentLength)}</td>
</tr>
{fullRevision.content &&
<tr>
diff --git a/apps/server/src/becca/entities/brevision.ts b/apps/server/src/becca/entities/brevision.ts
index 83767a3b2b..88f647db29 100644
--- a/apps/server/src/becca/entities/brevision.ts
+++ b/apps/server/src/becca/entities/brevision.ts
@@ -7,7 +7,7 @@ import becca from "../becca.js";
import AbstractBeccaEntity from "./abstract_becca_entity.js";
import sql from "../../services/sql.js";
import BAttachment from "./battachment.js";
-import type { AttachmentRow, NoteType, RevisionRow } from "@triliumnext/commons";
+import type { AttachmentRow, NoteType, RevisionPojo, RevisionRow } from "@triliumnext/commons";
import eraseService from "../../services/erase.js";
interface ContentOpts {
@@ -201,7 +201,7 @@ class BRevision extends AbstractBeccaEntity<BRevision> {
utcDateModified: this.utcDateModified,
content: this.content, // used when retrieving full note revision to frontend
contentLength: this.contentLength
- };
+ } satisfies RevisionPojo;
}
override getPojoToSave() {
diff --git a/apps/server/src/routes/api/revisions.ts b/apps/server/src/routes/api/revisions.ts
index f1edb3f135..cdcabe6d28 100644
--- a/apps/server/src/routes/api/revisions.ts
+++ b/apps/server/src/routes/api/revisions.ts
@@ -12,7 +12,7 @@ import type { Request, Response } from "express";
import type BRevision from "../../becca/entities/brevision.js";
import type BNote from "../../becca/entities/bnote.js";
import type { NotePojo } from "../../becca/becca-interface.js";
-import { RevisionItem, RevisionRow } from "@triliumnext/commons";
+import { RevisionItem, RevisionPojo, RevisionRow } from "@triliumnext/commons";
interface NotePath {
noteId: string;
@@ -60,7 +60,7 @@ function getRevision(req: Request) {
}
}
- return revision satisfies RevisionRow;
+ return revision satisfies RevisionPojo;
}
function getRevisionFilename(revision: BRevision) {
diff --git a/packages/commons/src/lib/server_api.ts b/packages/commons/src/lib/server_api.ts
index 81f45574a3..dfb5b611d7 100644
--- a/packages/commons/src/lib/server_api.ts
+++ b/packages/commons/src/lib/server_api.ts
@@ -28,3 +28,20 @@ export interface RevisionItem {
isProtected?: boolean;
mime: string;
}
+
+export interface RevisionPojo {
+ revisionId?: string;
+ noteId: string;
+ type: NoteType;
+ mime: string;
+ isProtected?: boolean;
+ title: string;
+ blobId?: string;
+ dateLastEdited?: string;
+ dateCreated?: string;
+ utcDateLastEdited?: string;
+ utcDateCreated?: string;
+ utcDateModified?: string;
+ content?: string | Buffer<ArrayBufferLike>;
+ contentLength?: number;
+}