From 9f21b58c1e5ea33efa024a16b6ba085509e755da Mon Sep 17 00:00:00 2001 From: Guilherme Gazzo Date: Fri, 27 Sep 2024 19:11:27 -0300 Subject: [PATCH] chore(Sidepanel): uses only local channels and discussions (#33387) --- .../views/room/Sidepanel/RoomSidepanel.tsx | 4 +- .../SidepanelItem/RoomSidepanelItem.tsx | 4 +- .../Sidepanel/hooks/useTeamslistChildren.ts | 79 ++++--------------- 3 files changed, 20 insertions(+), 67 deletions(-) diff --git a/apps/meteor/client/views/room/Sidepanel/RoomSidepanel.tsx b/apps/meteor/client/views/room/Sidepanel/RoomSidepanel.tsx index 27c45e2774e8..6ee16a850202 100644 --- a/apps/meteor/client/views/room/Sidepanel/RoomSidepanel.tsx +++ b/apps/meteor/client/views/room/Sidepanel/RoomSidepanel.tsx @@ -51,8 +51,8 @@ const RoomSidepanelWithData = ({ parentRid, openedRoom }: { parentRid: string; o ( diff --git a/apps/meteor/client/views/room/Sidepanel/SidepanelItem/RoomSidepanelItem.tsx b/apps/meteor/client/views/room/Sidepanel/SidepanelItem/RoomSidepanelItem.tsx index dceb69e1aba3..8bb4d84eaebe 100644 --- a/apps/meteor/client/views/room/Sidepanel/SidepanelItem/RoomSidepanelItem.tsx +++ b/apps/meteor/client/views/room/Sidepanel/SidepanelItem/RoomSidepanelItem.tsx @@ -1,4 +1,4 @@ -import type { IRoom, ISubscription, Serialized } from '@rocket.chat/core-typings'; +import type { IRoom, ISubscription } from '@rocket.chat/core-typings'; import { useUserSubscription } from '@rocket.chat/ui-contexts'; import React, { memo } from 'react'; @@ -8,7 +8,7 @@ import { useItemData } from '../hooks/useItemData'; export type RoomSidepanelItemProps = { openedRoom?: string; - room: Serialized; + room: IRoom; parentRid: string; viewMode?: 'extended' | 'medium' | 'condensed'; }; diff --git a/apps/meteor/client/views/room/Sidepanel/hooks/useTeamslistChildren.ts b/apps/meteor/client/views/room/Sidepanel/hooks/useTeamslistChildren.ts index 772c29509608..de7645ae2a30 100644 --- a/apps/meteor/client/views/room/Sidepanel/hooks/useTeamslistChildren.ts +++ b/apps/meteor/client/views/room/Sidepanel/hooks/useTeamslistChildren.ts @@ -1,107 +1,60 @@ import type { IRoom } from '@rocket.chat/core-typings'; -import { useEndpoint } from '@rocket.chat/ui-contexts'; -import { useMutation, useQuery, useQueryClient } from '@tanstack/react-query'; +import { useQuery } from '@tanstack/react-query'; import type { Mongo } from 'meteor/mongo'; import { useEffect, useMemo } from 'react'; import { ChatRoom } from '../../../../../app/models/client'; -const sortRoomByLastMessage = (a: IRoom, b: IRoom) => { - if (!a.lm) { - return 1; - } - if (!b.lm) { - return -1; - } - return b.lm.getTime() - a.lm.getTime(); -}; - export const useTeamsListChildrenUpdate = ( parentRid: string, teamId?: string | null, sidepanelItems?: 'channels' | 'discussions' | null, ) => { - const queryClient = useQueryClient(); - const query = useMemo(() => { const query: Mongo.Selector = { $or: [ { _id: parentRid, }, - { - prid: parentRid, - }, ], }; - if (teamId && query.$or) { + if ((!sidepanelItems || sidepanelItems === 'discussions') && query.$or) { + query.$or.push({ + prid: parentRid, + }); + } + + if ((!sidepanelItems || sidepanelItems === 'channels') && teamId && query.$or) { query.$or.push({ teamId, }); } return query; - }, [parentRid, teamId]); + }, [parentRid, teamId, sidepanelItems]); - const teamList = useEndpoint('GET', '/v1/teams.listChildren'); - - const listRoomsAndDiscussions = useEndpoint('GET', '/v1/teams.listChildren'); const result = useQuery({ queryKey: ['sidepanel', 'list', parentRid, sidepanelItems], queryFn: () => - listRoomsAndDiscussions({ - roomId: parentRid, - sort: JSON.stringify({ lm: -1 }), - type: sidepanelItems || undefined, - }), + ChatRoom.find(query, { + sort: { lm: -1 }, + }).fetch(), enabled: sidepanelItems !== null && teamId !== null, refetchInterval: 5 * 60 * 1000, keepPreviousData: true, }); - const { mutate: update } = useMutation({ - mutationFn: async (params?: { action: 'add' | 'remove' | 'update'; data: IRoom }) => { - queryClient.setQueryData(['sidepanel', 'list', parentRid, sidepanelItems], (data: Awaited> | void) => { - if (!data) { - return; - } - - if (params?.action === 'add') { - data.data = [JSON.parse(JSON.stringify(params.data)), ...data.data].sort(sortRoomByLastMessage); - } - - if (params?.action === 'remove') { - data.data = data.data.filter((item) => item._id !== params.data?._id); - } - - if (params?.action === 'update') { - data.data = data.data - .map((item) => (item._id === params.data?._id ? JSON.parse(JSON.stringify(params.data)) : item)) - .sort(sortRoomByLastMessage); - } - - return { ...data }; - }); - }, - }); - useEffect(() => { const liveQueryHandle = ChatRoom.find(query).observe({ - added: (item) => { - queueMicrotask(() => update({ action: 'add', data: item })); - }, - changed: (item) => { - queueMicrotask(() => update({ action: 'update', data: item })); - }, - removed: (item) => { - queueMicrotask(() => update({ action: 'remove', data: item })); - }, + added: () => queueMicrotask(() => result.refetch({ exact: false })), + changed: () => queueMicrotask(() => result.refetch({ exact: false })), + removed: () => queueMicrotask(() => result.refetch({ exact: false })), }); return () => { liveQueryHandle.stop(); }; - }, [update, query]); + }, [query, result]); return result; };