chore(client/ts): port similar_notes

This commit is contained in:
Elian Doran 2025-01-19 21:08:57 +02:00
parent 4492c60aa9
commit befdade7ba
No known key found for this signature in database
2 changed files with 32 additions and 6 deletions

View File

@ -36,6 +36,12 @@ interface DateLimits {
maxDate: string; maxDate: string;
} }
interface SimilarNote {
score: number;
notePath: string[];
noteId: string;
}
function filterUrlValue(value: string) { function filterUrlValue(value: string) {
return value return value
.replace(/https?:\/\//gi, "") .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); 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 = []; const results = [];
let i = 0; let i = 0;
@ -417,6 +423,7 @@ async function findSimilarNotes(noteId: string) {
// this takes care of note hoisting // this takes care of note hoisting
if (!notePath) { if (!notePath) {
// TODO: This return is suspicious, it should probably be continue
return; return;
} }

View File

@ -3,10 +3,12 @@ import linkService from "../../services/link.js";
import server from "../../services/server.js"; import server from "../../services/server.js";
import froca from "../../services/froca.js"; import froca from "../../services/froca.js";
import NoteContextAwareWidget from "../note_context_aware_widget.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 = ` const TPL = `
<div class="similar-notes-widget"> <div class="similar-notes-widget">
<style> <style>
.similar-notes-wrapper { .similar-notes-wrapper {
max-height: 200px; max-height: 200px;
overflow: auto; overflow: auto;
@ -31,7 +33,20 @@ const TPL = `
</div> </div>
`; `;
// TODO: Deduplicate with server
interface SimilarNote {
score: number;
notePath: string[];
noteId: string;
}
export default class SimilarNotesWidget extends NoteContextAwareWidget { export default class SimilarNotesWidget extends NoteContextAwareWidget {
private $similarNotesWrapper!: JQuery<HTMLElement>;
private title?: string;
private rendered?: boolean;
get name() { get name() {
return "similarNotes"; return "similarNotes";
} }
@ -41,7 +56,7 @@ export default class SimilarNotesWidget extends NoteContextAwareWidget {
} }
isEnabled() { isEnabled() {
return super.isEnabled() && this.note.type !== "search" && !this.note.isLabelTruthy("similarNotesWidgetDisabled"); return super.isEnabled() && this.note?.type !== "search" && !this.note?.isLabelTruthy("similarNotesWidgetDisabled");
} }
getTitle() { getTitle() {
@ -59,11 +74,15 @@ export default class SimilarNotesWidget extends NoteContextAwareWidget {
this.$similarNotesWrapper = this.$widget.find(".similar-notes-wrapper"); 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 // remember which title was when we found the similar notes
this.title = this.note.title; 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) { if (similarNotes.length === 0) {
this.$similarNotesWrapper.empty().append(t("similar_notes.no_similar_notes_found")); 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); this.$similarNotesWrapper.empty().append($list);
} }
entitiesReloadedEvent({ loadResults }) { entitiesReloadedEvent({ loadResults }: EventData<"entitiesReloaded">) {
if (this.note && this.title !== this.note.title) { if (this.note && this.title !== this.note.title) {
this.rendered = false; this.rendered = false;