Skip to content

Commit

Permalink
add: pagination
Browse files Browse the repository at this point in the history
  • Loading branch information
AlexW00 committed Mar 8, 2024
1 parent db73482 commit cb53ec2
Show file tree
Hide file tree
Showing 5 changed files with 45 additions and 8 deletions.
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "obisidian-note-linker"
version = "1.1.4"
version = "1.2.4"
authors = ["Alexander Weichart"]
edition = "2018"

Expand Down
2 changes: 1 addition & 1 deletion manifest.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"id": "obisidian-note-linker",
"name": "Note Linker",
"version": "1.1.4",
"version": "1.2.4",
"description": "Automatically find and link notes in Obsidian",
"author": "Alexander Weichart",
"authorUrl": "https://github.com/AlexW00",
Expand Down
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
{
"name": "obisidian-note-linker",
"version": "1.1.4",
"version": "1.2.4",
"description": "Automatically find and link notes in Obsidian",
"main": "main.js",
"scripts": {
"build": "wasm-pack build --target web && rollup --config rollup.config.js",
"build:copy": "npm run build && ./copy_to_vault.sh /home/aw/Dropbox/Documents/Other/Obsidian/YouTube"
"build:copy": "npm run build && ./copy_to_vault.sh /home/aw/Documents/Obsidian/obs"
},
"keywords": [],
"author": "",
Expand Down
39 changes: 35 additions & 4 deletions src/ts/components/lists/LinkFinderResultsListComponent.tsx
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
import * as React from "react";
import { LinkFinderResult, Note, NoteChangeOperation } from "../../../../pkg";
import { LinkMatchesListComponent } from "./LinkMatchesListComponent";
import { LinkFinderResultContext } from "../../context";
import { useCallback } from "react";
import { LinkMatchesListComponent } from "./LinkMatchesListComponent";

interface LinkFinderResultsListProps {
linkFinderResults: Array<LinkFinderResult>;
Expand All @@ -19,6 +18,16 @@ export const LinkFinderResultsList = ({
noteChangeOperations,
setNoteChangeOperations,
}: LinkFinderResultsListProps) => {
const [currentPage, setCurrentPage] = React.useState(0);
const itemsPerPage = 30;

const totalPages = Math.ceil(linkFinderResults.length / itemsPerPage);

const currentItems = linkFinderResults.slice(
currentPage * itemsPerPage,
(currentPage + 1) * itemsPerPage
);

const findNoteChangeOperation = (
note: Note
): NoteChangeOperation | undefined => {
Expand All @@ -29,7 +38,7 @@ export const LinkFinderResultsList = ({
return findNoteChangeOperation(note)?.replacements ?? [];
};

const totalReplacements = useCallback(() => {
const totalReplacements = React.useCallback(() => {
let total = 0;
noteChangeOperations?.forEach(
(noteChangeOperation: NoteChangeOperation) => {
Expand All @@ -39,12 +48,20 @@ export const LinkFinderResultsList = ({
return total;
}, [noteChangeOperations]);

const handlePrevPage = () => {
setCurrentPage((prevPage) => Math.max(prevPage - 1, 0));
};

const handleNextPage = () => {
setCurrentPage((prevPage) => Math.min(prevPage + 1, totalPages - 1));
};

if (linkFinderResults.length !== 0)
return (
<div className="note-matching-result-list">
<h1>Note Link Matches</h1>
<ul className={"hide-list-styling"}>
{linkFinderResults.map((linkFinderResult: LinkFinderResult) => {
{currentItems.map((linkFinderResult: LinkFinderResult) => {
const selectedReplacements = findReplacements(
linkFinderResult.note
);
Expand All @@ -62,6 +79,20 @@ export const LinkFinderResultsList = ({
);
})}
</ul>
<div className="pagination-controls">
<button onClick={handlePrevPage} disabled={currentPage === 0}>
Previous
</button>
<span>
Page {currentPage + 1} of {totalPages}
</span>
<button
onClick={handleNextPage}
disabled={currentPage === totalPages - 1}
>
Next
</button>
</div>
<button onClick={onClickReplaceButton}>
🔗 Link {totalReplacements()} notes
</button>
Expand Down
6 changes: 6 additions & 0 deletions styles.css
Original file line number Diff line number Diff line change
Expand Up @@ -212,3 +212,9 @@ button {
border-radius: var(--small-round-borders);
padding: var(--small-padding);
}

.pagination-controls {
display: flex;
flex-direction: row;
justify-content: center;
}

0 comments on commit cb53ec2

Please sign in to comment.