From a80a5770e2826b3494840cbfbbd64dade64e4fbb Mon Sep 17 00:00:00 2001 From: Aleksander Nicacio da Silva Date: Thu, 5 Oct 2023 16:18:21 -0300 Subject: [PATCH] chore: added mac endpoints and stream --- .../hooks/omnichannel/useIsOverMacLimit.tsx | 21 +++++++++++++------ .../hooks/omnichannel/useIsRoomActive.tsx | 13 ++++++------ 2 files changed, 22 insertions(+), 12 deletions(-) diff --git a/apps/meteor/client/hooks/omnichannel/useIsOverMacLimit.tsx b/apps/meteor/client/hooks/omnichannel/useIsOverMacLimit.tsx index 21e534c962dad..f09cb858bcfd9 100644 --- a/apps/meteor/client/hooks/omnichannel/useIsOverMacLimit.tsx +++ b/apps/meteor/client/hooks/omnichannel/useIsOverMacLimit.tsx @@ -1,9 +1,18 @@ -// @ts-nocheck -// import { useEndpoint } from '@rocket.chat/ui-contexts'; -import { useQuery } from '@tanstack/react-query'; +import { useEndpoint, useStream } from '@rocket.chat/ui-contexts'; +import { useQuery, useQueryClient } from '@tanstack/react-query'; +import { useEffect } from 'react'; export const useIsOverMacLimit = (): boolean => { - const getMacLimit = () => Promise.resolve(true); // useEndpoint('GET', '/v1/livechat/mac'); // TODO: add the correct endpoint - const { data: isOverMacLimit } = useQuery(['omnichannel', '/v1/livechat/mac'], () => getMacLimit()); - return isOverMacLimit; + const queryClient = useQueryClient(); + const notifyLogged = useStream('notify-logged'); + const getMacLimit = useEndpoint('GET', '/v1/omnichannel/mac/check'); + const { data: isOverMacLimit } = useQuery(['/v1/omnichannel/mac/check'], () => getMacLimit()); + + useEffect(() => { + return notifyLogged(`mac.limit`, ({ limitReached }) => { + limitReached && queryClient.invalidateQueries(['/v1/omnichannel/mac/check']); + }); + }, [notifyLogged, queryClient]); + + return !!isOverMacLimit; }; diff --git a/apps/meteor/client/hooks/omnichannel/useIsRoomActive.tsx b/apps/meteor/client/hooks/omnichannel/useIsRoomActive.tsx index b57a35b0ce282..51c6f8d20f3fc 100644 --- a/apps/meteor/client/hooks/omnichannel/useIsRoomActive.tsx +++ b/apps/meteor/client/hooks/omnichannel/useIsRoomActive.tsx @@ -1,12 +1,13 @@ -import type { IOmnichannelRoom } from '@rocket.chat/core-typings'; +import type { IOmnichannelGenericRoom } from '@rocket.chat/core-typings'; import { useMemo } from 'react'; -export const useIsRoomActive = (room: IOmnichannelRoom) => { - // @ts-ignore - const { activity = [] } = room.v; // TODO: add activity to IOmnichannelRoom['v'] +const getPeriod = (date: Date) => `${date.getFullYear()}-${String(date.getMonth() + 1).padStart(2, '0')}`; + +export const useIsRoomActive = (room: IOmnichannelGenericRoom) => { + const { activity = [] } = room.v; + const isContactActive = useMemo(() => { - const date = new Date(); - const currentPeriod = `${date.getFullYear()}-${date.getMonth() + 1}`; + const currentPeriod = getPeriod(new Date()); return activity.includes(currentPeriod); }, [activity]);