-
Notifications
You must be signed in to change notification settings - Fork 10.7k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
chore(Sidepanel): uses only local channels and discussions (#33387)
- Loading branch information
Showing
3 changed files
with
20 additions
and
67 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
79 changes: 16 additions & 63 deletions
79
apps/meteor/client/views/room/Sidepanel/hooks/useTeamslistChildren.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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<IRoom> = { | ||
$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<ReturnType<typeof teamList>> | 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; | ||
}; |