- {downloads.length > 0 ? (
- downloads.map((state) => {
+ {paginatedCompletedDownloads.data.length > 0 ? (
+ <>
+ {paginatedCompletedDownloads.data.map((state) => {
return (
);
- })
+ })}
+ {paginatedCompletedDownloads.pages.length > 1 && (
+
+ )}
+ >
) : (
diff --git a/src/helpers/use-downloader.ts b/src/helpers/use-downloader.ts
index 51603cb..c940d88 100644
--- a/src/helpers/use-downloader.ts
+++ b/src/helpers/use-downloader.ts
@@ -88,8 +88,8 @@ export default function useDownloader() {
const updateDownloadState = debounce((state: DownloadState) => {
downloadStateSaver.mutate(state, {
- onSuccess: (data) => {
- console.log("Download State saved successfully:", data);
+ onSuccess: (_data) => {
+ // console.log("Download State saved successfully:", data);
queryClient.invalidateQueries({ queryKey: ['download-states'] });
},
onError: (error) => {
diff --git a/src/pages/library.tsx b/src/pages/library.tsx
index 586841f..4c214ef 100644
--- a/src/pages/library.tsx
+++ b/src/pages/library.tsx
@@ -20,8 +20,7 @@ export default function LibraryPage() {
const { pauseDownload } = useAppContext();
const incompleteDownloads = downloadStates.filter(state => state.download_status !== 'completed');
- const completedDownloads = downloadStates.filter(state => state.download_status === 'completed')
- .sort((a, b) => {
+ const completedDownloads = downloadStates.filter(state => state.download_status === 'completed').sort((a, b) => {
// Latest updated first
const dateA = a.updated_at ? new Date(a.updated_at).getTime() : 0;
const dateB = b.updated_at ? new Date(b.updated_at).getTime() : 0;
diff --git a/src/services/store.ts b/src/services/store.ts
index 86c0c0b..87f8fd3 100644
--- a/src/services/store.ts
+++ b/src/services/store.ts
@@ -114,7 +114,9 @@ export const useDownloaderPageStatesStore = create((s
export const useLibraryPageStatesStore = create((set) => ({
activeTab: 'completed',
- setActiveTab: (tab) => set(() => ({ activeTab: tab }))
+ activeCompletedDownloadsPage: 1,
+ setActiveTab: (tab) => set(() => ({ activeTab: tab })),
+ setActiveCompletedDownloadsPage: (page) => set(() => ({ activeCompletedDownloadsPage: page }))
}));
export const useDownloadActionStatesStore = create((set) => ({
diff --git a/src/types/download.ts b/src/types/download.ts
index 0dc2ec0..59af055 100644
--- a/src/types/download.ts
+++ b/src/types/download.ts
@@ -97,3 +97,21 @@ export interface DownloadProgress {
total: number | null;
eta: number | null;
}
+
+export interface Paginated {
+ current_page: number;
+ from: number;
+ first_page: number;
+ last_page: number;
+ pages: Array<{
+ label: string;
+ page: number;
+ active: boolean;
+ }>;
+ next_page: number | null;
+ per_page: number;
+ prev_page: number | null;
+ to: number;
+ total: number;
+ data: T[];
+}
diff --git a/src/types/store.ts b/src/types/store.ts
index 6c4a691..bfb4a69 100644
--- a/src/types/store.ts
+++ b/src/types/store.ts
@@ -71,7 +71,9 @@ export interface DownloaderPageStatesStore {
export interface LibraryPageStatesStore {
activeTab: string;
+ activeCompletedDownloadsPage: number;
setActiveTab: (tab: string) => void;
+ setActiveCompletedDownloadsPage: (page: number) => void;
}
export interface DownloadActionStatesStore {
diff --git a/src/utils.ts b/src/utils.ts
index db7cae1..951be19 100644
--- a/src/utils.ts
+++ b/src/utils.ts
@@ -1,6 +1,6 @@
import { RoutesObj } from "@/types/route";
import { AllRoutes } from "@/routes";
-import { DownloadProgress } from "@/types/download";
+import { DownloadProgress, Paginated } from "@/types/download";
import { VideoFormat } from "@/types/video";
import * as fs from "@tauri-apps/plugin-fs";
@@ -402,3 +402,46 @@ export const sortByBitrate = (formats: VideoFormat[] | undefined) => {
return 0;
});
};
+
+export const paginate = (items: T[], currentPage: number, itemsPerPage: number): Paginated => {
+ const total = items.length;
+ const lastPage = Math.max(1, Math.ceil(total / itemsPerPage));
+
+ // Clamp current page to valid range
+ const validCurrentPage = Math.max(1, Math.min(currentPage, lastPage));
+
+ // Calculate start and end indices
+ const startIndex = (validCurrentPage - 1) * itemsPerPage;
+ const endIndex = Math.min(startIndex + itemsPerPage, total);
+
+ // Get paginated data
+ const data = items.slice(startIndex, endIndex);
+
+ // Calculate from/to (1-indexed)
+ const from = total > 0 ? startIndex + 1 : 0;
+ const to = total > 0 ? endIndex : 0;
+
+ // Generate pages array
+ const pages: Array<{ label: string; page: number; active: boolean }> = [];
+ for (let i = 1; i <= lastPage; i++) {
+ pages.push({
+ label: i.toString(),
+ page: i,
+ active: i === validCurrentPage
+ });
+ }
+
+ return {
+ current_page: validCurrentPage,
+ from,
+ first_page: 1,
+ last_page: lastPage,
+ pages,
+ next_page: validCurrentPage < lastPage ? validCurrentPage + 1 : null,
+ per_page: itemsPerPage,
+ prev_page: validCurrentPage > 1 ? validCurrentPage - 1 : null,
+ to,
+ total,
+ data
+ };
+};