2025-08-30 15:44:49 +03:00
|
|
|
import { allViewTypes, ViewModeProps, ViewTypeOptions } from "./interface";
|
2025-08-30 15:11:49 +03:00
|
|
|
import { useNoteContext, useNoteLabel, useTriliumEvent } from "../react/hooks";
|
|
|
|
|
import FNote from "../../entities/fnote";
|
|
|
|
|
import "./NoteList.css";
|
|
|
|
|
import ListView from "./legacy/ListView";
|
|
|
|
|
import { useEffect, useState } from "preact/hooks";
|
|
|
|
|
|
2025-08-30 14:29:54 +03:00
|
|
|
interface NoteListProps {
|
|
|
|
|
displayOnlyCollections?: boolean;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export default function NoteList({ }: NoteListProps) {
|
2025-08-30 15:11:49 +03:00
|
|
|
const { note } = useNoteContext();
|
|
|
|
|
const viewType = useNoteViewType(note);
|
|
|
|
|
const noteIds = useNoteIds(note, viewType);
|
2025-08-30 15:44:49 +03:00
|
|
|
const isEnabled = (note && !!viewType);
|
2025-08-30 15:11:49 +03:00
|
|
|
|
|
|
|
|
// Refresh note Ids
|
|
|
|
|
|
|
|
|
|
return (
|
|
|
|
|
<div className="note-list-widget">
|
|
|
|
|
{isEnabled && (
|
|
|
|
|
<div className="note-list-widget-content">
|
2025-08-30 15:44:49 +03:00
|
|
|
{getComponentByViewType(note, noteIds, viewType)}
|
2025-08-30 15:11:49 +03:00
|
|
|
</div>
|
|
|
|
|
)}
|
|
|
|
|
</div>
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
2025-08-30 15:44:49 +03:00
|
|
|
function getComponentByViewType(note: FNote, noteIds: string[], viewType: ViewTypeOptions) {
|
|
|
|
|
const props: ViewModeProps = { note, noteIds };
|
|
|
|
|
|
2025-08-30 15:11:49 +03:00
|
|
|
switch (viewType) {
|
|
|
|
|
case "list":
|
2025-08-30 15:44:49 +03:00
|
|
|
return <ListView {...props} />;
|
2025-08-30 15:11:49 +03:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function useNoteViewType(note?: FNote | null): ViewTypeOptions | undefined {
|
|
|
|
|
const [ viewType ] = useNoteLabel(note, "viewType");
|
|
|
|
|
|
|
|
|
|
if (!note) {
|
|
|
|
|
return undefined;
|
|
|
|
|
} else if (!(allViewTypes as readonly string[]).includes(viewType || "")) {
|
|
|
|
|
// when not explicitly set, decide based on the note type
|
|
|
|
|
return note.type === "search" ? "list" : "grid";
|
|
|
|
|
} else {
|
|
|
|
|
return viewType as ViewTypeOptions;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function useNoteIds(note: FNote | null | undefined, viewType: ViewTypeOptions | undefined) {
|
|
|
|
|
const [ noteIds, setNoteIds ] = useState<string[]>([]);
|
|
|
|
|
|
|
|
|
|
async function refreshNoteIds() {
|
|
|
|
|
if (!note) {
|
|
|
|
|
setNoteIds([]);
|
|
|
|
|
} else if (viewType === "list" || viewType === "grid") {
|
2025-08-30 15:44:49 +03:00
|
|
|
console.log("Refreshed note IDs");
|
2025-08-30 15:11:49 +03:00
|
|
|
setNoteIds(note.getChildNoteIds());
|
|
|
|
|
} else {
|
2025-08-30 15:44:49 +03:00
|
|
|
console.log("Refreshed note IDs");
|
2025-08-30 15:11:49 +03:00
|
|
|
setNoteIds(await note.getSubtreeNoteIds());
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Refresh on note switch.
|
|
|
|
|
useEffect(() => { refreshNoteIds() }, [ note ]);
|
|
|
|
|
|
|
|
|
|
// Refresh on alterations to the note subtree.
|
|
|
|
|
useTriliumEvent("entitiesReloaded", ({ loadResults }) => {
|
|
|
|
|
if (note && loadResults.getBranchRows().some(branch =>
|
|
|
|
|
branch.parentNoteId === note.noteId
|
|
|
|
|
|| noteIds.includes(branch.parentNoteId ?? ""))) {
|
|
|
|
|
refreshNoteIds();
|
|
|
|
|
}
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
return noteIds;
|
2025-08-30 14:29:54 +03:00
|
|
|
}
|