Canna71 / obsidian-chronology

Other
99 stars 7 forks source link

Duplicity in the list view (notes are sometimes showed twice) #36

Open jankoweb opened 2 months ago

jankoweb commented 2 months ago

Why are some notes duplicated in a list view? How can I deal with it?

image

My chronology setting:

image
mbw8421 commented 1 month ago

It looks like the function that gets the notes is built with the default calendar view in mind so when passed to the NotesList component there can be duplicates due to different timestamps. As the notes look to already be sorted by most recent you could filter the NotesList items array within the component to ensure only the first occurrence of each unique file is rendered. Something like the below.

import { PaneType, TFile } from "obsidian";
import * as React from "react";
import { CalendarItem } from "../CalendarType";
import { NoteAttributes } from "../TimeIndex";
import { NoteView } from "./NoteView";

export const NotesList = ({ calItem, items, onOpen }: {
    calItem: CalendarItem,
    items: NoteAttributes[],
    onOpen: (note: TFile, paneType: PaneType | boolean) => void
}) => {

    // Filter to get unique items based on note.path
    const uniqueItems = items.filter((item, index, self) =>
        index === self.findIndex(t => t.note.path === item.note.path)
    );

    return (
        <div className="chronology-noteslist-container">
            <div className="chronology-noteslist-wrapper">
                {uniqueItems.map(item =>
                    <NoteView key={item.note.path + item.attribute} item={item} onOpen={onOpen} extraInfo={false} />
                )}
            </div>
        </div>
    );
}

export default NotesList;

P.S. I am not a JS dev so this method might be horribly inefficient, looks like O(n^2) due to the nested loop.