@cogito.ai/cli 0.4.3 → 0.4.4

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (45) hide show
  1. package/dist/index.js +1 -1
  2. package/dist/templates/web-nextjs/apps/docs/.source/browser.ts +8 -18
  3. package/dist/templates/web-nextjs/apps/docs/.source/dynamic.ts +5 -11
  4. package/dist/templates/web-nextjs/apps/docs/.source/server.ts +17 -37
  5. package/dist/templates/web-nextjs/apps/web/.env.example +35 -0
  6. package/dist/templates/web-nextjs/apps/web/messages/en.json +70 -0
  7. package/dist/templates/web-nextjs/apps/web/messages/zh.json +71 -1
  8. package/dist/templates/web-nextjs/apps/web/next-env.d.ts +1 -1
  9. package/dist/templates/web-nextjs/apps/web/package.json +4 -0
  10. package/dist/templates/web-nextjs/apps/web/src/app/[locale]/(auth)/forgot-password/page.tsx +4 -10
  11. package/dist/templates/web-nextjs/apps/web/src/app/[locale]/(protected)/payment/[paymentId]/page.tsx +88 -0
  12. package/dist/templates/web-nextjs/apps/web/src/app/[locale]/(protected)/payment/checkout/page.tsx +170 -0
  13. package/dist/templates/web-nextjs/apps/web/src/app/[locale]/(protected)/pricing/page.tsx +120 -0
  14. package/dist/templates/web-nextjs/apps/web/src/app/[locale]/(protected)/settings/layout.tsx +48 -1
  15. package/dist/templates/web-nextjs/apps/web/src/app/[locale]/(protected)/settings/subscription/page.tsx +128 -0
  16. package/dist/templates/web-nextjs/apps/web/src/app/[locale]/layout.tsx +1 -1
  17. package/dist/templates/web-nextjs/apps/web/src/app/api/payments/alipay/create/route.ts +43 -0
  18. package/dist/templates/web-nextjs/apps/web/src/app/api/payments/alipay/notify/route.ts +105 -0
  19. package/dist/templates/web-nextjs/apps/web/src/app/api/payments/alipay/query/route.ts +54 -0
  20. package/dist/templates/web-nextjs/apps/web/src/app/api/payments/alipay/return/route.ts +20 -0
  21. package/dist/templates/web-nextjs/apps/web/src/app/api/payments/create/route.ts +52 -0
  22. package/dist/templates/web-nextjs/apps/web/src/app/api/payments/wechat/create/route.ts +58 -0
  23. package/dist/templates/web-nextjs/apps/web/src/app/api/payments/wechat/notify/route.ts +92 -0
  24. package/dist/templates/web-nextjs/apps/web/src/app/api/payments/wechat/query/route.ts +54 -0
  25. package/dist/templates/web-nextjs/apps/web/src/features/subscription/alipay/client.ts +36 -0
  26. package/dist/templates/web-nextjs/apps/web/src/features/subscription/alipay/server.ts +83 -0
  27. package/dist/templates/web-nextjs/apps/web/src/features/subscription/components/index.ts +4 -0
  28. package/dist/templates/web-nextjs/apps/web/src/features/subscription/components/pro-badge.tsx +9 -0
  29. package/dist/templates/web-nextjs/apps/web/src/features/subscription/components/pro-feature-comparison.tsx +70 -0
  30. package/dist/templates/web-nextjs/apps/web/src/features/subscription/components/quota-warning-banner.tsx +37 -0
  31. package/dist/templates/web-nextjs/apps/web/src/features/subscription/components/upgrade-button.tsx +42 -0
  32. package/dist/templates/web-nextjs/apps/web/src/features/subscription/hooks.ts +141 -0
  33. package/dist/templates/web-nextjs/apps/web/src/features/subscription/payment-helpers.ts +27 -0
  34. package/dist/templates/web-nextjs/apps/web/src/features/subscription/payment-service.ts +56 -0
  35. package/dist/templates/web-nextjs/apps/web/src/features/subscription/service.ts +55 -0
  36. package/dist/templates/web-nextjs/apps/web/src/features/subscription/types.ts +73 -0
  37. package/dist/templates/web-nextjs/apps/web/src/features/subscription/wechat/client.ts +33 -0
  38. package/dist/templates/web-nextjs/apps/web/src/features/subscription/wechat/server.ts +147 -0
  39. package/dist/templates/web-nextjs/apps/web/src/lib/supabase/admin.ts +23 -0
  40. package/dist/templates/web-nextjs/apps/web/src/lib/wechat-pay/client.ts +66 -0
  41. package/dist/templates/web-nextjs/apps/web/src/lib/wechat-pay/crypto.ts +99 -0
  42. package/dist/templates/web-nextjs/apps/web/src/lib/wechat-pay/types.ts +10 -0
  43. package/dist/templates/web-nextjs/pnpm-lock.yaml +319 -0
  44. package/dist/templates/web-nextjs/supabase/migrations/20250608_add_subscription_tables.sql +125 -0
  45. package/package.json +1 -1
@@ -0,0 +1,20 @@
1
+ import { NextRequest, NextResponse } from 'next/server'
2
+ import { getPaymentByOrderNo } from '@/features/subscription/payment-service'
3
+
4
+ export async function GET(req: NextRequest) {
5
+ const url = new URL(req.url)
6
+ const outTradeNo = url.searchParams.get('out_trade_no')
7
+
8
+ if (!outTradeNo) {
9
+ return NextResponse.redirect(new URL('/pricing', req.url))
10
+ }
11
+
12
+ // Lookup payment by order_no to get the numeric ID for the status page
13
+ const payment = await getPaymentByOrderNo(outTradeNo)
14
+ if (!payment) {
15
+ return NextResponse.redirect(new URL('/pricing', req.url))
16
+ }
17
+
18
+ // Redirect to payment status page using the numeric payment ID
19
+ return NextResponse.redirect(new URL(`/payment/${payment.id}`, req.url))
20
+ }
@@ -0,0 +1,52 @@
1
+ import { NextRequest, NextResponse } from 'next/server'
2
+ import { createPaymentRecord, getPaymentByOrderNo } from '@/features/subscription/payment-service'
3
+ import { getServerClient } from '@/infra/db/client'
4
+ import { generateOrderNumber } from '@/features/subscription/payment-helpers'
5
+
6
+ export async function POST(req: NextRequest) {
7
+ try {
8
+ const supabase = await getServerClient()
9
+ const {
10
+ data: { user },
11
+ } = await supabase.auth.getUser()
12
+
13
+ if (!user) {
14
+ return NextResponse.json({ error: 'Unauthorized' }, { status: 401 })
15
+ }
16
+
17
+ const body = (await req.json()) as {
18
+ planId: number
19
+ amount: number
20
+ paymentMethod: string
21
+ billingCycle: string
22
+ }
23
+
24
+ if (!body.planId || !body.amount || !body.paymentMethod || !body.billingCycle) {
25
+ return NextResponse.json({ error: 'Missing required fields' }, { status: 400 })
26
+ }
27
+
28
+ const orderNo = generateOrderNumber()
29
+
30
+ const payment = await createPaymentRecord({
31
+ user_id: user.id,
32
+ order_no: orderNo,
33
+ amount: body.amount,
34
+ currency: 'CNY',
35
+ payment_method: body.paymentMethod as 'alipay' | 'wechat',
36
+ metadata: {
37
+ plan_id: body.planId,
38
+ billing_cycle: body.billingCycle,
39
+ },
40
+ })
41
+
42
+ return NextResponse.json({
43
+ id: payment.id,
44
+ orderNo: payment.order_no,
45
+ amount: payment.amount,
46
+ status: payment.status,
47
+ })
48
+ } catch (error) {
49
+ console.error('Payment creation error:', error)
50
+ return NextResponse.json({ error: 'Failed to create payment' }, { status: 500 })
51
+ }
52
+ }
@@ -0,0 +1,58 @@
1
+ import { NextRequest, NextResponse } from 'next/server'
2
+ import { createWechatNativePay, createWechatH5Pay } from '@/features/subscription/wechat/server'
3
+ import { getPaymentById } from '@/features/subscription/payment-service'
4
+
5
+ export async function POST(req: NextRequest) {
6
+ try {
7
+ const { paymentId } = (await req.json()) as { paymentId: number }
8
+
9
+ const payment = await getPaymentById(paymentId)
10
+ if (!payment) {
11
+ return NextResponse.json({ error: 'Payment not found' }, { status: 404 })
12
+ }
13
+
14
+ const userAgent = req.headers.get('user-agent') || ''
15
+ const isMobile = /Mobile|Android|iPhone/i.test(userAgent)
16
+
17
+ // In development, always use native (qrcode) even on mobile (D3)
18
+ const forceNative = process.env.NODE_ENV === 'development' && isMobile
19
+
20
+ const notifyUrl = `${process.env.NEXT_PUBLIC_APP_URL}/api/payments/wechat/notify`
21
+
22
+ if (!isMobile || forceNative) {
23
+ // Native (PC) - return QR code URL
24
+ const result = await createWechatNativePay({
25
+ description: 'Subscription',
26
+ outTradeNo: payment.order_no,
27
+ notifyUrl,
28
+ amount: { total: payment.amount, currency: 'CNY' },
29
+ })
30
+
31
+ if (forceNative) {
32
+ return NextResponse.json({
33
+ type: 'qrcode',
34
+ codeUrl: result.code_url,
35
+ warning: 'H5 disabled in dev',
36
+ })
37
+ }
38
+
39
+ return NextResponse.json({ type: 'qrcode', codeUrl: result.code_url })
40
+ } else {
41
+ // H5 (mobile)
42
+ const result = await createWechatH5Pay({
43
+ description: 'Subscription',
44
+ outTradeNo: payment.order_no,
45
+ notifyUrl,
46
+ amount: { total: payment.amount, currency: 'CNY' },
47
+ sceneInfo: {
48
+ payer_client_ip: req.headers.get('x-forwarded-for')?.split(',')[0]?.trim() || '127.0.0.1',
49
+ },
50
+ })
51
+
52
+ return NextResponse.json({ type: 'redirect', h5Url: result.h5_url })
53
+ }
54
+ } catch (error) {
55
+ console.error('WeChat create error:', error)
56
+ return NextResponse.json({ error: 'Failed to create WeChat payment' }, { status: 500 })
57
+ }
58
+ }
@@ -0,0 +1,92 @@
1
+ import { NextRequest, NextResponse } from 'next/server'
2
+ import { verifyAndDecryptNotify } from '@/features/subscription/wechat/server'
3
+ import { createAdminClient } from '@/lib/supabase/admin'
4
+ import { getPaymentByOrderNo } from '@/features/subscription/payment-service'
5
+
6
+ export async function POST(req: NextRequest) {
7
+ try {
8
+ const rawBody = await req.text()
9
+ const headers: Record<string, string | undefined> = {
10
+ 'wechatpay-timestamp': req.headers.get('wechatpay-timestamp') || undefined,
11
+ 'wechatpay-nonce': req.headers.get('wechatpay-nonce') || undefined,
12
+ 'wechatpay-signature': req.headers.get('wechatpay-signature') || undefined,
13
+ 'wechatpay-serial': req.headers.get('wechatpay-serial') || undefined,
14
+ }
15
+
16
+ const result = await verifyAndDecryptNotify(headers, rawBody)
17
+
18
+ if (result.tradeState !== 'SUCCESS') {
19
+ return NextResponse.json({ code: 'SUCCESS', message: '成功' })
20
+ }
21
+
22
+ const payment = await getPaymentByOrderNo(result.outTradeNo)
23
+ if (!payment) {
24
+ console.warn(`WeChat notify: payment not found for order ${result.outTradeNo}`)
25
+ return NextResponse.json({ code: 'SUCCESS', message: '成功' })
26
+ }
27
+
28
+ // Validate mch_id matches our configured merchant
29
+ if (result.mchId !== process.env.WECHAT_PAY_MCH_ID) {
30
+ console.warn(`WeChat notify: mch_id mismatch. received=${result.mchId}`)
31
+ return NextResponse.json({ code: 'FAIL', message: 'mch_id mismatch' }, { status: 400 })
32
+ }
33
+
34
+ // Validate appid matches our configured app
35
+ if (result.appId !== process.env.WECHAT_PAY_APP_ID) {
36
+ console.warn(`WeChat notify: appid mismatch. received=${result.appId}`)
37
+ return NextResponse.json({ code: 'FAIL', message: 'appid mismatch' }, { status: 400 })
38
+ }
39
+
40
+ // Validate amount matches the local order
41
+ if (result.amount !== payment.amount) {
42
+ console.warn(
43
+ `WeChat notify: amount mismatch. order=${result.outTradeNo}, expected=${payment.amount}, received=${result.amount}`,
44
+ )
45
+ return NextResponse.json({ code: 'FAIL', message: 'amount mismatch' }, { status: 400 })
46
+ }
47
+
48
+ // Idempotency
49
+ if (payment.status === 'paid') {
50
+ return NextResponse.json({ code: 'SUCCESS', message: '成功' })
51
+ }
52
+
53
+ const supabase = createAdminClient()
54
+
55
+ // Update payment with provider_trade_no and conditional update
56
+ const { error: updateError } = await supabase
57
+ .from('payments')
58
+ .update({
59
+ status: 'paid',
60
+ provider_trade_no: result.transactionId,
61
+ paid_at: new Date().toISOString(),
62
+ })
63
+ .eq('order_no', result.outTradeNo)
64
+ .eq('status', 'pending')
65
+
66
+ if (updateError) {
67
+ console.error('WeChat notify: failed to update payment', updateError)
68
+ return NextResponse.json({ code: 'FAIL', message: 'update failed' }, { status: 500 })
69
+ }
70
+
71
+ // Update subscription
72
+ if (payment.subscription_id) {
73
+ const now = new Date()
74
+ const nextMonth = new Date(now)
75
+ nextMonth.setMonth(nextMonth.getMonth() + 1)
76
+
77
+ await supabase
78
+ .from('user_subscriptions')
79
+ .update({
80
+ status: 'active',
81
+ current_period_start: now.toISOString(),
82
+ current_period_end: nextMonth.toISOString(),
83
+ })
84
+ .eq('id', payment.subscription_id)
85
+ }
86
+
87
+ return NextResponse.json({ code: 'SUCCESS', message: '成功' })
88
+ } catch (error) {
89
+ console.error('WeChat notify error:', error)
90
+ return NextResponse.json({ code: 'FAIL', message: (error as Error).message }, { status: 500 })
91
+ }
92
+ }
@@ -0,0 +1,54 @@
1
+ import { NextRequest, NextResponse } from 'next/server'
2
+ import { queryWechatOrder } from '@/features/subscription/wechat/server'
3
+ import { getPaymentByOrderNo } from '@/features/subscription/payment-service'
4
+ import { getServerClient } from '@/infra/db/client'
5
+
6
+ function normalizeWechatStatus(tradeState: string): string {
7
+ switch (tradeState) {
8
+ case 'SUCCESS':
9
+ return 'paid'
10
+ case 'CLOSED':
11
+ case 'REVOKED':
12
+ return 'failed'
13
+ case 'NOTPAY':
14
+ case 'USERPAYING':
15
+ default:
16
+ return 'pending'
17
+ }
18
+ }
19
+
20
+ export async function POST(req: NextRequest) {
21
+ try {
22
+ const supabase = await getServerClient()
23
+ const {
24
+ data: { user },
25
+ } = await supabase.auth.getUser()
26
+
27
+ if (!user) {
28
+ return NextResponse.json({ error: 'Unauthorized' }, { status: 401 })
29
+ }
30
+
31
+ const { outTradeNo } = (await req.json()) as { outTradeNo: string }
32
+
33
+ // Verify order ownership
34
+ const payment = await getPaymentByOrderNo(outTradeNo)
35
+ if (!payment) {
36
+ return NextResponse.json({ error: 'Payment not found' }, { status: 404 })
37
+ }
38
+
39
+ if (payment.user_id !== user.id) {
40
+ return NextResponse.json({ error: 'Forbidden' }, { status: 403 })
41
+ }
42
+
43
+ const result = await queryWechatOrder(outTradeNo)
44
+ const normalizedStatus = normalizeWechatStatus(result.trade_state)
45
+
46
+ return NextResponse.json({
47
+ status: normalizedStatus,
48
+ tradeState: result.trade_state,
49
+ })
50
+ } catch (error) {
51
+ console.error('WeChat query error:', error)
52
+ return NextResponse.json({ error: 'Failed to query WeChat order' }, { status: 500 })
53
+ }
54
+ }
@@ -0,0 +1,36 @@
1
+ 'use client'
2
+
3
+ interface InitiateAlipayPaymentResult {
4
+ formHtml?: string
5
+ redirectUrl?: string
6
+ }
7
+
8
+ export async function initiateAlipayPayment(paymentId: number, userId: string): Promise<void> {
9
+ const res = await fetch('/api/payments/alipay/create', {
10
+ method: 'POST',
11
+ headers: { 'Content-Type': 'application/json' },
12
+ body: JSON.stringify({ paymentId, userId }),
13
+ })
14
+
15
+ if (!res.ok) {
16
+ throw new Error('Failed to initiate Alipay payment')
17
+ }
18
+
19
+ const data: InitiateAlipayPaymentResult = await res.json()
20
+
21
+ if (data.formHtml) {
22
+ // PC page pay: inject form HTML and auto-submit
23
+ const div = document.createElement('div')
24
+ div.innerHTML = data.formHtml
25
+ document.body.appendChild(div)
26
+ const form = div.querySelector('form')
27
+ if (form) {
28
+ form.submit()
29
+ }
30
+ } else if (data.redirectUrl) {
31
+ // H5 wap pay: redirect
32
+ window.location.href = data.redirectUrl
33
+ } else {
34
+ throw new Error('Unexpected Alipay response')
35
+ }
36
+ }
@@ -0,0 +1,83 @@
1
+ import { AlipaySdk } from 'alipay-sdk'
2
+
3
+ const ALIPAY_APP_ID = process.env.ALIPAY_APP_ID
4
+ const ALIPAY_PRIVATE_KEY = process.env.ALIPAY_PRIVATE_KEY
5
+ const ALIPAY_PUBLIC_KEY = process.env.ALIPAY_PUBLIC_KEY
6
+ const ALIPAY_GATEWAY = process.env.ALIPAY_GATEWAY || 'https://openapi.alipay.com/gateway.do'
7
+
8
+ export interface AlipayPayParams {
9
+ outTradeNo: string
10
+ totalAmount: string
11
+ subject: string
12
+ body?: string
13
+ returnUrl: string
14
+ notifyUrl: string
15
+ }
16
+
17
+ export function getAlipaySDK(): AlipaySdk {
18
+ if (!ALIPAY_APP_ID || !ALIPAY_PRIVATE_KEY || !ALIPAY_PUBLIC_KEY) {
19
+ throw new Error(
20
+ 'Missing Alipay credentials. Ensure ALIPAY_APP_ID, ALIPAY_PRIVATE_KEY, and ALIPAY_PUBLIC_KEY are set.',
21
+ )
22
+ }
23
+
24
+ return new AlipaySdk({
25
+ appId: ALIPAY_APP_ID,
26
+ privateKey: ALIPAY_PRIVATE_KEY,
27
+ signType: 'RSA2',
28
+ alipayPublicKey: ALIPAY_PUBLIC_KEY,
29
+ gateway: ALIPAY_GATEWAY,
30
+ camelcase: true,
31
+ })
32
+ }
33
+
34
+ export async function createAlipayPagePay(params: AlipayPayParams) {
35
+ const alipay = getAlipaySDK()
36
+ const result = await alipay.exec('alipay.trade.page.pay', {
37
+ notify_url: params.notifyUrl,
38
+ return_url: params.returnUrl,
39
+ bizContent: {
40
+ out_trade_no: params.outTradeNo,
41
+ total_amount: params.totalAmount,
42
+ subject: params.subject,
43
+ body: params.body,
44
+ product_code: 'FAST_INSTANT_TRADE_PAY',
45
+ },
46
+ })
47
+
48
+ return { formHtml: result }
49
+ }
50
+
51
+ export async function createAlipayWapPay(params: AlipayPayParams) {
52
+ const alipay = getAlipaySDK()
53
+ const result = await alipay.exec('alipay.trade.wap.pay', {
54
+ notify_url: params.notifyUrl,
55
+ return_url: params.returnUrl,
56
+ bizContent: {
57
+ out_trade_no: params.outTradeNo,
58
+ total_amount: params.totalAmount,
59
+ subject: params.subject,
60
+ body: params.body,
61
+ product_code: 'QUICK_WAP_WAY',
62
+ },
63
+ })
64
+
65
+ return { redirectUrl: (result as unknown as { url: string }).url }
66
+ }
67
+
68
+ export async function queryAlipayOrder(outTradeNo: string) {
69
+ const alipay = getAlipaySDK()
70
+ const result = await alipay.exec('alipay.trade.query', {
71
+ bizContent: { out_trade_no: outTradeNo },
72
+ })
73
+
74
+ return result as unknown as {
75
+ tradeStatus: string
76
+ code: string
77
+ }
78
+ }
79
+
80
+ export async function verifyAlipayNotify(formData: Record<string, unknown>) {
81
+ const alipay = getAlipaySDK()
82
+ return alipay.checkNotifySign(formData)
83
+ }
@@ -0,0 +1,4 @@
1
+ export { UpgradeButton } from './upgrade-button'
2
+ export { ProBadge } from './pro-badge'
3
+ export { ProFeatureComparison } from './pro-feature-comparison'
4
+ export { QuotaWarningBanner } from './quota-warning-banner'
@@ -0,0 +1,9 @@
1
+ import { Badge } from '@/components/ui/badge'
2
+
3
+ export function ProBadge() {
4
+ return (
5
+ <Badge variant="default" className="bg-gradient-to-r from-amber-500 to-orange-500 text-white">
6
+ PRO
7
+ </Badge>
8
+ )
9
+ }
@@ -0,0 +1,70 @@
1
+ 'use client'
2
+
3
+ import { useTranslations } from 'next-intl'
4
+ import { Check, X } from 'lucide-react'
5
+
6
+ interface Feature {
7
+ name: string
8
+ free: boolean | string
9
+ pro: boolean | string
10
+ }
11
+
12
+ interface ProFeatureComparisonProps {
13
+ features?: Feature[]
14
+ }
15
+
16
+ export function ProFeatureComparison({ features }: ProFeatureComparisonProps) {
17
+ const t = useTranslations('subscription')
18
+
19
+ const defaultFeatures: Feature[] = [
20
+ { name: t('featureProjects'), free: '3', pro: '50' },
21
+ { name: t('featureApiCalls'), free: '1,000', pro: '100,000' },
22
+ { name: t('featureSupport'), free: false, pro: true },
23
+ { name: t('featureAnalytics'), free: false, pro: true },
24
+ ]
25
+
26
+ const displayFeatures = features || defaultFeatures
27
+
28
+ return (
29
+ <div className="w-full overflow-hidden rounded-lg border">
30
+ <table className="w-full text-sm">
31
+ <thead>
32
+ <tr className="border-b bg-muted/50">
33
+ <th className="px-4 py-3 text-left font-medium">{t('feature')}</th>
34
+ <th className="px-4 py-3 text-center font-medium">{t('free')}</th>
35
+ <th className="px-4 py-3 text-center font-medium">{t('pro')}</th>
36
+ </tr>
37
+ </thead>
38
+ <tbody>
39
+ {displayFeatures.map((feat, index) => (
40
+ <tr key={index} className="border-b last:border-0">
41
+ <td className="px-4 py-3 font-medium">{feat.name}</td>
42
+ <td className="px-4 py-3 text-center">
43
+ {typeof feat.free === 'boolean' ? (
44
+ feat.free ? (
45
+ <Check className="mx-auto h-4 w-4 text-green-500" />
46
+ ) : (
47
+ <X className="mx-auto h-4 w-4 text-muted-foreground" />
48
+ )
49
+ ) : (
50
+ <span>{feat.free}</span>
51
+ )}
52
+ </td>
53
+ <td className="px-4 py-3 text-center">
54
+ {typeof feat.pro === 'boolean' ? (
55
+ feat.pro ? (
56
+ <Check className="mx-auto h-4 w-4 text-green-500" />
57
+ ) : (
58
+ <X className="mx-auto h-4 w-4 text-muted-foreground" />
59
+ )
60
+ ) : (
61
+ <span className="font-medium">{feat.pro}</span>
62
+ )}
63
+ </td>
64
+ </tr>
65
+ ))}
66
+ </tbody>
67
+ </table>
68
+ </div>
69
+ )
70
+ }
@@ -0,0 +1,37 @@
1
+ 'use client'
2
+
3
+ import { useRouter } from 'next/navigation'
4
+ import { AlertTriangle } from 'lucide-react'
5
+ import { Button } from '@/components/ui/button'
6
+
7
+ interface QuotaWarningBannerProps {
8
+ quotaName?: string
9
+ currentUsage?: number
10
+ maxQuota?: number
11
+ }
12
+
13
+ export function QuotaWarningBanner({
14
+ quotaName = 'API calls',
15
+ currentUsage = 0,
16
+ maxQuota = 1000,
17
+ }: QuotaWarningBannerProps) {
18
+ const router = useRouter()
19
+ const percentage = (currentUsage / maxQuota) * 100
20
+ const isNearLimit = percentage >= 80
21
+
22
+ if (!isNearLimit) return null
23
+
24
+ return (
25
+ <div className="flex items-center gap-3 rounded-lg border border-amber-200 bg-amber-50 p-4 dark:border-amber-900 dark:bg-amber-950">
26
+ <AlertTriangle className="h-5 w-5 flex-shrink-0 text-amber-600 dark:text-amber-400" />
27
+ <div className="flex-1">
28
+ <p className="text-sm font-medium text-amber-900 dark:text-amber-200">
29
+ You are using {currentUsage} of {maxQuota} {quotaName} ({percentage.toFixed(0)}%)
30
+ </p>
31
+ </div>
32
+ <Button variant="outline" size="sm" onClick={() => router.push('/pricing')}>
33
+ Upgrade
34
+ </Button>
35
+ </div>
36
+ )
37
+ }
@@ -0,0 +1,42 @@
1
+ 'use client'
2
+
3
+ import { useRouter } from 'next/navigation'
4
+ import { Button } from '@/components/ui/button'
5
+ import { Zap } from 'lucide-react'
6
+
7
+ interface UpgradeButtonProps {
8
+ source?: string
9
+ feature?: string
10
+ trigger?: string
11
+ icon?: boolean
12
+ text?: string
13
+ highlight?: boolean
14
+ }
15
+
16
+ export function UpgradeButton({
17
+ source = 'default',
18
+ feature,
19
+ trigger,
20
+ icon = true,
21
+ text = 'Upgrade',
22
+ highlight = false,
23
+ }: UpgradeButtonProps) {
24
+ const router = useRouter()
25
+
26
+ const handleClick = () => {
27
+ const params = new URLSearchParams()
28
+ if (source) params.set('source', source)
29
+ if (feature) params.set('feature', feature)
30
+ if (trigger) params.set('trigger', trigger)
31
+
32
+ const queryString = params.toString()
33
+ router.push(`/pricing${queryString ? `?${queryString}` : ''}`)
34
+ }
35
+
36
+ return (
37
+ <Button onClick={handleClick} variant={highlight ? 'default' : 'outline'} size="sm">
38
+ {icon && <Zap className="mr-1 h-4 w-4" />}
39
+ {text}
40
+ </Button>
41
+ )
42
+ }