From c872f3e245214c9a5fdb2e668108e9d07af46a60 Mon Sep 17 00:00:00 2001 From: Tom Moor Date: Sat, 18 May 2024 11:29:35 -0400 Subject: [PATCH] fix: Unneccessary network requests on shared documents --- app/hooks/usePolicy.ts | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/app/hooks/usePolicy.ts b/app/hooks/usePolicy.ts index dd8e565c9..86052f7c3 100644 --- a/app/hooks/usePolicy.ts +++ b/app/hooks/usePolicy.ts @@ -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); }