MCPcopy Create free account
hub / github.com/hashintel/hash / useOrgs

Function useOrgs

apps/hash-frontend/src/components/hooks/use-orgs.ts:23–113  ·  view source on GitHub ↗
()

Source from the content-addressed store, hash-verified

21 * Retrieves a list of organizations
22 */
23export 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) => {

Callers 8

useGetWebIdForShortnameFunction · 0.90
useGetOwnerForEntityFunction · 0.90
OrgGeneralSettingsPageFunction · 0.90
CreateOrgFormFunction · 0.90
TypesTableFunction · 0.90
InviteAccountFormFunction · 0.90
DraftEntitiesFiltersFunction · 0.90
DraftEntityWebFunction · 0.90

Calls 9

useMemoCompareFunction · 0.90
isEntityOrgEntityFunction · 0.90
constructMinimalOrgFunction · 0.90
sortMethod · 0.80
mapMethod · 0.45
joinMethod · 0.45

Tested by

no test coverage detected