UNPKG

@droppii-org/chat-sdk

Version:

Droppii React Chat SDK

73 lines (72 loc) 3.48 kB
var __rest = (this && this.__rest) || function (s, e) { var t = {}; for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p) && e.indexOf(p) < 0) t[p] = s[p]; if (s != null && typeof Object.getOwnPropertySymbols === "function") for (var i = 0, p = Object.getOwnPropertySymbols(s); i < p.length; i++) { if (e.indexOf(p[i]) < 0 && Object.prototype.propertyIsEnumerable.call(s, p[i])) t[p[i]] = s[p[i]]; } return t; }; import { useInfiniteQuery } from "@tanstack/react-query"; import { QUERY_KEYS } from "../../services/query"; import { apiInstance } from "../../services/api"; import { ENDPOINTS } from "../../services/routes"; import { useMemo } from "react"; import dayjs from "dayjs"; import useAuthStore from "../../store/auth"; import { PAGE_SIZE } from "../../constants"; export const useSearchMessage = ({ payload, options, }) => { const _a = useInfiniteQuery({ initialPageParam: 1, queryKey: [QUERY_KEYS.SEARCH_MESSAGE, payload, options], queryFn: async ({ pageParam = 1 }) => { const params = Object.assign(Object.assign({ pageSize: (options === null || options === void 0 ? void 0 : options.pageSize) || PAGE_SIZE }, payload), { page: pageParam, applicationType: useAuthStore.getState().applicationType }); const res = await apiInstance.post(ENDPOINTS.chatService.searchMessage, params); return res.data; }, getNextPageParam: (lastPage) => { var _a, _b; const pageSize = (options === null || options === void 0 ? void 0 : options.pageSize) || PAGE_SIZE; const dataLength = ((_a = lastPage === null || lastPage === void 0 ? void 0 : lastPage.data) === null || _a === void 0 ? void 0 : _a.length) || 0; const currentPage = ((_b = lastPage === null || lastPage === void 0 ? void 0 : lastPage.pageable) === null || _b === void 0 ? void 0 : _b.pageNumber) || 1; return dataLength < pageSize ? undefined : currentPage + 1; }, enabled: hasValidFilter(payload), }), { data, fetchNextPage, hasNextPage, isFetchingNextPage, isLoading } = _a, rest = __rest(_a, ["data", "fetchNextPage", "hasNextPage", "isFetchingNextPage", "isLoading"]); const { groupedData, dataFlatten } = useMemo(() => { var _a, _b; if (!data) return { groupedData: {}, dataFlatten: [], }; const allItems = ((_b = (_a = data === null || data === void 0 ? void 0 : data.pages) === null || _a === void 0 ? void 0 : _a.flatMap) === null || _b === void 0 ? void 0 : _b.call(_a, (page) => page.data)) || []; const mGroupeddata = allItems.reduce((acc, item) => { const dateKey = dayjs(item.chatLog.sendTime).format("YYYY-MM-DD"); if (!acc[dateKey]) acc[dateKey] = []; acc[dateKey].push(item); return acc; }, {}); return { groupedData: mGroupeddata, dataFlatten: allItems, }; }, [data]); return Object.assign({ data, fetchNextPage, hasNextPage, isFetchingNextPage, isLoading, groupedData, dataFlatten }, rest); }; const hasValidFilter = (filter) => { return Object.values(filter).some((v) => { if (typeof v === "string") return v.trim() !== ""; return v !== undefined && v !== null; }); };