/**
* useRoleQuery fetches role name from the api, using cached results if possible to avoid further fetches, since role names are not changeable.
*/
export function useRoleName(roleId: string, { enabled } = DEFAULT_OPTIONS): string | undefined {
const queryClient = useQueryClient();
const roleQuery = useQuery({
queryKey: roleQueryKeys.detail(roleId),
queryFn: () => Roles.getRole(roleId),
enabled,
staleTime: Infinity,
// Use a role from the roles list query as the initial data, if possible
placeholderData: () => {
const queryResults = queryClient.getQueriesData<Successful<RoleListItem[] | null> | undefined>(
roleQueryKeys.lists()
);
const roleListItem = queryResults
.map((hit) => hit[1]?.data)
.flat()
.find((d) => {
return d?.id === roleId;
});
// If we found a match, use it as the initial data for the query
if (roleListItem) {
const role = { ...roleListItem, firewallRules: [] };
return { data: role };
}
},
});
return roleQuery.data?.data.name;
}
/**
* useRoleQuery fetches role name from the api, using cached results if possible to avoid further fetches, since role names are not changeable.
*/
export function useRoleName(roleId: string, { enabled } = DEFAULT_OPTIONS): string | undefined {
const queryClient = useQueryClient();
const roleQuery = useQuery({
queryKey: roleQueryKeys.detail(roleId),
queryFn: () => Roles.getRole(roleId),
enabled,
staleTime: Infinity,
// Use a role from the roles list query as the initial data, if possible
placeholderData: () => {
const queryResults = queryClient.getQueriesData<Successful<RoleListItem[] | null> | undefined>(
roleQueryKeys.lists()
);
const roleListItem = queryResults
.map((hit) => hit[1]?.data)
.flat()
.find((d) => {
return d?.id === roleId;
});
// If we found a match, use it as the initial data for the query
if (roleListItem) {
const role = { ...roleListItem, firewallRules: [] };
return { data: role };
}
},
});
return roleQuery.data?.data.name;
}