create-nextdevtpl 2.6.0
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.
- package/README.md +29 -0
- package/catalog.json +385 -0
- package/dist/catalog.d.ts +3 -0
- package/dist/catalog.js +112 -0
- package/dist/catalog.js.map +1 -0
- package/dist/cli.d.ts +3 -0
- package/dist/cli.js +115 -0
- package/dist/cli.js.map +1 -0
- package/dist/filesystem.d.ts +10 -0
- package/dist/filesystem.js +86 -0
- package/dist/filesystem.js.map +1 -0
- package/dist/generate.d.ts +5 -0
- package/dist/generate.js +758 -0
- package/dist/generate.js.map +1 -0
- package/dist/index.d.ts +4 -0
- package/dist/index.js +4 -0
- package/dist/index.js.map +1 -0
- package/dist/selection.d.ts +2 -0
- package/dist/selection.js +111 -0
- package/dist/selection.js.map +1 -0
- package/dist/service-templates.d.ts +4 -0
- package/dist/service-templates.js +180 -0
- package/dist/service-templates.js.map +1 -0
- package/dist/types.d.ts +65 -0
- package/dist/types.js +2 -0
- package/dist/types.js.map +1 -0
- package/package.json +54 -0
- package/template/.dockerignore +14 -0
- package/template/.env.example +282 -0
- package/template/.gitattributes +17 -0
- package/template/.node-version +1 -0
- package/template/Dockerfile +45 -0
- package/template/LICENSE +21 -0
- package/template/README.md +267 -0
- package/template/biome.json +68 -0
- package/template/cloudflare/templates/logger.ts +162 -0
- package/template/cloudflare/templates/mail/server.ts +6 -0
- package/template/cloudflare/templates/mail/templates.ts +87 -0
- package/template/cloudflare/templates/mail/utils.ts +69 -0
- package/template/cloudflare/worker.mjs +25 -0
- package/template/components.json +22 -0
- package/template/compose.yaml +74 -0
- package/template/deploy/check-health.mjs +25 -0
- package/template/deploy/docker/README.md +14 -0
- package/template/deploy/server/README.md +24 -0
- package/template/deploy/server/build.sh +24 -0
- package/template/deploy/server/nextdevtpl.service +19 -0
- package/template/deploy/server/start.sh +19 -0
- package/template/deploy/vercel/README.md +16 -0
- package/template/deploy/verify-production-health.mjs +86 -0
- package/template/drizzle.config.ts +47 -0
- package/template/gitignore +62 -0
- package/template/manifest.json +35 -0
- package/template/messages/en.json +766 -0
- package/template/messages/zh.json +762 -0
- package/template/next.config.mjs +28 -0
- package/template/open-next.config.ts +3 -0
- package/template/package.json +125 -0
- package/template/pnpm-lock.yaml +15901 -0
- package/template/postcss.config.mjs +7 -0
- package/template/public/file.svg +1 -0
- package/template/public/globe.svg +1 -0
- package/template/public/logo.svg +6 -0
- package/template/public/next.svg +1 -0
- package/template/public/vercel.svg +1 -0
- package/template/public/window.svg +1 -0
- package/template/sentry.client.config.ts +10 -0
- package/template/sentry.edge.config.ts +10 -0
- package/template/sentry.server.config.ts +10 -0
- package/template/source.config.ts +59 -0
- package/template/src/adapters/ai/anthropic.ts +113 -0
- package/template/src/adapters/ai/index.ts +6 -0
- package/template/src/adapters/ai/openai-compatible.ts +88 -0
- package/template/src/adapters/ai/workers-ai.ts +63 -0
- package/template/src/adapters/index.ts +10 -0
- package/template/src/adapters/jobs/cloudflare-workflows.ts +46 -0
- package/template/src/adapters/jobs/index.ts +5 -0
- package/template/src/adapters/jobs/inngest/adapter.ts +29 -0
- package/template/src/adapters/jobs/inngest/client.ts +8 -0
- package/template/src/adapters/jobs/inngest/functions.ts +17 -0
- package/template/src/adapters/jobs/inngest/handler.ts +6 -0
- package/template/src/adapters/jobs/inngest/index.ts +4 -0
- package/template/src/adapters/mail/cloudflare-email.ts +50 -0
- package/template/src/adapters/mail/disabled.ts +17 -0
- package/template/src/adapters/mail/index.ts +7 -0
- package/template/src/adapters/mail/resend.ts +76 -0
- package/template/src/adapters/mail/smtp.ts +82 -0
- package/template/src/adapters/payment/creem.ts +269 -0
- package/template/src/adapters/payment/index.ts +2 -0
- package/template/src/adapters/payment/stripe.ts +302 -0
- package/template/src/adapters/rate-limit/cloudflare.ts +42 -0
- package/template/src/adapters/rate-limit/index.ts +6 -0
- package/template/src/adapters/rate-limit/noop.ts +27 -0
- package/template/src/adapters/rate-limit/upstash.ts +113 -0
- package/template/src/adapters/registry.ts +228 -0
- package/template/src/adapters/storage/index.ts +3 -0
- package/template/src/adapters/storage/r2-binding.ts +105 -0
- package/template/src/adapters/storage/s3-compatible.ts +149 -0
- package/template/src/app/[locale]/(admin)/admin/layout.tsx +36 -0
- package/template/src/app/[locale]/(admin)/admin/page.tsx +345 -0
- package/template/src/app/[locale]/(admin)/admin/tickets/[id]/page.tsx +261 -0
- package/template/src/app/[locale]/(admin)/admin/tickets/page.tsx +236 -0
- package/template/src/app/[locale]/(admin)/admin/users/page.tsx +557 -0
- package/template/src/app/[locale]/(auth)/forgot-password/page.tsx +9 -0
- package/template/src/app/[locale]/(auth)/layout.tsx +28 -0
- package/template/src/app/[locale]/(auth)/sign-in/page.tsx +9 -0
- package/template/src/app/[locale]/(auth)/sign-up/page.tsx +9 -0
- package/template/src/app/[locale]/(dashboard)/dashboard/credits/buy/buy-credits-view.tsx +170 -0
- package/template/src/app/[locale]/(dashboard)/dashboard/credits/buy/page.tsx +12 -0
- package/template/src/app/[locale]/(dashboard)/dashboard/credits/page.tsx +5 -0
- package/template/src/app/[locale]/(dashboard)/dashboard/loading.tsx +41 -0
- package/template/src/app/[locale]/(dashboard)/dashboard/page.tsx +299 -0
- package/template/src/app/[locale]/(dashboard)/dashboard/settings/page.tsx +38 -0
- package/template/src/app/[locale]/(dashboard)/dashboard/support/[id]/page.tsx +220 -0
- package/template/src/app/[locale]/(dashboard)/dashboard/support/new/page.tsx +183 -0
- package/template/src/app/[locale]/(dashboard)/dashboard/support/page.tsx +145 -0
- package/template/src/app/[locale]/(dashboard)/layout.tsx +35 -0
- package/template/src/app/[locale]/(marketing)/blog/[slug]/page.tsx +178 -0
- package/template/src/app/[locale]/(marketing)/blog/blog-post-card.tsx +95 -0
- package/template/src/app/[locale]/(marketing)/blog/page.tsx +127 -0
- package/template/src/app/[locale]/(marketing)/demo/plan-badges/page.tsx +36 -0
- package/template/src/app/[locale]/(marketing)/layout.tsx +15 -0
- package/template/src/app/[locale]/(marketing)/legal/[slug]/page.tsx +130 -0
- package/template/src/app/[locale]/(marketing)/page.tsx +94 -0
- package/template/src/app/[locale]/(marketing)/pseo/[slug]/page.tsx +68 -0
- package/template/src/app/[locale]/(marketing)/pseo/page.tsx +137 -0
- package/template/src/app/[locale]/docs/[[...slug]]/page.tsx +63 -0
- package/template/src/app/[locale]/docs/layout.tsx +39 -0
- package/template/src/app/[locale]/layout.tsx +80 -0
- package/template/src/app/[locale]/opengraph-image.tsx +20 -0
- package/template/src/app/[locale]/twitter-image.tsx +20 -0
- package/template/src/app/api/auth/[...all]/route.ts +20 -0
- package/template/src/app/api/health/route.ts +45 -0
- package/template/src/app/api/inngest/route.ts +12 -0
- package/template/src/app/api/jobs/credits/expire/route.ts +102 -0
- package/template/src/app/api/search/route.ts +17 -0
- package/template/src/app/api/upload/presigned/route.ts +98 -0
- package/template/src/app/api/webhooks/payment/route.ts +363 -0
- package/template/src/app/globals.css +406 -0
- package/template/src/app/icon.svg +6 -0
- package/template/src/app/image-proxy/[...path]/route.ts +137 -0
- package/template/src/app/layout.tsx +73 -0
- package/template/src/app/robots.ts +29 -0
- package/template/src/app/sitemap.ts +68 -0
- package/template/src/components/motion/reveal.tsx +49 -0
- package/template/src/components/seo/json-ld.tsx +87 -0
- package/template/src/components/seo/og-image-template.tsx +118 -0
- package/template/src/components/ui/accordion.tsx +66 -0
- package/template/src/components/ui/alert-dialog.tsx +195 -0
- package/template/src/components/ui/avatar.tsx +53 -0
- package/template/src/components/ui/badge.tsx +46 -0
- package/template/src/components/ui/button.tsx +65 -0
- package/template/src/components/ui/card.tsx +92 -0
- package/template/src/components/ui/checkbox.tsx +32 -0
- package/template/src/components/ui/collapsible.tsx +33 -0
- package/template/src/components/ui/dialog.tsx +124 -0
- package/template/src/components/ui/dropdown-menu.tsx +257 -0
- package/template/src/components/ui/form.tsx +233 -0
- package/template/src/components/ui/index.ts +2 -0
- package/template/src/components/ui/input.tsx +21 -0
- package/template/src/components/ui/label.tsx +24 -0
- package/template/src/components/ui/navigation-menu.tsx +167 -0
- package/template/src/components/ui/pagination.tsx +125 -0
- package/template/src/components/ui/popover.tsx +48 -0
- package/template/src/components/ui/progress.tsx +27 -0
- package/template/src/components/ui/radio-group.tsx +45 -0
- package/template/src/components/ui/select.tsx +190 -0
- package/template/src/components/ui/separator.tsx +28 -0
- package/template/src/components/ui/sheet.tsx +140 -0
- package/template/src/components/ui/switch.tsx +31 -0
- package/template/src/components/ui/tabs.tsx +75 -0
- package/template/src/components/ui/textarea.tsx +18 -0
- package/template/src/config/index.ts +31 -0
- package/template/src/config/nav.ts +238 -0
- package/template/src/config/payment.ts +282 -0
- package/template/src/config/site.ts +53 -0
- package/template/src/config/subscription-plan.ts +164 -0
- package/template/src/content/blog/en/hello-world.mdx +48 -0
- package/template/src/content/blog/zh/hello-world.mdx +48 -0
- package/template/src/content/blog/zh/nextdevtpl-linuxdo-intro.mdx +98 -0
- package/template/src/content/docs/en/admin-and-support.mdx +65 -0
- package/template/src/content/docs/en/authentication.mdx +71 -0
- package/template/src/content/docs/en/background-jobs.mdx +53 -0
- package/template/src/content/docs/en/configuration.mdx +71 -0
- package/template/src/content/docs/en/credits.mdx +69 -0
- package/template/src/content/docs/en/customization.mdx +95 -0
- package/template/src/content/docs/en/database.mdx +66 -0
- package/template/src/content/docs/en/deployment.mdx +62 -0
- package/template/src/content/docs/en/email.mdx +48 -0
- package/template/src/content/docs/en/index.mdx +48 -0
- package/template/src/content/docs/en/internationalization.mdx +78 -0
- package/template/src/content/docs/en/meta.json +22 -0
- package/template/src/content/docs/en/observability.mdx +69 -0
- package/template/src/content/docs/en/payments.mdx +64 -0
- package/template/src/content/docs/en/project-structure.mdx +82 -0
- package/template/src/content/docs/en/quick-start.mdx +113 -0
- package/template/src/content/docs/en/storage.mdx +51 -0
- package/template/src/content/docs/zh/admin-and-support.mdx +65 -0
- package/template/src/content/docs/zh/authentication.mdx +71 -0
- package/template/src/content/docs/zh/background-jobs.mdx +53 -0
- package/template/src/content/docs/zh/configuration.mdx +70 -0
- package/template/src/content/docs/zh/credits.mdx +69 -0
- package/template/src/content/docs/zh/customization.mdx +95 -0
- package/template/src/content/docs/zh/database.mdx +66 -0
- package/template/src/content/docs/zh/deployment.mdx +61 -0
- package/template/src/content/docs/zh/email.mdx +47 -0
- package/template/src/content/docs/zh/index.mdx +48 -0
- package/template/src/content/docs/zh/internationalization.mdx +78 -0
- package/template/src/content/docs/zh/meta.json +22 -0
- package/template/src/content/docs/zh/observability.mdx +68 -0
- package/template/src/content/docs/zh/payments.mdx +64 -0
- package/template/src/content/docs/zh/project-structure.mdx +82 -0
- package/template/src/content/docs/zh/quick-start.mdx +110 -0
- package/template/src/content/docs/zh/storage.mdx +51 -0
- package/template/src/content/legal/en/cookie-policy.mdx +78 -0
- package/template/src/content/legal/en/privacy.mdx +131 -0
- package/template/src/content/legal/en/terms.mdx +93 -0
- package/template/src/content/legal/zh/cookie-policy.mdx +78 -0
- package/template/src/content/legal/zh/privacy.mdx +131 -0
- package/template/src/content/legal/zh/terms.mdx +92 -0
- package/template/src/core/modules/contract.ts +225 -0
- package/template/src/core/modules/index.ts +1 -0
- package/template/src/core/services/ai.ts +31 -0
- package/template/src/core/services/common.ts +112 -0
- package/template/src/core/services/index.ts +8 -0
- package/template/src/core/services/jobs.ts +24 -0
- package/template/src/core/services/mail.ts +35 -0
- package/template/src/core/services/manifest.ts +51 -0
- package/template/src/core/services/payment.ts +92 -0
- package/template/src/core/services/rate-limit.ts +47 -0
- package/template/src/core/services/storage.ts +42 -0
- package/template/src/db/index.ts +71 -0
- package/template/src/db/schema/auth.ts +67 -0
- package/template/src/db/schema/credits.ts +94 -0
- package/template/src/db/schema/index.ts +5 -0
- package/template/src/db/schema/mail.ts +14 -0
- package/template/src/db/schema/subscription.ts +21 -0
- package/template/src/db/schema/support.ts +56 -0
- package/template/src/db/schema.ts +2 -0
- package/template/src/features/admin/components/admin-sidebar.tsx +213 -0
- package/template/src/features/admin/components/index.ts +2 -0
- package/template/src/features/admin/index.ts +3 -0
- package/template/src/features/admin/manifest.ts +41 -0
- package/template/src/features/analytics/components/analytics.tsx +62 -0
- package/template/src/features/analytics/components/index.ts +2 -0
- package/template/src/features/analytics/index.ts +3 -0
- package/template/src/features/analytics/manifest.ts +13 -0
- package/template/src/features/auth/components/auth-brand-panel.tsx +82 -0
- package/template/src/features/auth/components/auth-error-alert.tsx +28 -0
- package/template/src/features/auth/components/auth-footer.tsx +52 -0
- package/template/src/features/auth/components/auth-logo.tsx +30 -0
- package/template/src/features/auth/components/forgot-password-form.tsx +146 -0
- package/template/src/features/auth/components/index.ts +9 -0
- package/template/src/features/auth/components/sign-in-form.tsx +241 -0
- package/template/src/features/auth/components/sign-up-form.tsx +307 -0
- package/template/src/features/auth/index.ts +3 -0
- package/template/src/features/auth/manifest.ts +30 -0
- package/template/src/features/blog/components/blog-post-item.tsx +72 -0
- package/template/src/features/blog/components/index.ts +1 -0
- package/template/src/features/blog/data/mock-posts.ts +62 -0
- package/template/src/features/blog/index.ts +3 -0
- package/template/src/features/blog/manifest.ts +22 -0
- package/template/src/features/credits/account.ts +76 -0
- package/template/src/features/credits/actions.ts +427 -0
- package/template/src/features/credits/components/credit-balance-badge.tsx +54 -0
- package/template/src/features/credits/components/credit-usage-section.tsx +160 -0
- package/template/src/features/credits/components/index.ts +7 -0
- package/template/src/features/credits/components/transaction-history.tsx +346 -0
- package/template/src/features/credits/config.ts +70 -0
- package/template/src/features/credits/consume.ts +140 -0
- package/template/src/features/credits/core.ts +23 -0
- package/template/src/features/credits/errors.ts +26 -0
- package/template/src/features/credits/expire.ts +112 -0
- package/template/src/features/credits/grant.ts +168 -0
- package/template/src/features/credits/index.ts +6 -0
- package/template/src/features/credits/manifest.ts +36 -0
- package/template/src/features/credits/query.ts +65 -0
- package/template/src/features/credits/types.ts +48 -0
- package/template/src/features/dashboard/components/index.ts +3 -0
- package/template/src/features/dashboard/components/main-wrapper.tsx +100 -0
- package/template/src/features/dashboard/components/sidebar.tsx +341 -0
- package/template/src/features/dashboard/context/index.ts +2 -0
- package/template/src/features/dashboard/context/sidebar-context.tsx +72 -0
- package/template/src/features/dashboard/index.ts +4 -0
- package/template/src/features/dashboard/manifest.ts +28 -0
- package/template/src/features/mail/actions.ts +175 -0
- package/template/src/features/mail/index.ts +5 -0
- package/template/src/features/mail/manifest.ts +13 -0
- package/template/src/features/mail/server.ts +8 -0
- package/template/src/features/mail/templates/index.ts +11 -0
- package/template/src/features/mail/templates/primary-action-email.tsx +224 -0
- package/template/src/features/mail/templates/welcome-email.tsx +121 -0
- package/template/src/features/mail/utils.ts +190 -0
- package/template/src/features/marketing/components/animated-price.tsx +51 -0
- package/template/src/features/marketing/components/cookie-consent.tsx +255 -0
- package/template/src/features/marketing/components/cta-section.tsx +50 -0
- package/template/src/features/marketing/components/faq-section.tsx +59 -0
- package/template/src/features/marketing/components/feature-grid.tsx +80 -0
- package/template/src/features/marketing/components/footer.tsx +128 -0
- package/template/src/features/marketing/components/header.tsx +255 -0
- package/template/src/features/marketing/components/hero-section.tsx +323 -0
- package/template/src/features/marketing/components/how-it-works.tsx +115 -0
- package/template/src/features/marketing/components/index.ts +14 -0
- package/template/src/features/marketing/components/nav-menu.tsx +236 -0
- package/template/src/features/marketing/components/pricing-section.tsx +446 -0
- package/template/src/features/marketing/components/testimonials.tsx +59 -0
- package/template/src/features/marketing/components/use-cases-section.tsx +78 -0
- package/template/src/features/marketing/index.ts +3 -0
- package/template/src/features/marketing/manifest.ts +35 -0
- package/template/src/features/payment/actions.ts +182 -0
- package/template/src/features/payment/index.ts +4 -0
- package/template/src/features/payment/manifest.ts +18 -0
- package/template/src/features/payment/server.ts +7 -0
- package/template/src/features/payment/types.ts +202 -0
- package/template/src/features/pseo/components/index.ts +6 -0
- package/template/src/features/pseo/components/pseo-cta.tsx +51 -0
- package/template/src/features/pseo/components/pseo-faq.tsx +40 -0
- package/template/src/features/pseo/components/pseo-feature-grid.tsx +74 -0
- package/template/src/features/pseo/components/pseo-hero.tsx +106 -0
- package/template/src/features/pseo/components/pseo-related.tsx +62 -0
- package/template/src/features/pseo/components/pseo-use-cases.tsx +50 -0
- package/template/src/features/pseo/data/pseo-pages.json +534 -0
- package/template/src/features/pseo/index.ts +4 -0
- package/template/src/features/pseo/lib/pseo-data.ts +140 -0
- package/template/src/features/pseo/manifest.ts +22 -0
- package/template/src/features/settings/actions/delete-account.ts +50 -0
- package/template/src/features/settings/actions/index.ts +3 -0
- package/template/src/features/settings/actions/update-profile.ts +49 -0
- package/template/src/features/settings/components/billing-section.tsx +283 -0
- package/template/src/features/settings/components/index.ts +3 -0
- package/template/src/features/settings/components/profile-form.tsx +154 -0
- package/template/src/features/settings/components/security-section.tsx +158 -0
- package/template/src/features/settings/components/settings-profile-view.tsx +506 -0
- package/template/src/features/settings/index.ts +5 -0
- package/template/src/features/settings/manifest.ts +28 -0
- package/template/src/features/settings/schemas/index.ts +2 -0
- package/template/src/features/settings/schemas/update-profile.ts +21 -0
- package/template/src/features/shared/components/cookie-settings-dialog.tsx +282 -0
- package/template/src/features/shared/components/index.ts +5 -0
- package/template/src/features/shared/components/language-switcher.tsx +82 -0
- package/template/src/features/shared/components/max-width-wrapper.tsx +49 -0
- package/template/src/features/shared/components/mode-toggle.tsx +119 -0
- package/template/src/features/shared/icons/google-icon.tsx +39 -0
- package/template/src/features/shared/icons/index.ts +7 -0
- package/template/src/features/shared/index.ts +5 -0
- package/template/src/features/shared/manifest.ts +13 -0
- package/template/src/features/shared/providers.tsx +59 -0
- package/template/src/features/storage/actions.ts +107 -0
- package/template/src/features/storage/index.ts +6 -0
- package/template/src/features/storage/manifest.ts +22 -0
- package/template/src/features/storage/types.ts +143 -0
- package/template/src/features/storage/utils.ts +85 -0
- package/template/src/features/storage/validation.ts +151 -0
- package/template/src/features/subscription/actions/get-user-plan.ts +26 -0
- package/template/src/features/subscription/actions/index.ts +5 -0
- package/template/src/features/subscription/components/current-plan-badge.tsx +18 -0
- package/template/src/features/subscription/components/index.ts +6 -0
- package/template/src/features/subscription/components/plan-badge.tsx +98 -0
- package/template/src/features/subscription/index.ts +25 -0
- package/template/src/features/subscription/manifest.ts +13 -0
- package/template/src/features/subscription/services/user-plan.ts +181 -0
- package/template/src/features/support/actions/admin-ticket.ts +172 -0
- package/template/src/features/support/actions/admin-users.ts +295 -0
- package/template/src/features/support/actions/index.ts +22 -0
- package/template/src/features/support/actions/ticket.ts +12 -0
- package/template/src/features/support/actions/user-ticket.ts +153 -0
- package/template/src/features/support/components/admin-ticket-reply-form.tsx +109 -0
- package/template/src/features/support/components/admin-ticket-status-select.tsx +119 -0
- package/template/src/features/support/components/index.ts +6 -0
- package/template/src/features/support/components/ticket-message-form.tsx +99 -0
- package/template/src/features/support/components/user-role-select.tsx +107 -0
- package/template/src/features/support/index.ts +5 -0
- package/template/src/features/support/manifest.ts +53 -0
- package/template/src/features/support/schemas/index.ts +15 -0
- package/template/src/features/support/schemas/ticket.ts +81 -0
- package/template/src/hooks/index.ts +2 -0
- package/template/src/i18n/index.ts +12 -0
- package/template/src/i18n/request.ts +24 -0
- package/template/src/i18n/routing.ts +23 -0
- package/template/src/instrumentation.ts +19 -0
- package/template/src/lib/ai/index.ts +16 -0
- package/template/src/lib/api-logger.ts +24 -0
- package/template/src/lib/auth/admin.ts +54 -0
- package/template/src/lib/auth/api.ts +17 -0
- package/template/src/lib/auth/client.ts +227 -0
- package/template/src/lib/auth/edge.ts +84 -0
- package/template/src/lib/auth/errors.ts +99 -0
- package/template/src/lib/auth/index.ts +159 -0
- package/template/src/lib/auth/server.ts +46 -0
- package/template/src/lib/auth/session-context.tsx +96 -0
- package/template/src/lib/cloudflare/bindings.ts +31 -0
- package/template/src/lib/cookie-consent.ts +33 -0
- package/template/src/lib/file-utils.ts +45 -0
- package/template/src/lib/fingerprint/index.ts +73 -0
- package/template/src/lib/health/checks.ts +6 -0
- package/template/src/lib/health/core.ts +86 -0
- package/template/src/lib/health/index.ts +11 -0
- package/template/src/lib/health/types.ts +13 -0
- package/template/src/lib/index.ts +2 -0
- package/template/src/lib/logger/index.ts +258 -0
- package/template/src/lib/monitoring/index.ts +384 -0
- package/template/src/lib/rate-limit/anonymous.ts +85 -0
- package/template/src/lib/rate-limit/index.ts +111 -0
- package/template/src/lib/safe-action.ts +127 -0
- package/template/src/lib/seo/json-ld.ts +188 -0
- package/template/src/lib/source.ts +122 -0
- package/template/src/lib/utils.ts +6 -0
- package/template/src/middleware.ts +162 -0
- package/template/src/modules/index.ts +10 -0
- package/template/src/modules/presets.ts +8 -0
- package/template/src/modules/registry.ts +34 -0
- package/template/src/modules/selection.ts +45 -0
- package/template/src/services/ai.ts +49 -0
- package/template/src/services/index.ts +6 -0
- package/template/src/services/jobs.ts +3 -0
- package/template/src/services/mail.ts +7 -0
- package/template/src/services/payment.ts +3 -0
- package/template/src/services/rate-limit.ts +12 -0
- package/template/src/services/storage.ts +3 -0
- package/template/src/types/fumadocs-source.d.ts +14 -0
- package/template/src/types/index.ts +2 -0
- package/template/tsconfig.json +53 -0
- package/template/vercel.json +11 -0
- package/template/vitest.config.ts +51 -0
- package/template/wrangler.jsonc +30 -0
|
@@ -0,0 +1,427 @@
|
|
|
1
|
+
"use server";
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* 积分系统 Server Actions
|
|
5
|
+
*
|
|
6
|
+
* 提供积分系统的前端调用接口
|
|
7
|
+
*/
|
|
8
|
+
|
|
9
|
+
import { z } from "zod";
|
|
10
|
+
|
|
11
|
+
import { getBaseUrl } from "@/config/payment";
|
|
12
|
+
import { logEvent } from "@/lib/logger";
|
|
13
|
+
import { actionClient, protectedAction } from "@/lib/safe-action";
|
|
14
|
+
import { paymentService } from "@/services/payment";
|
|
15
|
+
|
|
16
|
+
import {
|
|
17
|
+
CREDIT_PACKAGES,
|
|
18
|
+
CREDITS_EXPIRY_DAYS,
|
|
19
|
+
MONTHLY_SUBSCRIPTION_CREDITS,
|
|
20
|
+
REGISTRATION_BONUS_CREDITS,
|
|
21
|
+
} from "./config";
|
|
22
|
+
import {
|
|
23
|
+
AccountFrozenError,
|
|
24
|
+
consumeCredits,
|
|
25
|
+
ensureRegistrationBonus,
|
|
26
|
+
getCreditsBalance,
|
|
27
|
+
getUserActiveBatches,
|
|
28
|
+
getUserTransactions,
|
|
29
|
+
getUserTransactionsCount,
|
|
30
|
+
grantCredits,
|
|
31
|
+
InsufficientCreditsError,
|
|
32
|
+
} from "./core";
|
|
33
|
+
|
|
34
|
+
const withPublicCreditsAction = (name: string) =>
|
|
35
|
+
actionClient.metadata({ action: `credits.${name}` });
|
|
36
|
+
const withProtectedCreditsAction = (name: string) =>
|
|
37
|
+
protectedAction.metadata({ action: `credits.${name}` });
|
|
38
|
+
|
|
39
|
+
// ============================================
|
|
40
|
+
// 公开 Actions
|
|
41
|
+
// ============================================
|
|
42
|
+
|
|
43
|
+
/**
|
|
44
|
+
* 注册奖励积分
|
|
45
|
+
*
|
|
46
|
+
* 新用户注册时调用,赠送初始积分
|
|
47
|
+
*/
|
|
48
|
+
export const grantRegistrationBonus = withPublicCreditsAction(
|
|
49
|
+
"grantRegistrationBonus"
|
|
50
|
+
)
|
|
51
|
+
.schema(
|
|
52
|
+
z.object({
|
|
53
|
+
userId: z.string().min(1),
|
|
54
|
+
})
|
|
55
|
+
)
|
|
56
|
+
.action(async ({ parsedInput }) => {
|
|
57
|
+
const { userId } = parsedInput;
|
|
58
|
+
const expiresAt = CREDITS_EXPIRY_DAYS
|
|
59
|
+
? new Date(Date.now() + CREDITS_EXPIRY_DAYS * 24 * 60 * 60 * 1000)
|
|
60
|
+
: null;
|
|
61
|
+
|
|
62
|
+
const result = await grantCredits({
|
|
63
|
+
userId,
|
|
64
|
+
amount: REGISTRATION_BONUS_CREDITS,
|
|
65
|
+
sourceType: "bonus",
|
|
66
|
+
debitAccount: "SYSTEM:registration_bonus",
|
|
67
|
+
transactionType: "registration_bonus",
|
|
68
|
+
expiresAt,
|
|
69
|
+
description: "新用户注册奖励",
|
|
70
|
+
metadata: {
|
|
71
|
+
bonusType: "registration",
|
|
72
|
+
},
|
|
73
|
+
});
|
|
74
|
+
|
|
75
|
+
return {
|
|
76
|
+
success: true,
|
|
77
|
+
...result,
|
|
78
|
+
};
|
|
79
|
+
});
|
|
80
|
+
|
|
81
|
+
// ============================================
|
|
82
|
+
// 受保护 Actions(需要登录)
|
|
83
|
+
// ============================================
|
|
84
|
+
|
|
85
|
+
/**
|
|
86
|
+
* 获取当前用户积分余额
|
|
87
|
+
*
|
|
88
|
+
* 包含懒加载注册奖励机制:
|
|
89
|
+
* 首次调用时,如果用户没有任何交易记录,会自动发放注册奖励
|
|
90
|
+
*/
|
|
91
|
+
export const getMyCreditsBalance = withProtectedCreditsAction(
|
|
92
|
+
"getMyCreditsBalance"
|
|
93
|
+
).action(async ({ ctx }) => {
|
|
94
|
+
const { userId } = ctx;
|
|
95
|
+
|
|
96
|
+
// 懒加载: 确保新用户获得注册奖励
|
|
97
|
+
await ensureRegistrationBonus(
|
|
98
|
+
userId,
|
|
99
|
+
REGISTRATION_BONUS_CREDITS,
|
|
100
|
+
CREDITS_EXPIRY_DAYS
|
|
101
|
+
);
|
|
102
|
+
|
|
103
|
+
// 获取余额
|
|
104
|
+
const balance = await getCreditsBalance(userId);
|
|
105
|
+
|
|
106
|
+
return {
|
|
107
|
+
balance: balance.balance,
|
|
108
|
+
totalEarned: balance.totalEarned,
|
|
109
|
+
totalSpent: balance.totalSpent,
|
|
110
|
+
status: balance.status,
|
|
111
|
+
};
|
|
112
|
+
});
|
|
113
|
+
|
|
114
|
+
/**
|
|
115
|
+
* 获取当前用户活跃批次
|
|
116
|
+
*/
|
|
117
|
+
export const getMyActiveBatches = withProtectedCreditsAction(
|
|
118
|
+
"getMyActiveBatches"
|
|
119
|
+
).action(async ({ ctx }) => {
|
|
120
|
+
const { userId } = ctx;
|
|
121
|
+
const batches = await getUserActiveBatches(userId);
|
|
122
|
+
|
|
123
|
+
return batches.map((batch) => ({
|
|
124
|
+
id: batch.id,
|
|
125
|
+
amount: batch.amount,
|
|
126
|
+
remaining: batch.remaining,
|
|
127
|
+
issuedAt: batch.issuedAt,
|
|
128
|
+
expiresAt: batch.expiresAt,
|
|
129
|
+
sourceType: batch.sourceType,
|
|
130
|
+
}));
|
|
131
|
+
});
|
|
132
|
+
|
|
133
|
+
/**
|
|
134
|
+
* 获取当前用户交易历史
|
|
135
|
+
*/
|
|
136
|
+
export const getMyTransactions = withProtectedCreditsAction("getMyTransactions")
|
|
137
|
+
.schema(
|
|
138
|
+
z
|
|
139
|
+
.object({
|
|
140
|
+
limit: z.number().min(1).max(100).optional(),
|
|
141
|
+
offset: z.number().min(0).optional(),
|
|
142
|
+
})
|
|
143
|
+
.optional()
|
|
144
|
+
)
|
|
145
|
+
.action(async ({ parsedInput, ctx }) => {
|
|
146
|
+
const { userId } = ctx;
|
|
147
|
+
const limit = parsedInput?.limit;
|
|
148
|
+
const offset = parsedInput?.offset;
|
|
149
|
+
|
|
150
|
+
const [transactions, totalCount] = await Promise.all([
|
|
151
|
+
getUserTransactions(userId, {
|
|
152
|
+
...(limit !== undefined && { limit }),
|
|
153
|
+
...(offset !== undefined && { offset }),
|
|
154
|
+
}),
|
|
155
|
+
getUserTransactionsCount(userId),
|
|
156
|
+
]);
|
|
157
|
+
|
|
158
|
+
return {
|
|
159
|
+
transactions: transactions.map((tx) => ({
|
|
160
|
+
id: tx.id,
|
|
161
|
+
type: tx.type,
|
|
162
|
+
amount: tx.amount,
|
|
163
|
+
debitAccount: tx.debitAccount,
|
|
164
|
+
creditAccount: tx.creditAccount,
|
|
165
|
+
description: tx.description,
|
|
166
|
+
metadata: tx.metadata as Record<string, unknown> | null,
|
|
167
|
+
createdAt: tx.createdAt,
|
|
168
|
+
})),
|
|
169
|
+
totalCount,
|
|
170
|
+
};
|
|
171
|
+
});
|
|
172
|
+
|
|
173
|
+
/**
|
|
174
|
+
* 消费积分
|
|
175
|
+
*
|
|
176
|
+
* 用于 AI 服务等需要消费积分的场景
|
|
177
|
+
*/
|
|
178
|
+
export const useCredits = withProtectedCreditsAction("useCredits")
|
|
179
|
+
.schema(
|
|
180
|
+
z.object({
|
|
181
|
+
amount: z.number().min(1),
|
|
182
|
+
serviceName: z.string().min(1),
|
|
183
|
+
description: z.string().optional(),
|
|
184
|
+
metadata: z.record(z.string(), z.unknown()).optional(),
|
|
185
|
+
})
|
|
186
|
+
)
|
|
187
|
+
.action(async ({ parsedInput, ctx }) => {
|
|
188
|
+
const { userId } = ctx;
|
|
189
|
+
const { amount, serviceName, description, metadata } = parsedInput;
|
|
190
|
+
|
|
191
|
+
try {
|
|
192
|
+
const result = await consumeCredits({
|
|
193
|
+
userId,
|
|
194
|
+
amount,
|
|
195
|
+
serviceName,
|
|
196
|
+
...(description !== undefined && { description }),
|
|
197
|
+
...(metadata !== undefined && { metadata }),
|
|
198
|
+
});
|
|
199
|
+
|
|
200
|
+
logEvent("credits.consumed", {
|
|
201
|
+
userId,
|
|
202
|
+
amount,
|
|
203
|
+
serviceName,
|
|
204
|
+
});
|
|
205
|
+
|
|
206
|
+
return {
|
|
207
|
+
success: true,
|
|
208
|
+
consumedAmount: result.consumedAmount,
|
|
209
|
+
remainingBalance: result.remainingBalance,
|
|
210
|
+
transactionId: result.transactionId,
|
|
211
|
+
};
|
|
212
|
+
} catch (error) {
|
|
213
|
+
if (error instanceof InsufficientCreditsError) {
|
|
214
|
+
return {
|
|
215
|
+
success: false,
|
|
216
|
+
error: "insufficient_credits",
|
|
217
|
+
message: error.message,
|
|
218
|
+
required: error.required,
|
|
219
|
+
available: error.available,
|
|
220
|
+
};
|
|
221
|
+
}
|
|
222
|
+
if (error instanceof AccountFrozenError) {
|
|
223
|
+
return {
|
|
224
|
+
success: false,
|
|
225
|
+
error: "account_frozen",
|
|
226
|
+
message: error.message,
|
|
227
|
+
};
|
|
228
|
+
}
|
|
229
|
+
throw error;
|
|
230
|
+
}
|
|
231
|
+
});
|
|
232
|
+
|
|
233
|
+
/**
|
|
234
|
+
* 检查用户是否有足够积分
|
|
235
|
+
*/
|
|
236
|
+
export const checkCreditsAvailable = withProtectedCreditsAction(
|
|
237
|
+
"checkCreditsAvailable"
|
|
238
|
+
)
|
|
239
|
+
.schema(
|
|
240
|
+
z.object({
|
|
241
|
+
amount: z.number().min(1),
|
|
242
|
+
})
|
|
243
|
+
)
|
|
244
|
+
.action(async ({ parsedInput, ctx }) => {
|
|
245
|
+
const { userId } = ctx;
|
|
246
|
+
const { amount } = parsedInput;
|
|
247
|
+
|
|
248
|
+
const balance = await getCreditsBalance(userId);
|
|
249
|
+
|
|
250
|
+
// balance 由 ensureCreditsBalance 保证不为 undefined
|
|
251
|
+
return {
|
|
252
|
+
available: balance.balance >= amount && balance.status === "active",
|
|
253
|
+
currentBalance: balance.balance,
|
|
254
|
+
required: amount,
|
|
255
|
+
status: balance.status,
|
|
256
|
+
};
|
|
257
|
+
});
|
|
258
|
+
|
|
259
|
+
// ============================================
|
|
260
|
+
// 订阅相关积分 Actions
|
|
261
|
+
// ============================================
|
|
262
|
+
|
|
263
|
+
/**
|
|
264
|
+
* 发放月度订阅积分
|
|
265
|
+
*
|
|
266
|
+
* 在订阅续费时调用
|
|
267
|
+
*/
|
|
268
|
+
export const grantMonthlySubscriptionCredits = withPublicCreditsAction(
|
|
269
|
+
"grantMonthlySubscriptionCredits"
|
|
270
|
+
)
|
|
271
|
+
.schema(
|
|
272
|
+
z.object({
|
|
273
|
+
userId: z.string().min(1),
|
|
274
|
+
subscriptionId: z.string().min(1),
|
|
275
|
+
})
|
|
276
|
+
)
|
|
277
|
+
.action(async ({ parsedInput }) => {
|
|
278
|
+
const { userId, subscriptionId } = parsedInput;
|
|
279
|
+
// 月度积分,下个月过期
|
|
280
|
+
const expiresAt = new Date();
|
|
281
|
+
expiresAt.setMonth(expiresAt.getMonth() + 1);
|
|
282
|
+
|
|
283
|
+
const result = await grantCredits({
|
|
284
|
+
userId,
|
|
285
|
+
amount: MONTHLY_SUBSCRIPTION_CREDITS,
|
|
286
|
+
sourceType: "subscription",
|
|
287
|
+
debitAccount: `SUBSCRIPTION:${subscriptionId}`,
|
|
288
|
+
transactionType: "monthly_grant",
|
|
289
|
+
expiresAt,
|
|
290
|
+
sourceRef: subscriptionId,
|
|
291
|
+
description: "月度订阅积分",
|
|
292
|
+
metadata: {
|
|
293
|
+
subscriptionId,
|
|
294
|
+
grantType: "monthly",
|
|
295
|
+
},
|
|
296
|
+
});
|
|
297
|
+
|
|
298
|
+
return {
|
|
299
|
+
success: true,
|
|
300
|
+
...result,
|
|
301
|
+
};
|
|
302
|
+
});
|
|
303
|
+
|
|
304
|
+
/**
|
|
305
|
+
* 购买积分 (内部函数)
|
|
306
|
+
*
|
|
307
|
+
* 由支付 Webhook 调用,在支付成功后发放积分
|
|
308
|
+
* 注意: 这个函数不应该直接被前端调用
|
|
309
|
+
*/
|
|
310
|
+
export const purchaseCredits = withProtectedCreditsAction("purchaseCredits")
|
|
311
|
+
.schema(
|
|
312
|
+
z.object({
|
|
313
|
+
amount: z.number().min(1),
|
|
314
|
+
paymentId: z.string().min(1),
|
|
315
|
+
expiresInDays: z.number().optional(),
|
|
316
|
+
})
|
|
317
|
+
)
|
|
318
|
+
.action(async ({ parsedInput, ctx }) => {
|
|
319
|
+
const { userId } = ctx;
|
|
320
|
+
const { amount, paymentId, expiresInDays } = parsedInput;
|
|
321
|
+
|
|
322
|
+
const expiresAt = expiresInDays
|
|
323
|
+
? new Date(Date.now() + expiresInDays * 24 * 60 * 60 * 1000)
|
|
324
|
+
: null;
|
|
325
|
+
|
|
326
|
+
const result = await grantCredits({
|
|
327
|
+
userId,
|
|
328
|
+
amount,
|
|
329
|
+
sourceType: "purchase",
|
|
330
|
+
debitAccount: `PAYMENT:${paymentId}`,
|
|
331
|
+
transactionType: "purchase",
|
|
332
|
+
expiresAt,
|
|
333
|
+
sourceRef: paymentId,
|
|
334
|
+
description: `购买 ${amount} 积分`,
|
|
335
|
+
metadata: {
|
|
336
|
+
paymentId,
|
|
337
|
+
purchaseType: "direct",
|
|
338
|
+
},
|
|
339
|
+
});
|
|
340
|
+
|
|
341
|
+
logEvent("credits.purchased", {
|
|
342
|
+
userId,
|
|
343
|
+
amount,
|
|
344
|
+
paymentId,
|
|
345
|
+
source: "creem",
|
|
346
|
+
});
|
|
347
|
+
|
|
348
|
+
return {
|
|
349
|
+
success: true,
|
|
350
|
+
...result,
|
|
351
|
+
};
|
|
352
|
+
});
|
|
353
|
+
|
|
354
|
+
// ============================================
|
|
355
|
+
// 积分购买 Checkout
|
|
356
|
+
// ============================================
|
|
357
|
+
|
|
358
|
+
/**
|
|
359
|
+
* 创建积分购买 Checkout Session
|
|
360
|
+
*
|
|
361
|
+
* 创建支付 Checkout Session 用于购买积分套餐
|
|
362
|
+
* metadata 中包含 type: 'credit_purchase' 和 credits 数量
|
|
363
|
+
* Webhook 会根据这些信息发放积分
|
|
364
|
+
*/
|
|
365
|
+
export const createCreditsPurchaseCheckout = withProtectedCreditsAction(
|
|
366
|
+
"createCreditsPurchaseCheckout"
|
|
367
|
+
)
|
|
368
|
+
.schema(
|
|
369
|
+
z.object({
|
|
370
|
+
packageId: z.enum(["lite", "standard", "pro"]),
|
|
371
|
+
successUrl: z.string().optional(),
|
|
372
|
+
cancelUrl: z.string().optional(),
|
|
373
|
+
})
|
|
374
|
+
)
|
|
375
|
+
.action(async ({ parsedInput, ctx }) => {
|
|
376
|
+
const { packageId, successUrl } = parsedInput;
|
|
377
|
+
const { userId } = ctx;
|
|
378
|
+
|
|
379
|
+
// 查找套餐配置
|
|
380
|
+
const pkg = CREDIT_PACKAGES.find((p) => p.id === packageId);
|
|
381
|
+
if (!pkg) {
|
|
382
|
+
throw new Error("无效的积分套餐");
|
|
383
|
+
}
|
|
384
|
+
|
|
385
|
+
const baseUrl = getBaseUrl();
|
|
386
|
+
|
|
387
|
+
logEvent("payment.checkout.started", {
|
|
388
|
+
userId,
|
|
389
|
+
packageId: pkg.id,
|
|
390
|
+
credits: pkg.credits,
|
|
391
|
+
provider: paymentService.provider,
|
|
392
|
+
checkoutType: "credits",
|
|
393
|
+
});
|
|
394
|
+
|
|
395
|
+
const checkout = await paymentService.createCheckout({
|
|
396
|
+
productId: `credits_${packageId}`,
|
|
397
|
+
mode: "one-time",
|
|
398
|
+
successUrl:
|
|
399
|
+
successUrl ??
|
|
400
|
+
`${baseUrl}/dashboard/settings?tab=usage&success=true&credits=${pkg.credits}`,
|
|
401
|
+
requestId: `credit_purchase_${userId}_${Date.now()}`,
|
|
402
|
+
metadata: {
|
|
403
|
+
userId,
|
|
404
|
+
type: "credit_purchase",
|
|
405
|
+
credits: String(pkg.credits),
|
|
406
|
+
packageId: pkg.id,
|
|
407
|
+
},
|
|
408
|
+
});
|
|
409
|
+
|
|
410
|
+
return { url: checkout.url };
|
|
411
|
+
});
|
|
412
|
+
|
|
413
|
+
/**
|
|
414
|
+
* 获取积分套餐列表
|
|
415
|
+
*/
|
|
416
|
+
export const getCreditPackages = withProtectedCreditsAction(
|
|
417
|
+
"getCreditPackages"
|
|
418
|
+
).action(async () => {
|
|
419
|
+
return CREDIT_PACKAGES.map((pkg) => ({
|
|
420
|
+
id: pkg.id,
|
|
421
|
+
name: pkg.name,
|
|
422
|
+
credits: pkg.credits,
|
|
423
|
+
price: pkg.price,
|
|
424
|
+
description: pkg.description,
|
|
425
|
+
popular: "popular" in pkg ? pkg.popular : false,
|
|
426
|
+
}));
|
|
427
|
+
});
|
|
@@ -0,0 +1,54 @@
|
|
|
1
|
+
"use client";
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* 积分余额徽章组件
|
|
5
|
+
*
|
|
6
|
+
* 显示在侧边栏中,展示用户当前可用积分
|
|
7
|
+
*/
|
|
8
|
+
|
|
9
|
+
import { Coins } from "lucide-react";
|
|
10
|
+
import { useAction } from "next-safe-action/hooks";
|
|
11
|
+
import { useEffect } from "react";
|
|
12
|
+
|
|
13
|
+
import { Badge } from "@/components/ui/badge";
|
|
14
|
+
import { getMyCreditsBalance } from "@/features/credits/actions";
|
|
15
|
+
|
|
16
|
+
/**
|
|
17
|
+
* 积分余额徽章
|
|
18
|
+
*
|
|
19
|
+
* 特性:
|
|
20
|
+
* - 自动获取用户积分余额
|
|
21
|
+
* - 显示为闪电图标 + 数字格式
|
|
22
|
+
* - 支持 Tooltip 显示详情
|
|
23
|
+
*/
|
|
24
|
+
export function CreditBalanceBadge() {
|
|
25
|
+
const { execute, result, isPending } = useAction(getMyCreditsBalance);
|
|
26
|
+
|
|
27
|
+
// 组件挂载时获取余额
|
|
28
|
+
useEffect(() => {
|
|
29
|
+
execute();
|
|
30
|
+
}, [execute]);
|
|
31
|
+
|
|
32
|
+
const balance = result.data?.balance ?? 0;
|
|
33
|
+
|
|
34
|
+
// 加载状态
|
|
35
|
+
if (isPending) {
|
|
36
|
+
return (
|
|
37
|
+
<Badge variant="secondary" className="gap-1 px-2 py-1">
|
|
38
|
+
<Coins className="h-3 w-3" />
|
|
39
|
+
<span className="text-xs">...</span>
|
|
40
|
+
</Badge>
|
|
41
|
+
);
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
return (
|
|
45
|
+
<Badge
|
|
46
|
+
variant="secondary"
|
|
47
|
+
className="gap-1 px-2 py-1 bg-warning/15 text-warning hover:bg-warning/25"
|
|
48
|
+
title="Available Credits"
|
|
49
|
+
>
|
|
50
|
+
<Coins className="h-3 w-3" />
|
|
51
|
+
<span className="text-xs font-medium">{balance.toLocaleString()}</span>
|
|
52
|
+
</Badge>
|
|
53
|
+
);
|
|
54
|
+
}
|
|
@@ -0,0 +1,160 @@
|
|
|
1
|
+
"use client";
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* 积分使用情况组件
|
|
5
|
+
*
|
|
6
|
+
* Settings > Usage Tab 的主要内容
|
|
7
|
+
* 包含:
|
|
8
|
+
* - 可用积分余额
|
|
9
|
+
* - 购买积分入口
|
|
10
|
+
* - 即将过期积分提示
|
|
11
|
+
* - 订阅状态
|
|
12
|
+
* - 交易历史
|
|
13
|
+
*/
|
|
14
|
+
|
|
15
|
+
import { Clock, Coins } from "lucide-react";
|
|
16
|
+
import Link from "next/link";
|
|
17
|
+
import { useLocale, useTranslations } from "next-intl";
|
|
18
|
+
import { useAction } from "next-safe-action/hooks";
|
|
19
|
+
import { useEffect } from "react";
|
|
20
|
+
|
|
21
|
+
import { Button } from "@/components/ui/button";
|
|
22
|
+
import { Separator } from "@/components/ui/separator";
|
|
23
|
+
import {
|
|
24
|
+
getMyActiveBatches,
|
|
25
|
+
getMyCreditsBalance,
|
|
26
|
+
} from "@/features/credits/actions";
|
|
27
|
+
|
|
28
|
+
import { TransactionHistory } from "./transaction-history";
|
|
29
|
+
|
|
30
|
+
/**
|
|
31
|
+
* 格式化日期
|
|
32
|
+
*/
|
|
33
|
+
function formatDate(date: Date | string | null, locale: string): string {
|
|
34
|
+
if (!date) return "Never";
|
|
35
|
+
const d = new Date(date);
|
|
36
|
+
return d.toLocaleDateString(locale === "zh" ? "zh-CN" : "en-US", {
|
|
37
|
+
year: "numeric",
|
|
38
|
+
month: "2-digit",
|
|
39
|
+
day: "2-digit",
|
|
40
|
+
});
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
/**
|
|
44
|
+
* 积分使用情况组件
|
|
45
|
+
*/
|
|
46
|
+
export function CreditUsageSection() {
|
|
47
|
+
const t = useTranslations("Settings.usage");
|
|
48
|
+
const locale = useLocale();
|
|
49
|
+
|
|
50
|
+
// 获取积分余额
|
|
51
|
+
const {
|
|
52
|
+
execute: fetchBalance,
|
|
53
|
+
result: balanceResult,
|
|
54
|
+
isPending: isBalanceLoading,
|
|
55
|
+
} = useAction(getMyCreditsBalance);
|
|
56
|
+
|
|
57
|
+
// 获取活跃批次(用于显示即将过期)
|
|
58
|
+
const { execute: fetchBatches, result: batchesResult } =
|
|
59
|
+
useAction(getMyActiveBatches);
|
|
60
|
+
|
|
61
|
+
// 组件挂载时获取数据
|
|
62
|
+
useEffect(() => {
|
|
63
|
+
fetchBalance();
|
|
64
|
+
fetchBatches();
|
|
65
|
+
}, [fetchBalance, fetchBatches]);
|
|
66
|
+
|
|
67
|
+
const balance = balanceResult.data?.balance ?? 0;
|
|
68
|
+
const batches = batchesResult.data ?? [];
|
|
69
|
+
|
|
70
|
+
// 找到最近即将过期的批次
|
|
71
|
+
const expiringBatch = batches
|
|
72
|
+
.filter((b) => b.expiresAt !== null)
|
|
73
|
+
.sort((a, b) => {
|
|
74
|
+
if (!a.expiresAt || !b.expiresAt) return 0;
|
|
75
|
+
return new Date(a.expiresAt).getTime() - new Date(b.expiresAt).getTime();
|
|
76
|
+
})[0];
|
|
77
|
+
|
|
78
|
+
return (
|
|
79
|
+
<div className="space-y-8">
|
|
80
|
+
{/* 标题 */}
|
|
81
|
+
<div>
|
|
82
|
+
<h2 className="text-xl font-semibold">{t("title")}</h2>
|
|
83
|
+
<p className="text-sm text-muted-foreground">{t("description")}</p>
|
|
84
|
+
</div>
|
|
85
|
+
|
|
86
|
+
{/* 可用积分 */}
|
|
87
|
+
<div className="flex items-center justify-between py-4">
|
|
88
|
+
<div className="flex items-center gap-3">
|
|
89
|
+
<div className="flex h-10 w-10 items-center justify-center rounded-full bg-warning/15">
|
|
90
|
+
<Coins className="h-5 w-5 text-warning" />
|
|
91
|
+
</div>
|
|
92
|
+
<span className="font-medium">{t("availableCredits")}</span>
|
|
93
|
+
</div>
|
|
94
|
+
|
|
95
|
+
<div className="text-right">
|
|
96
|
+
{isBalanceLoading ? (
|
|
97
|
+
<div className="animate-pulse">
|
|
98
|
+
<div className="h-10 w-16 bg-muted rounded" />
|
|
99
|
+
</div>
|
|
100
|
+
) : (
|
|
101
|
+
<>
|
|
102
|
+
<div className="text-4xl font-bold">
|
|
103
|
+
{balance.toLocaleString()}
|
|
104
|
+
</div>
|
|
105
|
+
<div className="text-sm text-muted-foreground">
|
|
106
|
+
{t("creditsAvailable")}
|
|
107
|
+
</div>
|
|
108
|
+
</>
|
|
109
|
+
)}
|
|
110
|
+
</div>
|
|
111
|
+
</div>
|
|
112
|
+
|
|
113
|
+
<Separator />
|
|
114
|
+
|
|
115
|
+
{/* 购买更多积分 */}
|
|
116
|
+
<div className="space-y-4">
|
|
117
|
+
<div>
|
|
118
|
+
<h3 className="text-lg font-semibold">{t("getMoreCredits.title")}</h3>
|
|
119
|
+
<p className="text-sm text-muted-foreground">
|
|
120
|
+
{t("getMoreCredits.description")}
|
|
121
|
+
</p>
|
|
122
|
+
</div>
|
|
123
|
+
|
|
124
|
+
<div className="flex gap-3">
|
|
125
|
+
<Button asChild>
|
|
126
|
+
<Link href="/#pricing">{t("getMoreCredits.viewPlans")}</Link>
|
|
127
|
+
</Button>
|
|
128
|
+
</div>
|
|
129
|
+
</div>
|
|
130
|
+
|
|
131
|
+
{/* 即将过期积分提示 */}
|
|
132
|
+
{expiringBatch && (
|
|
133
|
+
<>
|
|
134
|
+
<Separator />
|
|
135
|
+
<div className="flex items-center justify-between rounded-lg border border-warning/30 bg-warning/10 px-4 py-3">
|
|
136
|
+
<div className="flex items-center gap-3">
|
|
137
|
+
<Clock className="h-5 w-5 text-warning" />
|
|
138
|
+
<div>
|
|
139
|
+
<p className="font-medium text-warning">
|
|
140
|
+
{t("expiringSoon.title")}
|
|
141
|
+
</p>
|
|
142
|
+
<p className="text-sm text-warning">
|
|
143
|
+
{t("expiringSoon.message", {
|
|
144
|
+
count: expiringBatch.remaining,
|
|
145
|
+
date: formatDate(expiringBatch.expiresAt, locale),
|
|
146
|
+
})}
|
|
147
|
+
</p>
|
|
148
|
+
</div>
|
|
149
|
+
</div>
|
|
150
|
+
</div>
|
|
151
|
+
</>
|
|
152
|
+
)}
|
|
153
|
+
|
|
154
|
+
<Separator />
|
|
155
|
+
|
|
156
|
+
{/* 交易历史 */}
|
|
157
|
+
<TransactionHistory />
|
|
158
|
+
</div>
|
|
159
|
+
);
|
|
160
|
+
}
|