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