Files
tm_panel/src/components/Tables/notification-history/useNotificationHistoryTablePresenter.ts
T
AmirReza Jamali 72948a9b55 feat(dashboard): add daily trend chart and statistics section to dashboard
- Introduced DailyTrendChart component for visualizing daily scraped TED notices, documents, and translated notices.
- Implemented StatisticsSection to display key metrics with animated counters and a days selector for data range.
- Created utility function to convert API data into labeled points for chart rendering.
- Updated dashboard index to include the new StatisticsSection, enhancing overall dashboard functionality and user experience.
- Refactored existing trends chart to utilize the new DailyPoint type for consistency in data handling.
2026-06-07 12:39:15 +03:30

116 lines
3.1 KiB
TypeScript

"use client";
import { apiDefaultParams } from "@/constants/Api_Params";
import { useGetNotificationHistoryQuery } from "@/hooks/queries/useNotificationQueries";
import { useHybridPagination } from "@/hooks/useHybridPagination";
import { useTableSectionRowAnimations } from "@/hooks/useTableSectionRowAnimations";
import { deleteEmptyKeys } from "@/utils/shared";
import { useIsMutating } from "@tanstack/react-query";
import { usePathname, useRouter, useSearchParams } from "next/navigation";
import { useCallback, useEffect, useMemo, useRef, useState } from "react";
import { useForm } from "react-hook-form";
const useNotificationHistoryTablePresenter = () => {
const {
register: filterRegister,
handleSubmit,
watch,
setValue,
reset: filterFormReset,
} = useForm();
const [params, setParams] = useState<Record<string, any>>({
...apiDefaultParams,
});
const searchParams = useSearchParams();
const tableSectionRef = useRef<HTMLDivElement>(null);
const skeletonTbodyRef = useRef<HTMLTableSectionElement>(null);
const dataTbodyRef = useRef<HTMLTableSectionElement>(null);
const { data, isLoading, isError, error } =
useGetNotificationHistoryQuery(params);
const { pagination, handlePaginationChange, buildFirstPageParams } =
useHybridPagination({
meta: data?.meta,
params,
setParams,
isError,
error,
});
useTableSectionRowAnimations(isLoading, data, {
tableSectionRef,
skeletonTbodyRef,
dataTbodyRef,
});
const shouldShowPagination =
!isLoading && (data?.meta?.pages ?? 1) * (data?.meta?.limit ?? 10) > 10;
const isMutating = useIsMutating();
const columns: string[] = [
"row",
"title",
"channel",
"created at",
"receiver",
"message",
"priority",
"type",
"seen",
"status",
"actions",
];
const pathName = usePathname();
const router = useRouter();
useEffect(() => {
const newParams: Record<string, any> = {
...apiDefaultParams,
offset: 0,
limit: 10,
};
for (const [key, value] of searchParams.entries()) {
newParams[key] = value;
}
setParams(newParams);
filterFormReset(newParams);
}, [searchParams, filterFormReset]);
const search = (formData: any) => {
const newParams = buildFirstPageParams({ ...params, ...formData });
const cleanedParams = deleteEmptyKeys(newParams);
const queryString = new URLSearchParams(cleanedParams).toString();
router.push(`${pathName}?${queryString}`);
};
const clearAllFilters = useCallback(() => {
filterFormReset({ seen: undefined, search: "" });
router.push(pathName);
}, [pathName, router, filterFormReset]);
return {
notificationHistory: data?.data,
metadata: data?.meta,
isLoading,
columns,
router,
pathName,
setParams,
filterRegister,
handleSubmit,
handleFilterSubmit: handleSubmit,
watch,
setValue,
isMutating,
search,
shouldShowPagination,
clearAllFilters,
tableSectionRef,
skeletonTbodyRef,
dataTbodyRef,
pagination,
handlePaginationChange,
};
};
export default useNotificationHistoryTablePresenter;