mirror of
https://github.com/docmost/docmost.git
synced 2026-05-18 23:44:24 +08:00
f92d63261d
* Hide pagination buttons if there is nothing to paginate * Create reusable hook for search and pagination
18 lines
515 B
TypeScript
18 lines
515 B
TypeScript
import { useState, useRef, useCallback } from "react";
|
|
|
|
export function usePaginateAndSearch(initialQuery: string = "") {
|
|
const [search, setSearch] = useState(initialQuery);
|
|
const [page, setPage] = useState(1);
|
|
const prevSearchRef = useRef(search);
|
|
|
|
const handleSearch = useCallback((newQuery: string) => {
|
|
if (prevSearchRef.current !== newQuery) {
|
|
prevSearchRef.current = newQuery;
|
|
setSearch(newQuery);
|
|
setPage(1);
|
|
}
|
|
}, []);
|
|
|
|
return { search, page, setPage, handleSearch };
|
|
}
|