Skip to content

Commit

Permalink
fix: incomplete validation on useIsRoomActive
Browse files Browse the repository at this point in the history
  • Loading branch information
aleksandernsilva committed Oct 9, 2023
1 parent 983a8e4 commit a56f12a
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 13 deletions.
13 changes: 2 additions & 11 deletions apps/meteor/client/hooks/omnichannel/useIsOverMacLimit.tsx
Original file line number Diff line number Diff line change
@@ -1,18 +1,9 @@
import { useEndpoint, useStream } from '@rocket.chat/ui-contexts';
import { useQuery, useQueryClient } from '@tanstack/react-query';
import { useEffect } from 'react';
import { useEndpoint } from '@rocket.chat/ui-contexts';
import { useQuery } from '@tanstack/react-query';

export const useIsOverMacLimit = (): boolean => {
const queryClient = useQueryClient();
const notifyLogged = useStream('notify-logged');
const getMacLimit = useEndpoint('GET', '/v1/omnichannel/mac/check');
const { data: { onLimit: isOverMacLimit = false } = {} } = useQuery(['/v1/omnichannel/mac/check'], () => getMacLimit());

useEffect(() => {
return notifyLogged(`mac.limit`, ({ limitReached }) => {
limitReached && queryClient.invalidateQueries(['/v1/omnichannel/mac/check']);
});
}, [notifyLogged, queryClient]);

return !!isOverMacLimit;
};
7 changes: 5 additions & 2 deletions apps/meteor/client/hooks/omnichannel/useIsRoomActive.tsx
Original file line number Diff line number Diff line change
@@ -1,15 +1,18 @@
import type { IOmnichannelGenericRoom } from '@rocket.chat/core-typings';
import { useMemo } from 'react';

import { useIsOverMacLimit } from './useIsOverMacLimit';

const getPeriod = (date: Date) => `${date.getFullYear()}-${String(date.getMonth() + 1).padStart(2, '0')}`;

export const useIsRoomActive = (room: IOmnichannelGenericRoom) => {
const isOverMacLimit = useIsOverMacLimit();
const { activity = [] } = room.v;

const isContactActive = useMemo(() => {
const currentPeriod = getPeriod(new Date());
return activity.includes(currentPeriod);
}, [activity]);
return !isOverMacLimit || activity.includes(currentPeriod);
}, [activity, isOverMacLimit]);

return isContactActive;
};
6 changes: 6 additions & 0 deletions apps/meteor/client/providers/OmnichannelProvider.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,12 @@ const OmnichannelProvider: FC = ({ children }) => {
});
}, [isPrioritiesEnabled, queryClient, subscribe]);

useEffect(() => {
return subscribe(`mac.limit`, ({ limitReached }) => {
limitReached && queryClient.invalidateQueries(['/v1/omnichannel/mac/check']);
});
}, [subscribe, queryClient]);

useEffect(() => {
if (!accessible) {
return;
Expand Down

0 comments on commit a56f12a

Please sign in to comment.