()
| 21 | * Retrieves a list of organizations |
| 22 | */ |
| 23 | export const useOrgs = (): { |
| 24 | loading: boolean; |
| 25 | orgs?: MinimalOrg[]; |
| 26 | refetch: () => Promise<ApolloQueryResult<QueryEntitiesQuery>>; |
| 27 | } => { |
| 28 | const { data, loading, refetch } = useQuery< |
| 29 | QueryEntitiesQuery, |
| 30 | QueryEntitiesQueryVariables |
| 31 | >(queryEntitiesQuery, { |
| 32 | variables: { |
| 33 | request: { |
| 34 | filter: convertBpFilterToGraphFilter({ |
| 35 | filters: [ |
| 36 | entityHasEntityTypeByVersionedUrlFilter( |
| 37 | systemEntityTypes.organization.entityTypeId, |
| 38 | ), |
| 39 | ], |
| 40 | operator: "AND", |
| 41 | }), |
| 42 | temporalAxes: currentTimeInstantTemporalAxes, |
| 43 | includeDrafts: false, |
| 44 | includePermissions: false, |
| 45 | }, |
| 46 | }, |
| 47 | fetchPolicy: "cache-and-network", |
| 48 | }); |
| 49 | |
| 50 | const { queryEntities } = data ?? {}; |
| 51 | |
| 52 | const orgs = useMemoCompare( |
| 53 | () => { |
| 54 | if (!queryEntities) { |
| 55 | return undefined; |
| 56 | } |
| 57 | |
| 58 | return deserializeQueryEntitiesResponse(queryEntities).entities.map( |
| 59 | (orgEntity) => { |
| 60 | if (!isEntityOrgEntity(orgEntity)) { |
| 61 | throw new Error( |
| 62 | `Entity with type(s) ${orgEntity.metadata.entityTypeIds.join(", ")} is not an org entity`, |
| 63 | ); |
| 64 | } |
| 65 | return constructMinimalOrg({ orgEntity }); |
| 66 | }, |
| 67 | ); |
| 68 | }, |
| 69 | [queryEntities], |
| 70 | /** |
| 71 | * Check if the previous and new orgs are the same. |
| 72 | * If they are, the return value from the hook won't change, avoiding unnecessary re-renders. |
| 73 | * |
| 74 | * This assumes that the UX/performance benefit of avoiding re-renders outweighs the cost of the comparison. |
| 75 | * |
| 76 | * An alternative approach would be to not use a 'cache-and-network' fetch policy, which also makes a network request |
| 77 | * for all the orgs every time the hook is run, but instead use polling (or a subscription) to get updates. |
| 78 | * |
| 79 | * An identical approach is taken in {@link useUsers}. Update that too if this is changed. |
| 80 | */ (a, b) => { |
no test coverage detected