1
- import { QueryClient } from "react-query" ;
1
+ import { QueryClient , UseQueryOptions } from "react-query" ;
2
2
import * as API from "api/api" ;
3
3
import { checkAuthorization } from "api/api" ;
4
4
import {
@@ -25,6 +25,44 @@ export const group = (groupId: string) => {
25
25
} ;
26
26
} ;
27
27
28
+ export type GroupsByUserId = Readonly < Map < string , readonly Group [ ] > > ;
29
+
30
+ export function groupsByUserId ( organizationId : string ) {
31
+ return {
32
+ ...groups ( organizationId ) ,
33
+ select : ( allGroups ) => {
34
+ const userIdMapper = new Map < string , Group [ ] > ( ) ;
35
+
36
+ for ( const group of allGroups ) {
37
+ for ( const user of group . members ) {
38
+ let groupsForUser = userIdMapper . get ( user . id ) ;
39
+ if ( groupsForUser === undefined ) {
40
+ groupsForUser = [ ] ;
41
+ userIdMapper . set ( user . id , groupsForUser ) ;
42
+ }
43
+
44
+ groupsForUser . push ( group ) ;
45
+ }
46
+ }
47
+
48
+ for ( const groupsList of userIdMapper . values ( ) ) {
49
+ groupsList . sort ( ( g1 , g2 ) => {
50
+ const key =
51
+ g1 . display_name && g2 . display_name ? "display_name" : "name" ;
52
+
53
+ if ( g1 [ key ] === g2 [ key ] ) {
54
+ return 0 ;
55
+ }
56
+
57
+ return g1 [ key ] < g2 [ key ] ? - 1 : 1 ;
58
+ } ) ;
59
+ }
60
+
61
+ return userIdMapper ;
62
+ } ,
63
+ } satisfies UseQueryOptions < Group [ ] , unknown , GroupsByUserId > ;
64
+ }
65
+
28
66
export const groupPermissions = ( groupId : string ) => {
29
67
return {
30
68
queryKey : [ ...getGroupQueryKey ( groupId ) , "permissions" ] ,
0 commit comments