fix: Unneccessary network requests on shared documents

This commit is contained in:
Tom Moor
2024-05-18 11:29:35 -04:00
parent aa755ffc34
commit c872f3e245

View File

@@ -1,5 +1,6 @@
import * as React from "react";
import Model from "~/models/base/Model";
import useCurrentUser from "./useCurrentUser";
import useStores from "./useStores";
/**
@@ -11,6 +12,7 @@ import useStores from "./useStores";
*/
export default function usePolicy(entity?: string | Model | null) {
const { policies } = useStores();
const user = useCurrentUser({ rejectOnEmpty: false });
const entityId = entity
? typeof entity === "string"
? entity
@@ -24,12 +26,13 @@ export default function usePolicy(entity?: string | Model | null) {
!entity.isNew &&
!entity.isSaving
) {
// The policy for this model is missing, reload relationships for this model.
if (!policies.get(entity.id)) {
// The policy for this model is missing and we have an authenticated session, attempt to
// reload relationships for this model.
if (!policies.get(entity.id) && user) {
void entity.loadRelations();
}
}
}, [policies, entity]);
}, [policies, user, entity]);
return policies.abilities(entityId);
}