All files / packages/app/components pagination-block.tsx

100% Statements 10/10
93.75% Branches 30/32
100% Functions 5/5
100% Lines 10/10

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 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153                                                                                  1485x               1485x 1485x 132x   1485x       1485x                                   2x                                                                                                     30x     183x                             1299x                
import React from "react";
 
import { SearchBar } from "~app/components/search-bar";
import { LimitOption } from "~app/features/settings/limit-options";
import type { SearchParamStateDefaulted } from "~app/utils/navigation";
import { DEFAULT_LIMIT } from "~app/utils/validation";
import { Checkbox } from "~components/checkbox";
import { Pagination } from "~components/pagination";
import { cn } from "~components/utils";
import type { ViewReactNode } from "~components/view";
import { View } from "~components/view";
 
type ShapeProps<T> = {
	limit: number;
	totalCount: number;
	offset: number;
	onLimitChange?: (selected: number) => void;
	onPageChange?: (selected: number) => void;
	isDisabled?: boolean;
	selection?: {
		items: T[];
		selectedItems: T[];
		setSelectedItems: React.Dispatch<React.SetStateAction<T[]>>;
	};
	search?: React.ComponentProps<typeof SearchBar>;
	endContent?: ViewReactNode;
};
 
// Generic component has to be a function
// oxlint-disable-next-line func-style react/function-component-definition
function PaginationBlockShape<T>({
	limit,
	totalCount,
	offset,
	onLimitChange,
	onPageChange,
	isDisabled,
	selection,
	endContent,
	search,
}: ShapeProps<T>) {
	const selectedStatus = selection
		? selection.selectedItems.length === selection.items.length
			? "selected-full"
			: selection.selectedItems.length === 0
				? "empty"
				: "selected-partial"
		: undefined;
 
	const [searchInput, setSearchInput] = React.useState(search?.value ?? "");
	React.useEffect(() => {
		search?.onValueChange(searchInput);
	}, [searchInput, search]);
	const searchBar = search ? (
		<SearchBar {...search} value={searchInput} onValueChange={setSearchInput} />
	) : null;
 
	return (
		<>
			<View
				className={cn(
					"flex flex-col-reverse items-end gap-4 sm:flex-row sm:justify-end",
					{
						"sm:justify-between": totalCount > limit,
					},
				)}
				testID="pagination-block"
			>
				<View className="flex w-full flex-row items-center justify-between gap-2 sm:w-auto">
					{selection ? (
						<View className="flex items-center justify-center p-2">
							<Checkbox
								isSelected={selectedStatus !== "empty"}
								isIndeterminate={selectedStatus === "selected-partial"}
								onValueChange={() =>
									selection.setSelectedItems(
										selectedStatus === "selected-full" ? [] : selection.items,
									)
								}
								color="secondary"
							/>
						</View>
					) : null}
					{totalCount > limit ? (
						<Pagination
							className="self-center"
							isDisabled={isDisabled}
							total={Math.ceil(totalCount / limit)}
							page={totalCount === 0 ? 0 : offset / limit + 1}
							onChange={onPageChange}
						/>
					) : searchBar ? (
						<View className="block sm:hidden">{searchBar}</View>
					) : null}
				</View>
				<View className="flex flex-1 flex-row items-center justify-end gap-2">
					{searchBar ? (
						<View
							className={cn(
								totalCount <= limit ? "hidden sm:flex" : "flex sm:hidden",
							)}
						>
							{searchBar}
						</View>
					) : null}
					{endContent}
					{totalCount <= DEFAULT_LIMIT ? null : (
						<LimitOption
							limit={limit}
							onChange={onLimitChange}
							isDisabled={isDisabled}
						/>
					)}
				</View>
			</View>
			{searchBar ? (
				<View
					className={cn("hidden", totalCount > limit ? "sm:block" : undefined)}
				>
					{searchBar}
				</View>
			) : null}
		</>
	);
}
 
export const PaginationBlockSkeleton: React.FC<{ limit: number }> = ({
	limit,
}) => (
	<PaginationBlockShape limit={limit} offset={0} totalCount={50} isDisabled />
);
 
type Props<T> = Omit<ShapeProps<T>, "onLimitChange" | "onPageChange"> & {
	setLimit: SearchParamStateDefaulted<"/_protected/debts/", "limit">[1];
	onPageChange: (page: number) => void;
};
 
// Generic component has to be a function
// oxlint-disable-next-line func-style react/function-component-definition
export function PaginationBlock<T>({
	totalCount,
	setLimit,
	...props
}: Props<T>) {
	return totalCount ? (
		<PaginationBlockShape<T>
			totalCount={totalCount}
			onLimitChange={setLimit}
			{...props}
		/>
	) : null;
}