Skip to content

Open source alternative to Semrush and Ahrefs

Show a way to get credits on every out-of-credits error (EVE-437) (#993)

Carter Smithb28f3b9b4a13
diff --git a/src/client/features/ai-visibility/AiResearchSetupGate.tsx b/src/client/features/ai-visibility/AiResearchSetupGate.tsx
index 7d0349cf..002042e3 100644
--- a/src/client/features/ai-visibility/AiResearchSetupGate.tsx
+++ b/src/client/features/ai-visibility/AiResearchSetupGate.tsx
@@ -6,6 +6,8 @@ import { Button } from "@/client/components/ui/button";
import { Card, CardContent } from "@/client/components/ui/card";
import { Field, FieldLabel } from "@/client/components/ui/field";
import { Textarea } from "@/client/components/ui/textarea";
+import { InsufficientCreditsError } from "@/client/features/billing/InsufficientCreditsError";
+import { getErrorCode } from "@/client/lib/error-messages";
import { projectsQueryOptions } from "@/client/features/projects/projectQueries";
import { WebsiteResearchProgress } from "@/client/features/projects/WebsiteResearchProgress";
import { WebsiteSetupReview } from "@/client/features/projects/WebsiteSetupReview";
@@ -174,14 +176,18 @@ export function AiResearchSetupGate({
monitor.
</p>
</div>
- {failed && (
- <Alert variant="destructive">
- <AlertDescription>
- {unreadable
- ? "We couldn’t read this website. Its bot protection may be blocking us. Describe your business below and try again, or check the URL in project settings."
- : "We couldn’t finish setting up AI visibility. Check your available usage credits, then try again."}
- </AlertDescription>
- </Alert>
+ {getErrorCode(start.error) === "INSUFFICIENT_CREDITS" ? (
+ <InsufficientCreditsError title="We couldn’t set up AI visibility" />
+ ) : (
+ failed && (
+ <Alert variant="destructive">
+ <AlertDescription>
+ {unreadable
+ ? "We couldn’t read this website. Its bot protection may be blocking us. Describe your business below and try again, or check the URL in project settings."
+ : "We couldn’t finish setting up AI visibility. Try again."}
+ </AlertDescription>
+ </Alert>
+ )
)}
{unreadable && (
<Field>
diff --git a/src/client/features/audit/launch/LaunchFormCard.tsx b/src/client/features/audit/launch/LaunchFormCard.tsx
index 7eb0df42..8c42f9bd 100644
--- a/src/client/features/audit/launch/LaunchFormCard.tsx
+++ b/src/client/features/audit/launch/LaunchFormCard.tsx
@@ -15,6 +15,7 @@ import {
FieldLabel,
} from "@/client/components/ui/field";
import { Input } from "@/client/components/ui/input";
+import { InsufficientCreditsError } from "@/client/features/billing/InsufficientCreditsError";
import { Switch } from "@/client/components/ui/switch";
import {
clampMaxPagesInput,
@@ -33,6 +34,7 @@ type Props = {
maxPagesLimit: number;
paidMaxPagesLimit: number;
canRenderJavaScript?: boolean;
+ outOfCredits?: boolean;
};
export function LaunchFormCard({
@@ -41,6 +43,7 @@ export function LaunchFormCard({
maxPagesLimit,
paidMaxPagesLimit,
canRenderJavaScript,
+ outOfCredits,
}: Props) {
return (
<Card>
@@ -110,7 +113,7 @@ export function LaunchFormCard({
</div>
</form>
- <LaunchErrors launchForm={launchForm} />
+ <LaunchErrors launchForm={launchForm} outOfCredits={outOfCredits} />
</CardContent>
</Card>
);
@@ -264,7 +267,10 @@ function RenderingOptions({
);
}
-function LaunchErrors({ launchForm }: Pick<Props, "launchForm">) {
+function LaunchErrors({
+ launchForm,
+ outOfCredits,
+}: Pick<Props, "launchForm" | "outOfCredits">) {
return (
<div className="space-y-2">
<launchForm.Field name="url">
@@ -279,6 +285,8 @@ function LaunchErrors({ launchForm }: Pick<Props, "launchForm">) {
{(submitError) => {
const errorMessage = getFormError(submitError);
+ if (errorMessage && outOfCredits)
+ return <InsufficientCreditsError title={errorMessage} />;
return errorMessage ? (
<Alert variant="destructive">
<AlertCircle />
diff --git a/src/client/features/audit/launch/LaunchView.tsx b/src/client/features/audit/launch/LaunchView.tsx
index c88021d0..ff97e815 100644
--- a/src/client/features/audit/launch/LaunchView.tsx
+++ b/src/client/features/audit/launch/LaunchView.tsx
@@ -37,6 +37,7 @@ export function LaunchView({
maxPagesLimit={controller.maxPagesLimit}
paidMaxPagesLimit={controller.paidMaxPagesLimit}
canRenderJavaScript={controller.canRenderJavaScript}
+ outOfCredits={controller.outOfCredits}
/>
<AuditHistorySection
diff --git a/src/client/features/audit/launch/useLaunchController.ts b/src/client/features/audit/launch/useLaunchController.ts
index be604e1e..d1cde9d2 100644
--- a/src/client/features/audit/launch/useLaunchController.ts
+++ b/src/client/features/audit/launch/useLaunchController.ts
@@ -67,6 +67,7 @@ export function useLaunchController({
const { startMutation, deleteMutation } = useLaunchMutations({ projectId });
// Pages of a large crawl waiting for the user to confirm it.
const [largeCrawlPages, setLargeCrawlPages] = useState<number | null>(null);
+ const [outOfCredits, setOutOfCredits] = useState(false);
const largeCrawlConfirmed = useRef(false);
const launchForm = useForm({
@@ -105,11 +106,13 @@ export function useLaunchController({
} catch (error) {
// Starting an audit refuses for credits only when the rendering hold
// does not fit, so the form can say how much the audit needs.
- const message =
+ const creditsShort =
value.renderJavaScript &&
- getErrorCode(error) === "INSUFFICIENT_CREDITS"
- ? renderingCreditsNeededText(effectiveMaxPages)
- : getStandardErrorMessage(error, "Failed to start audit");
+ getErrorCode(error) === "INSUFFICIENT_CREDITS";
+ setOutOfCredits(creditsShort);
+ const message = creditsShort
+ ? renderingCreditsNeededText(effectiveMaxPages)
+ : getStandardErrorMessage(error, "Failed to start audit");
formApi.setErrorMap({
onSubmit: createFormValidationErrors({ form: message }),
});
@@ -135,6 +138,7 @@ export function useLaunchController({
commitMaxPagesInput: () => commitMaxPagesInput(launchForm, maxPagesLimit),
deleteAudit: (auditId: string) => deleteMutation.mutate(auditId),
largeCrawlPages,
+ outOfCredits,
cancelLargeCrawl: () => setLargeCrawlPages(null),
confirmLargeCrawl: () => {
setLargeCrawlPages(null);
diff --git a/src/client/features/billing/InsufficientCreditsError.tsx b/src/client/features/billing/InsufficientCreditsError.tsx
index 5098e9a5..5dd83ef8 100644
--- a/src/client/features/billing/InsufficientCreditsError.tsx
+++ b/src/client/features/billing/InsufficientCreditsError.tsx
@@ -23,6 +23,23 @@ export function InsufficientCreditsError({
variant?: "inline" | "card" | "page";
title?: string;
}) {
+ const { message, action } = useCreditsOffer();
+ return (
+ <ErrorState
+ variant={variant}
+ title={title}
+ message={message}
+ action={action}
+ />
+ );
+}
+
+/** The card's button alone, for a spot too small for the card. */
+export function GetCreditsButton() {
+ return useCreditsOffer().action;
+}
+
+function useCreditsOffer() {
const { accountQuery, isFreePlan, refillDate } = useCreditBalance();
let message: string;
@@ -49,19 +66,19 @@ export function InsufficientCreditsError({
);
}
- return (
- <ErrorState
- variant={variant}
- title={title}
- message={message}
- action={action}
- />
- );
+ return { message, action };
}
function CreditsButton({ link, label }: { link: ReactElement; label: string }) {
return (
- <Button variant="outline" size="sm" nativeButton={false} render={link}>
+ // Its own text color: a red error around it would otherwise tint it.
+ <Button
+ variant="outline"
+ size="sm"
+ className="text-foreground"
+ nativeButton={false}
+ render={link}
+ >
{label}
</Button>
);
diff --git a/src/client/features/keywords/components/SerpAnalysisCard.tsx b/src/client/features/keywords/components/SerpAnalysisCard.tsx
index 33ddd249..3da13922 100644
--- a/src/client/features/keywords/components/SerpAnalysisCard.tsx
+++ b/src/client/features/keywords/components/SerpAnalysisCard.tsx
@@ -2,6 +2,7 @@ import { useMemo } from "react";
import { createColumnHelper } from "@tanstack/react-table";
import { ExternalLink, Globe } from "lucide-react";
import { ErrorState } from "@/client/components/ErrorState";
+import { GetCreditsButton } from "@/client/features/billing/InsufficientCreditsError";
import { ExportMenu } from "@/client/components/ExportMenu";
import { Button } from "@/client/components/ui/button";
import { DataTable, useDataTable } from "@/client/components/table/DataTable";
@@ -62,6 +63,7 @@ export function SerpAnalysisCard({
loadingMore,
canLoadMore,
error,
+ outOfCredits,
onRetry,
retrying,
deepFetchFailed,
@@ -77,6 +79,7 @@ export function SerpAnalysisCard({
/** Paging past the loaded results can buy a deeper snapshot. */
canLoadMore: boolean;
error?: string | null;
+ outOfCredits: boolean;
onRetry?: () => void;
retrying: boolean;
/** The failure was the deeper crawl, so retrying restores the shallow one. */
@@ -93,11 +96,23 @@ export function SerpAnalysisCard({
const table = useDataTable({ data: pageItems, columns });
const hasItems = !loading && items.length > 0;
- const retryButton = onRetry ? (
- <Button variant="outline" size="sm" pending={retrying} onClick={onRetry}>
- {deepFetchFailed ? "Show top 20" : "Retry"}
- </Button>
- ) : null;
+ // Out of credits, a retry can't help, but "Show top 20" still brings back
+ // the results already paid for.
+ const errorAction = (
+ <>
+ {outOfCredits && <GetCreditsButton />}
+ {onRetry && (deepFetchFailed || !outOfCredits) && (
+ <Button
+ variant="outline"
+ size="sm"
+ pending={retrying}
+ onClick={onRetry}
+ >
+ {deepFetchFailed ? "Show top 20" : "Retry"}
+ </Button>
+ )}
+ </>
+ );
return (
<DataTable
@@ -150,7 +165,7 @@ export function SerpAnalysisCard({
<ErrorState
variant="inline"
message={error}
- action={retryButton}
+ action={errorAction}
/>
</div>
) : null}
@@ -158,7 +173,7 @@ export function SerpAnalysisCard({
}
empty={
error
- ? { kind: "error", title: error, action: retryButton }
+ ? { kind: "error", title: error, action: errorAction }
: {
title: "No SERP details available for this keyword yet.",
description: keyword
diff --git a/src/client/features/keywords/hooks/useKeywordSerpAnalysis.ts b/src/client/features/keywords/hooks/useKeywordSerpAnalysis.ts
index 75dc0276..8a738694 100644
--- a/src/client/features/keywords/hooks/useKeywordSerpAnalysis.ts
+++ b/src/client/features/keywords/hooks/useKeywordSerpAnalysis.ts
@@ -1,6 +1,9 @@
import { useQuery } from "@tanstack/react-query";
import { useCallback, useState } from "react";
-import { getStandardErrorMessage } from "@/client/lib/error-messages";
+import {
+ getErrorCode,
+ getStandardErrorMessage,
+} from "@/client/lib/error-messages";
import { getSerpAnalysis } from "@/serverFunctions/keywords";
const SERP_PAGE_SIZE = 10;
@@ -84,6 +87,8 @@ export function useKeywordSerpAnalysis(
// expensive call. The card offers the shallow snapshot back instead.
const deepFetchFailed =
serpQuery.isError && requestedDepth === SERP_DEEP_DEPTH;
+ const serpOutOfCredits =
+ getErrorCode(serpQuery.error) === "INSUFFICIENT_CREDITS";
// Retrying a failed deep crawl re-buys the expensive call that just timed
// out. Drop back to the shallow snapshot instead: it's still cached, so the
@@ -141,6 +146,7 @@ export function useKeywordSerpAnalysis(
canLoadMoreSerp,
deepFetchFailed,
serpError,
+ serpOutOfCredits,
// `isLoading` stays false while a failed query refetches.
serpRetrying: serpQuery.isFetching,
};
diff --git a/src/client/features/keywords/page/KeywordResearchResults.tsx b/src/client/features/keywords/page/KeywordResearchResults.tsx
index 2d058647..a9feda68 100644
--- a/src/client/features/keywords/page/KeywordResearchResults.tsx
+++ b/src/client/features/keywords/page/KeywordResearchResults.tsx
@@ -295,6 +295,7 @@ function SerpPanel({ controller }: Props) {
loadingMore={controller.serpLoadingMore}
canLoadMore={controller.canLoadMoreSerp}
error={controller.serpError}
+ outOfCredits={controller.serpOutOfCredits}
onRetry={controller.retrySerp}
retrying={controller.serpRetrying}
deepFetchFailed={controller.deepFetchFailed}
diff --git a/src/client/features/keywords/state/useKeywordResearchController.ts b/src/client/features/keywords/state/useKeywordResearchController.ts
index 27fb69af..9fdf90da 100644
--- a/src/client/features/keywords/state/useKeywordResearchController.ts
+++ b/src/client/features/keywords/state/useKeywordResearchController.ts
@@ -96,6 +96,7 @@ export function useKeywordResearchController(
deepFetchFailed,
retrySerp,
serpError,
+ serpOutOfCredits,
serpRetrying,
} = useKeywordSerpAnalysis(input.projectId, locationCode, input.locationName);
@@ -295,6 +296,7 @@ export function useKeywordResearchController(
canLoadMoreSerp,
deepFetchFailed,
serpError,
+ serpOutOfCredits,
serpRetrying,
serpLoading,
serpLoadingMore,
diff --git a/src/client/features/projects/ProjectWebsiteSetup.tsx b/src/client/features/projects/ProjectWebsiteSetup.tsx
index b0becd67..8ff704f0 100644
--- a/src/client/features/projects/ProjectWebsiteSetup.tsx
+++ b/src/client/features/projects/ProjectWebsiteSetup.tsx
@@ -8,6 +8,8 @@ import { Button } from "@/client/components/ui/button";
import { Input } from "@/client/components/ui/input";
import { Field, FieldLabel } from "@/client/components/ui/field";
import { Alert, AlertDescription } from "@/client/components/ui/alert";
+import { InsufficientCreditsError } from "@/client/features/billing/InsufficientCreditsError";
+import { getErrorCode } from "@/client/lib/error-messages";
import { projectsQueryOptions } from "@/client/features/projects/projectQueries";
import { projectContextQueryKey } from "@/client/features/projects/project-context/shared";
import { getProjectContext } from "@/serverFunctions/projectContext";
@@ -114,13 +116,17 @@ export function ProjectWebsiteSetup({ projectId }: { projectId: string }) {
required
/>
</Field>
- {researchMutation.isError && (
- <Alert variant="destructive">
- <AlertDescription>
- We couldn’t research this website. Check the URL, provider
- settings, and available usage credits, then try again.
- </AlertDescription>
- </Alert>
+ {getErrorCode(researchMutation.error) === "INSUFFICIENT_CREDITS" ? (
+ <InsufficientCreditsError title="We couldn’t research this website" />
+ ) : (
+ researchMutation.isError && (
+ <Alert variant="destructive">
+ <AlertDescription>
+ We couldn’t research this website. Check the URL and provider
+ settings, then try again.
+ </AlertDescription>
+ </Alert>
+ )
)}
<div className="space-y-3">
<Button
diff --git a/src/client/features/rank-tracking/RankRunAlerts.tsx b/src/client/features/rank-tracking/RankRunAlerts.tsx
index e3048aaa..28f825ac 100644
--- a/src/client/features/rank-tracking/RankRunAlerts.tsx
+++ b/src/client/features/rank-tracking/RankRunAlerts.tsx
@@ -1,5 +1,6 @@
import { AlertTriangle } from "lucide-react";
import { Alert, AlertDescription } from "@/client/components/ui/alert";
+import { InsufficientCreditsError } from "@/client/features/billing/InsufficientCreditsError";
import { getStandardErrorMessage } from "@/client/lib/error-messages";
import type { useRankRunPolling } from "./useRankRunPolling";
@@ -22,6 +23,10 @@ export function RankRunAlerts({
);
}
+ if (run.status === "failed" && run.errorMessage === "INSUFFICIENT_CREDITS") {
+ return <InsufficientCreditsError title="Your last rank check didn't run" />;
+ }
+
if (run.status === "failed") {
return (
<Alert variant="destructive">
diff --git a/src/client/features/rank-tracking/RankTrackingDomainDetail.tsx b/src/client/features/rank-tracking/RankTrackingDomainDetail.tsx
index e09eb168..67288d23 100644
--- a/src/client/features/rank-tracking/RankTrackingDomainDetail.tsx
+++ b/src/client/features/rank-tracking/RankTrackingDomainDetail.tsx
@@ -10,10 +10,8 @@ import {
getRankPositionMatrix,
estimateRankCheckCost,
} from "@/serverFunctions/rank-tracking";
-import { AlertTriangle } from "lucide-react";
import { QueryState } from "@/client/components/QueryState";
import { SkeletonCard } from "@/client/components/SkeletonPresets";
-import { Alert, AlertDescription } from "@/client/components/ui/alert";
import { useHostedPlanGate } from "@/client/features/billing/HostedPlanGate";
import { captureClientEvent } from "@/client/lib/posthog";
import { FreePlanAlert } from "./FreePlanAlert";
@@ -238,16 +236,6 @@ export function RankTrackingDomainDetail({
<div className="space-y-3">
<RankTrackingChangesAlert />
- {config.lastSkipReason === "insufficient_credits" && (
- <Alert variant="warning">
- <AlertTriangle />
- <AlertDescription className="text-foreground">
- Last scheduled check was skipped due to insufficient credits. Top up
- your balance to resume automatic tracking.
- </AlertDescription>
- </Alert>
- )}
-
<RankRunAlerts run={latestRun} />
<FreePlanAlert visible={planStatus === "free"} />
diff --git a/src/client/features/rank-tracking/RankTrackingDomainList.tsx b/src/client/features/rank-tracking/RankTrackingDomainList.tsx
index c9d74b67..f7a7a043 100644
--- a/src/client/features/rank-tracking/RankTrackingDomainList.tsx
+++ b/src/client/features/rank-tracking/RankTrackingDomainList.tsx
@@ -6,6 +6,7 @@ import { LOCATIONS } from "@/client/features/keywords/locations";
import {
AlertTriangle,
Archive,
+ CircleAlert,
Globe,
Plus,
ChevronRight,
@@ -22,6 +23,7 @@ import {
} from "@/shared/keyword-locations";
import { ConfirmDialog } from "@/client/components/ConfirmDialog";
import { EmptyState } from "@/client/components/EmptyState";
+import { GetCreditsButton } from "@/client/features/billing/InsufficientCreditsError";
import { Button } from "@/client/components/ui/button";
import {
Card,
@@ -280,10 +282,14 @@ function DomainRow({
)}
</p>
{summary.lastSkipReason === "insufficient_credits" && (
- <p className="flex items-center gap-1 text-xs text-warning">
- <AlertTriangle className="size-3" />
- Scheduled check skipped — insufficient credits
- </p>
+ <div className="mt-1 flex items-center gap-2 text-xs text-destructive">
+ <CircleAlert className="size-3" />
+ Scheduled check skipped: out of credits
+ {/* Above the row link, like the archive button. */}
+ <span className="pointer-events-auto relative z-10">
+ <GetCreditsButton />
+ </span>
+ </div>
)}
{summary.lastSkipReason === "plan_required" && (
<p className="flex items-center gap-1 text-xs text-warning">
diff --git a/src/server/workflows/RankCheckWorkflow.ts b/src/server/workflows/RankCheckWorkflow.ts
index 7bbd72dd..14c6d9f9 100644
--- a/src/server/workflows/RankCheckWorkflow.ts
+++ b/src/server/workflows/RankCheckWorkflow.ts
@@ -119,10 +119,8 @@ async function prepareRankCheckKeywords(input: {
(topupCheck.balance?.remaining ?? 0);
if (available < costCredits) {
await sendCreditsExhaustedEvent(input.billingCustomer);
- throw new AppError(
- "INSUFFICIENT_CREDITS",
- "Insufficient credits for rank check",
- );
+ // The code alone, so the run's saved error shows the credits card.
+ throw new AppError("INSUFFICIENT_CREDITS");
}
}