Skip to content

🪢 Open source agent evals & observability: Trace, evaluate, and improve LLM applications with one open platform.

fix(billing): fix react rule of hooks violation (#9477)

Michael Fröhlich4ee723d9ed36
diff --git a/web/src/ee/features/billing/components/BillingDiscountView.tsx b/web/src/ee/features/billing/components/BillingDiscountView.tsx
index 8d9de2d997..494295b7b7 100644
--- a/web/src/ee/features/billing/components/BillingDiscountView.tsx
+++ b/web/src/ee/features/billing/components/BillingDiscountView.tsx
@@ -6,12 +6,6 @@ import { BillingDiscountCodeButton } from "@/src/ee/features/billing/components/
export const BillingDiscountView = () => {
const { organization } = useBillingInformation();
- // Hide promotion code view and button when user is on Hobby Plan
- // Hobby plan users don't have an active subscription ID
- if (!organization?.cloudConfig?.stripe?.activeSubscriptionId) {
- return null;
- }
-
const shouldRenderComponent = Boolean(
organization?.cloudConfig?.stripe?.customerId,
);
@@ -37,12 +31,19 @@ export const BillingDiscountView = () => {
}
};
- if (!shouldRenderComponent)
+ // Hide promotion code view and button when user is on Hobby Plan
+ // Hobby plan users don't have an active subscription ID
+ if (!organization?.cloudConfig?.stripe?.activeSubscriptionId) {
+ return null;
+ }
+
+ if (!shouldRenderComponent) {
return (
<div className="flex items-center">
<BillingDiscountCodeButton orgId={organization?.id} />
</div>
);
+ }
return (
<div className="flex flex-col gap-2">
diff --git a/web/src/ee/features/billing/components/BillingSettings.tsx b/web/src/ee/features/billing/components/BillingSettings.tsx
index 747950b065..6eebca60c9 100644
--- a/web/src/ee/features/billing/components/BillingSettings.tsx
+++ b/web/src/ee/features/billing/components/BillingSettings.tsx
@@ -25,9 +25,12 @@ export const BillingSettings = () => {
const entitled = useHasEntitlement("cloud-billing");
const isUsageAlertEntitled = useHasEntitlement("cloud-usage-alerts");
- if (!entitled) return null;
+ // Handle conditional rendering without early returns
+ if (!entitled) {
+ return null;
+ }
- if (!hasAccess)
+ if (!hasAccess) {
return (
<Alert>
<AlertTitle>Access Denied</AlertTitle>
@@ -37,6 +40,7 @@ export const BillingSettings = () => {
</AlertDescription>
</Alert>
);
+ }
return (
<div>
diff --git a/web/src/features/organizations/hooks.ts b/web/src/features/organizations/hooks.ts
index 56d7ec5335..ba1e800d6b 100644
--- a/web/src/features/organizations/hooks.ts
+++ b/web/src/features/organizations/hooks.ts
@@ -15,11 +15,11 @@ export const useQueryOrganization = () => {
export const useOrganization = (organizationId: string | null) => {
const session = useSession();
- if (organizationId === null) return null;
- const organization = session.data?.user?.organizations.find(
- (org) => org.id === organizationId,
- );
+ // Always call hooks first, then handle conditional logic in the return
+ const organization = organizationId
+ ? session.data?.user?.organizations.find((org) => org.id === organizationId)
+ : null;
return organization ?? null;
};
diff --git a/web/src/features/projects/hooks.ts b/web/src/features/projects/hooks.ts
index 2bf12faf22..087d4c6867 100644
--- a/web/src/features/projects/hooks.ts
+++ b/web/src/features/projects/hooks.ts
@@ -10,27 +10,27 @@ export const useQueryProject = () => {
export const useProject = (projectId: string | null) => {
const session = useSession();
- if (projectId === null) return { project: null, organization: null };
- const data = session.data?.user?.organizations
- // map to {project, organization}[]
- .flatMap((org) =>
- org.projects.map((project) => ({ project, organization: org })),
- )
- // find the project with the matching id
- .find(({ project }) => project.id === projectId);
+ // Always call hooks first, then handle conditional logic in the return
+ const data = projectId
+ ? session.data?.user?.organizations
+ // map to {project, organization}[]
+ .flatMap((org) =>
+ org.projects.map((project) => ({ project, organization: org })),
+ )
+ // find the project with the matching id
+ .find(({ project }) => project.id === projectId)
+ : null;
- if (!data)
- return {
- project: null,
- organization: null,
- };
-
- // explicitly destructuring the data object to make it clear what is being returned
- return {
- project: data.project,
- organization: data.organization,
- };
+ return data
+ ? {
+ project: data.project,
+ organization: data.organization,
+ }
+ : {
+ project: null,
+ organization: null,
+ };
};
export const useQueryProjectOrOrganization = () => {