import { createClient } from "@/lib/supabase/server";
import { SubmitButton } from "../ui/submit-button";
import { manageSubscription } from "@/lib/actions/billing";
import { PlanComparison, SUBSCRIPTION_PLANS } from "../billing/plan-comparison";
import { isLocalMode } from "@/lib/config";
type Props = {
accountId: string;
returnUrl: string;
}
export default async function AccountBillingStatus({ accountId, returnUrl }: Props) {
// In local development mode, show a simplified component
if (isLocalMode()) {
return (
Billing Status
Running in local development mode - billing features are disabled
Agent usage limits are not enforced in this environment
);
}
const supabaseClient = await createClient();
// Get account subscription and usage data
const { data: subscriptionData } = await supabaseClient
.schema('basejump')
.from('billing_subscriptions')
.select('*')
.eq('account_id', accountId)
.eq('status', 'active')
.limit(1)
.order('created_at', { ascending: false })
.single();
// Get agent runs for this account
// Get the account's threads
const { data: threads } = await supabaseClient
.from('threads')
.select('thread_id')
.eq('account_id', accountId);
const threadIds = threads?.map(t => t.thread_id) || [];
// Get current month usage
const now = new Date();
const startOfMonth = new Date(now.getFullYear(), now.getMonth(), 1);
const isoStartOfMonth = startOfMonth.toISOString();
let totalAgentTime = 0;
let usageDisplay = "No usage this month";
if (threadIds.length > 0) {
const { data: agentRuns } = await supabaseClient
.from('agent_runs')
.select('started_at, completed_at')
.in('thread_id', threadIds)
.gte('started_at', isoStartOfMonth);
if (agentRuns && agentRuns.length > 0) {
const nowTimestamp = now.getTime();
totalAgentTime = agentRuns.reduce((total, run) => {
const startTime = new Date(run.started_at).getTime();
const endTime = run.completed_at
? new Date(run.completed_at).getTime()
: nowTimestamp;
return total + (endTime - startTime) / 1000; // In seconds
}, 0);
// Convert to minutes
const totalMinutes = Math.round(totalAgentTime / 60);
usageDisplay = `${totalMinutes} minutes`;
}
}
const isPlan = (planId?: string) => {
return subscriptionData?.price_id === planId;
};
const planName = isPlan(SUBSCRIPTION_PLANS.FREE)
? "Free"
: isPlan(SUBSCRIPTION_PLANS.PRO)
? "Pro"
: isPlan(SUBSCRIPTION_PLANS.ENTERPRISE)
? "Enterprise"
: "Unknown";
return (
Billing Status
{subscriptionData ? (
<>
Agent Usage This Month
{usageDisplay}
{/* Plans Comparison */}
{/* Manage Subscription Button */}
>
) : (
<>
Current Plan
Free
Agent Usage This Month
{usageDisplay}
{/* Plans Comparison */}
{/* Manage Subscription Button */}
>
)}
)
}