diff --git a/src/lib/utils.ts b/src/lib/utils.ts index b54de9e..f87b9cc 100644 --- a/src/lib/utils.ts +++ b/src/lib/utils.ts @@ -1,21 +1,41 @@ -import { format } from "date-fns"; +import { + type MRT_PaginationState, + type MRT_ColumnFiltersState, +} from 'material-react-table'; +import { format, type Duration } from "date-fns"; -function formatDate(orig) { +function getUrl(path: string, filters: MRT_ColumnFiltersState, pagination: MRT_PaginationState) { + const newUrl = new URL(path, window.location.origin); + filters.forEach(item => { + if ( ! item.id ) return; + if ( item.value instanceof Function ) return; + if ( item.value instanceof Date ) { + newUrl.searchParams.set("date", formatDay(item.value)); + } else { + newUrl.searchParams.set(String(item.id), String(item.value)); + } + }); + if ( pagination.pageIndex ) newUrl.searchParams.set("page", String(pagination.pageIndex)); + newUrl.searchParams.set("pageSize", String(pagination.pageSize)); + return newUrl; +} + +function formatDate(orig: string | number | Date) { if (!orig) return ""; return format(new Date(orig), "yyyy-MM-dd HH:mm:ss"); } -function formatDay(orig) { +function formatDay(orig: string | number | Date) { if (!orig) return ""; return format(new Date(orig), "yyyy-MM-dd"); } -function pad(num) { +function pad(num: number) { return `${num}`.padStart(2, "0"); } -function getDuration(seconds) { - const result = {}; +function getDuration(seconds: number) { + const result: Duration = {}; let seconds_ = seconds; result.days = Math.floor(seconds_ / (60 * 60 * 24)); seconds_ %= 60 * 60 * 24; @@ -27,7 +47,7 @@ function getDuration(seconds) { return result; } -function formatDuration(seconds) { +function formatDuration(seconds: number) { let result = seconds; const hours = Math.floor(result / 3600); result %= 3600; @@ -36,10 +56,10 @@ function formatDuration(seconds) { return `${pad(hours)}:${pad(minutes)}:${pad(result)}`; } -function dirName(path) { +function dirName(path: string) { const array = path.split("/"); array.splice(-1, 1, ""); return array.join("/"); } -export { formatDate, formatDay, getDuration, formatDuration, dirName }; +export { formatDate, formatDay, getDuration, formatDuration, dirName, getUrl };