All files / views/webviews/sparql-results/components bindings-table-paging-hook.ts

0% Statements 0/18
0% Branches 0/18
0% Functions 0/6
0% Lines 0/18

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54                                                                                                           
import { useState, useCallback, useEffect } from 'react';
import { BindingsResult } from '@src/languages/sparql/services/sparql-query-state';
import { BindingsTablePagingState } from './bindings-table-paging-state';
 
/**
 * Custom hook for managing pagination state in a SPARQL bindings table.
 * @param result The result dataset.
 * @param defaultPageSize The default page size.
 * @returns An object containing pagination state and control functions.
 */
export function useBindingsTablePaging(result?: BindingsResult, defaultPageSize?: number) {
	const [paging, setPaging] = useState<BindingsTablePagingState | undefined>();
 
	// Initialize paging when result changes
	useEffect(() => {
		if (result) {
			setPaging(new BindingsTablePagingState(result, 0, defaultPageSize));
		} else {
			setPaging(undefined);
		}
	}, [result]);
 
	const updatePage = useCallback((page: number) => {
		if (paging && result) {
			setPaging(new BindingsTablePagingState(result, page, paging.pageSize));
		}
	}, [paging, result]);
 
	const updatePageSize = useCallback((pageSize: number) => {
		if (paging && result) {
			setPaging(new BindingsTablePagingState(result, 0, pageSize));
		}
	}, [paging, result]);
 
	const nextPage = useCallback(() => {
		if (paging && paging.currentPage < paging.totalPages - 1) {
			updatePage(paging.currentPage + 1);
		}
	}, [paging, updatePage]);
 
	const previousPage = useCallback(() => {
		if (paging && paging.currentPage > 0) {
			updatePage(paging.currentPage - 1);
		}
	}, [paging, updatePage]);
 
	return {
		paging,
		updatePage,
		updatePageSize,
		nextPage,
		previousPage
	};
}