|
| 1 | +import type { IRole, IUser } from '@rocket.chat/core-typings'; |
| 2 | +import { useMethod, useStream, useUserId } from '@rocket.chat/ui-contexts'; |
| 3 | +import type { UseQueryOptions } from '@tanstack/react-query'; |
| 4 | +import { useQuery, useQueryClient } from '@tanstack/react-query'; |
| 5 | +import { useEffect } from 'react'; |
| 6 | + |
| 7 | +import { Messages } from '../../app/models/client'; |
| 8 | +import { rolesQueryKeys } from '../lib/queryKeys'; |
| 9 | + |
| 10 | +type UserRoles = { |
| 11 | + uid: IUser['_id']; |
| 12 | + roles: IRole['_id'][]; |
| 13 | +}; |
| 14 | + |
| 15 | +type UseUserRolesQueryOptions<TData = UserRoles[]> = Omit< |
| 16 | + UseQueryOptions<UserRoles[], Error, TData, ReturnType<typeof rolesQueryKeys.userRoles>>, |
| 17 | + 'queryKey' | 'queryFn' |
| 18 | +>; |
| 19 | + |
| 20 | +export const useUserRolesQuery = <TData = UserRoles[]>(options?: UseUserRolesQueryOptions<TData>) => { |
| 21 | + const queryClient = useQueryClient(); |
| 22 | + |
| 23 | + const uid = useUserId(); |
| 24 | + |
| 25 | + const subscribeToNotifyLogged = useStream('notify-logged'); |
| 26 | + |
| 27 | + const enabled = !!uid && (options?.enabled ?? true); |
| 28 | + |
| 29 | + useEffect(() => { |
| 30 | + if (!enabled) return; |
| 31 | + |
| 32 | + return subscribeToNotifyLogged('roles-change', (role) => { |
| 33 | + switch (role.type) { |
| 34 | + case 'added': { |
| 35 | + const { _id: roleId, scope, u } = role; |
| 36 | + if (!!scope || !u) return; |
| 37 | + |
| 38 | + queryClient.setQueryData(rolesQueryKeys.userRoles(), (data: UserRoles[] | undefined = []) => { |
| 39 | + const record = data?.find((record) => record.uid === u._id); |
| 40 | + |
| 41 | + if (!record) { |
| 42 | + return [...data, { uid: u._id, roles: [roleId] }]; |
| 43 | + } |
| 44 | + |
| 45 | + const roles = new Set(record.roles); |
| 46 | + roles.add(roleId); |
| 47 | + record.roles = [...roles]; |
| 48 | + |
| 49 | + return [...data]; |
| 50 | + }); |
| 51 | + Messages.update({ 'u._id': u._id }, { $addToSet: { roles: roleId } }, { multi: true }); |
| 52 | + break; |
| 53 | + } |
| 54 | + |
| 55 | + case 'removed': { |
| 56 | + const { _id: roleId, scope, u } = role; |
| 57 | + if (!!scope || !u) return; |
| 58 | + |
| 59 | + queryClient.setQueryData(rolesQueryKeys.userRoles(), (data: UserRoles[] | undefined = []) => { |
| 60 | + const record = data?.find((record) => record.uid === u._id); |
| 61 | + |
| 62 | + if (!record) return data; |
| 63 | + |
| 64 | + const roles = new Set(record.roles); |
| 65 | + roles.delete(roleId); |
| 66 | + record.roles = [...roles]; |
| 67 | + |
| 68 | + return [...data]; |
| 69 | + }); |
| 70 | + Messages.update({ 'u._id': u._id }, { $pull: { roles: roleId } }, { multi: true }); |
| 71 | + break; |
| 72 | + } |
| 73 | + |
| 74 | + case 'changed': { |
| 75 | + Messages.update({ roles: role._id }, { $inc: { rerender: 1 } }, { multi: true }); |
| 76 | + break; |
| 77 | + } |
| 78 | + } |
| 79 | + }); |
| 80 | + }, [enabled, queryClient, subscribeToNotifyLogged, uid]); |
| 81 | + |
| 82 | + const getUserRoles = useMethod('getUserRoles'); |
| 83 | + |
| 84 | + return useQuery({ |
| 85 | + queryKey: rolesQueryKeys.userRoles(), |
| 86 | + queryFn: async () => { |
| 87 | + const results = await getUserRoles(); |
| 88 | + |
| 89 | + return results.map( |
| 90 | + (record): UserRoles => ({ |
| 91 | + uid: record._id, |
| 92 | + roles: record.roles, |
| 93 | + }), |
| 94 | + ); |
| 95 | + }, |
| 96 | + staleTime: Infinity, |
| 97 | + ...options, |
| 98 | + enabled, |
| 99 | + }); |
| 100 | +}; |
0 commit comments