1
0
Fork 0
mirror of https://github.com/jellyfin/jellyfin-web synced 2025-03-30 19:56:21 +00:00
jellyfin-web/src/view/components/Pagination.tsx

85 lines
3.1 KiB
TypeScript
Raw Normal View History

2022-08-06 01:36:13 +03:00
import { BaseItemDtoQueryResult } from '@thornbill/jellyfin-sdk/dist/generated-client';
2022-10-02 19:07:42 +03:00
import React, { FC, useCallback, useEffect, useRef } from 'react';
2022-08-21 03:09:22 +03:00
import IconButtonElement from '../../elements/IconButtonElement';
import globalize from '../../scripts/globalize';
2022-10-02 19:07:42 +03:00
import { QueryI } from './interface';
2022-08-06 01:36:13 +03:00
2022-10-02 19:07:42 +03:00
interface PaginationI {
query: QueryI;
2022-08-06 01:36:13 +03:00
itemsResult?: BaseItemDtoQueryResult;
reloadItems: () => void;
}
2022-10-02 19:07:42 +03:00
const Pagination: FC<PaginationI> = ({ query, itemsResult = {}, reloadItems }) => {
2022-08-21 03:09:22 +03:00
const startIndex = query.StartIndex;
const limit = query.Limit;
const totalRecordCount = itemsResult.TotalRecordCount || 0;
const recordsEnd = Math.min(startIndex + limit, totalRecordCount);
const showControls = limit < totalRecordCount;
2022-08-06 01:36:13 +03:00
const element = useRef<HTMLDivElement>(null);
2022-08-21 03:09:22 +03:00
const onNextPageClick = useCallback(() => {
if (query.Limit > 0) {
query.StartIndex += query.Limit;
2022-08-06 01:36:13 +03:00
}
2022-08-21 03:09:22 +03:00
reloadItems();
}, [query, reloadItems]);
2022-08-06 01:36:13 +03:00
2022-08-21 03:09:22 +03:00
const onPreviousPageClick = useCallback(() => {
if (query.Limit > 0) {
query.StartIndex = Math.max(0, query.StartIndex - query.Limit);
2022-08-06 01:36:13 +03:00
}
2022-08-21 03:09:22 +03:00
reloadItems();
}, [query, reloadItems]);
2022-08-06 01:36:13 +03:00
2022-08-21 03:09:22 +03:00
useEffect(() => {
2022-08-06 01:36:13 +03:00
const btnNextPage = element.current?.querySelector('.btnNextPage') as HTMLButtonElement;
if (btnNextPage) {
2022-08-21 03:09:22 +03:00
if (startIndex + limit >= totalRecordCount) {
btnNextPage.disabled = true;
} else {
btnNextPage.disabled = false;
}
2022-08-06 01:36:13 +03:00
btnNextPage.addEventListener('click', onNextPageClick);
}
const btnPreviousPage = element.current?.querySelector('.btnPreviousPage') as HTMLButtonElement;
if (btnPreviousPage) {
2022-08-21 03:09:22 +03:00
if (startIndex) {
btnPreviousPage.disabled = false;
} else {
btnPreviousPage.disabled = true;
}
2022-08-06 01:36:13 +03:00
btnPreviousPage.addEventListener('click', onPreviousPageClick);
}
2022-08-21 03:09:22 +03:00
}, [totalRecordCount, onNextPageClick, onPreviousPageClick, limit, startIndex]);
2022-08-06 01:36:13 +03:00
return (
<div ref={element}>
2022-08-21 03:09:22 +03:00
<div className='paging'>
{showControls && (
<div className='listPaging' style={{ display: 'flex', alignItems: 'center' }}>
<span>
{globalize.translate('ListPaging', (totalRecordCount ? startIndex + 1 : 0), recordsEnd, totalRecordCount)}
</span>
<IconButtonElement
is='paper-icon-button-light'
className='btnPreviousPage autoSize'
icon='material-icons arrow_back'
/>
<IconButtonElement
is='paper-icon-button-light'
className='btnNextPage autoSize'
icon='material-icons arrow_forward'
/>
</div>
)}
</div>
2022-08-06 01:36:13 +03:00
</div>
);
};
export default Pagination;