Skip to content

Build your personal knowledge base with Trilium Notes

chore(client/ts): port similar_notes

Elian Doranbefdade7ba72
diff --git a/src/becca/similarity.ts b/src/becca/similarity.ts
index 66af0ac61b..5b031df35c 100644
--- a/src/becca/similarity.ts
+++ b/src/becca/similarity.ts
@@ -36,6 +36,12 @@ interface DateLimits {
maxDate: string;
}
+interface SimilarNote {
+ score: number;
+ notePath: string[];
+ noteId: string;
+}
+
function filterUrlValue(value: string) {
return value
.replace(/https?:\/\//gi, "")
@@ -247,7 +253,7 @@ function hasConnectingRelation(sourceNote: BNote, targetNote: BNote) {
return sourceNote.getAttributes().find((attr) => attr.type === "relation" && ["includenotelink", "imagelink"].includes(attr.name) && attr.value === targetNote.noteId);
}
-async function findSimilarNotes(noteId: string) {
+async function findSimilarNotes(noteId: string): Promise<SimilarNote[] | undefined> {
const results = [];
let i = 0;
@@ -417,6 +423,7 @@ async function findSimilarNotes(noteId: string) {
// this takes care of note hoisting
if (!notePath) {
+ // TODO: This return is suspicious, it should probably be continue
return;
}
diff --git a/src/public/app/widgets/ribbon_widgets/similar_notes.js b/src/public/app/widgets/ribbon_widgets/similar_notes.ts
similarity index 76%
rename from src/public/app/widgets/ribbon_widgets/similar_notes.js
rename to src/public/app/widgets/ribbon_widgets/similar_notes.ts
index 7603a75183..f169b559d5 100644
--- a/src/public/app/widgets/ribbon_widgets/similar_notes.js
+++ b/src/public/app/widgets/ribbon_widgets/similar_notes.ts
@@ -3,10 +3,12 @@ import linkService from "../../services/link.js";
import server from "../../services/server.js";
import froca from "../../services/froca.js";
import NoteContextAwareWidget from "../note_context_aware_widget.js";
+import type FNote from "../../entities/fnote.js";
+import type { EventData } from "../../components/app_context.js";
const TPL = `
<div class="similar-notes-widget">
- <style>
+ <style>
.similar-notes-wrapper {
max-height: 200px;
overflow: auto;
@@ -31,7 +33,20 @@ const TPL = `
</div>
`;
+// TODO: Deduplicate with server
+interface SimilarNote {
+ score: number;
+ notePath: string[];
+ noteId: string;
+}
+
+
export default class SimilarNotesWidget extends NoteContextAwareWidget {
+
+ private $similarNotesWrapper!: JQuery<HTMLElement>;
+ private title?: string;
+ private rendered?: boolean;
+
get name() {
return "similarNotes";
}
@@ -41,7 +56,7 @@ export default class SimilarNotesWidget extends NoteContextAwareWidget {
}
isEnabled() {
- return super.isEnabled() && this.note.type !== "search" && !this.note.isLabelTruthy("similarNotesWidgetDisabled");
+ return super.isEnabled() && this.note?.type !== "search" && !this.note?.isLabelTruthy("similarNotesWidgetDisabled");
}
getTitle() {
@@ -59,11 +74,15 @@ export default class SimilarNotesWidget extends NoteContextAwareWidget {
this.$similarNotesWrapper = this.$widget.find(".similar-notes-wrapper");
}
- async refreshWithNote(note) {
+ async refreshWithNote(note: FNote) {
+ if (!this.note) {
+ return;
+ }
+
// remember which title was when we found the similar notes
this.title = this.note.title;
- const similarNotes = await server.get(`similar-notes/${this.noteId}`);
+ const similarNotes = await server.get<SimilarNote[]>(`similar-notes/${this.noteId}`);
if (similarNotes.length === 0) {
this.$similarNotesWrapper.empty().append(t("similar_notes.no_similar_notes_found"));
@@ -92,7 +111,7 @@ export default class SimilarNotesWidget extends NoteContextAwareWidget {
this.$similarNotesWrapper.empty().append($list);
}
- entitiesReloadedEvent({ loadResults }) {
+ entitiesReloadedEvent({ loadResults }: EventData<"entitiesReloaded">) {
if (this.note && this.title !== this.note.title) {
this.rendered = false;