Rallly is an open-source scheduling and collaboration tool designed to make organizing events and meetings easier.
TypeScript i18nextnext-authnextjspostgresqlprismareact-emailt3-stacktailwindcsstrpcturborepotypescriptzod
✨ Show API usage limits on the API keys settings page (RAL-1265) (#2656)
Luke Vella5c2daa1b4456
diff --git a/apps/web/public/locales/en/app.json b/apps/web/public/locales/en/app.jsonindex f5e2996bc..31f9cb92c 100644--- a/apps/web/public/locales/en/app.json+++ b/apps/web/public/locales/en/app.json@@ -47,6 +47,8 @@"alreadyMember": "This person is already a member of this space","annualDiscount": "Pay for {payMonths, number} months, get 12.","annualSavings": "Save {amount} with yearly billing",+ "apiAccessUpgradeDescription": "Create API keys to manage your polls programmatically. Included with Pro, with a shared limit of {count} requests per minute across your space.",+ "apiAccessUpgradeTitle": "Upgrade for API access","apiKeyCreated": "API key created","apiKeyCreatedDescription": "Copy your API key now. You won't be able to see it again.","apiKeyLimitReached": "You've reached the maximum number of API keys. Revoke one before creating another.",@@ -55,6 +57,9 @@"apiKeysDescription": "Manage API keys for programmatic access to your space","apiKeySecurityWarning": "Store this key securely. It won't be shown again.","apiKeysSettingsDescription": "Create and manage API keys for programmatic access to your space.",+ "apiRateLimitLabel": "Rate limit",+ "apiRateLimitValue": "{count} requests / minute",+ "apiViewDocs": "View API docs","applyToAllDates": "Apply to all dates","areYouSure": "Are you sure?","attendeeCount": "{count, plural, =0 {No attendees} one {1 attendee} other {# attendees}}",diff --git a/apps/web/src/app/[locale]/(space)/settings/api-keys/components/api-access-upgrade.tsx b/apps/web/src/app/[locale]/(space)/settings/api-keys/components/api-access-upgrade.tsxnew file mode 100644index 000000000..77b675040--- /dev/null+++ b/apps/web/src/app/[locale]/(space)/settings/api-keys/components/api-access-upgrade.tsx@@ -0,0 +1,49 @@+"use client";++import { posthog } from "@rallly/posthog/client";+import { Button } from "@rallly/ui/button";+import { TerminalIcon } from "lucide-react";+import {+ EmptyState,+ EmptyStateDescription,+ EmptyStateFooter,+ EmptyStateIcon,+ EmptyStateTitle,+} from "@/components/empty-state";+import { API_RATE_LIMIT_PER_MINUTE } from "@/features/api-keys/constants";+import { showPayWall } from "@/features/billing/client";+import { Trans } from "@/i18n/client";++export function ApiAccessUpgrade() {+ return (+ <EmptyState>+ <EmptyStateIcon>+ <TerminalIcon />+ </EmptyStateIcon>+ <EmptyStateTitle>+ <Trans+ i18nKey="apiAccessUpgradeTitle"+ defaults="Upgrade for API access"+ />+ </EmptyStateTitle>+ <EmptyStateDescription>+ <Trans+ i18nKey="apiAccessUpgradeDescription"+ defaults="Create API keys to manage your polls programmatically. Included with Pro, with a shared limit of {count} requests per minute across your space."+ values={{ count: API_RATE_LIMIT_PER_MINUTE }}+ />+ </EmptyStateDescription>+ <EmptyStateFooter>+ <Button+ variant="primary"+ onClick={() => {+ posthog?.capture("api_keys:upgrade_button_click");+ showPayWall();+ }}+ >+ <Trans i18nKey="upgradeToPro" defaults="Upgrade to Pro" />+ </Button>+ </EmptyStateFooter>+ </EmptyState>+ );+}diff --git a/apps/web/src/app/[locale]/(space)/settings/api-keys/components/api-usage-limits.tsx b/apps/web/src/app/[locale]/(space)/settings/api-keys/components/api-usage-limits.tsxnew file mode 100644index 000000000..3c0a23954--- /dev/null+++ b/apps/web/src/app/[locale]/(space)/settings/api-keys/components/api-usage-limits.tsx@@ -0,0 +1,40 @@+"use client";++import { Icon } from "@rallly/ui/icon";+import { ArrowUpRightIcon } from "lucide-react";+import Link from "next/link";+import {+ API_RATE_LIMIT_PER_MINUTE,+ getApiDocsPath,+} from "@/features/api-keys/constants";+import { Trans } from "@/i18n/client";++export function ApiUsageLimits() {+ return (+ <div className="flex flex-wrap items-baseline justify-between gap-x-6 gap-y-2">+ <div className="flex items-baseline gap-3">+ <span className="text-muted-foreground text-sm">+ <Trans i18nKey="apiRateLimitLabel" defaults="Rate limit" />+ </span>+ <span className="font-medium text-sm tabular-nums">+ <Trans+ i18nKey="apiRateLimitValue"+ defaults="{count} requests / minute"+ values={{ count: API_RATE_LIMIT_PER_MINUTE }}+ />+ </span>+ </div>+ <Link+ href={getApiDocsPath()}+ target="_blank"+ rel="noopener noreferrer"+ className="inline-flex items-center gap-1 text-primary text-sm underline-offset-4 hover:underline"+ >+ <Trans i18nKey="apiViewDocs" defaults="View API docs" />+ <Icon>+ <ArrowUpRightIcon />+ </Icon>+ </Link>+ </div>+ );+}diff --git a/apps/web/src/app/[locale]/(space)/settings/api-keys/page.tsx b/apps/web/src/app/[locale]/(space)/settings/api-keys/page.tsxindex 0c21f41da..2580c997c 100644--- a/apps/web/src/app/[locale]/(space)/settings/api-keys/page.tsx+++ b/apps/web/src/app/[locale]/(space)/settings/api-keys/page.tsx@@ -7,64 +7,86 @@ import {} from "@/components/page-layout";import {SettingsPage,+ SettingsPageAction,SettingsPageContent,SettingsPageDescription,SettingsPageHeader,SettingsPageTitle,} from "@/components/settings-layout";-import { getSpaceApiKeys } from "@/features/api-keys/data";+import { getApiKeysPageState, getSpaceApiKeys } from "@/features/api-keys/data";import { Trans } from "@/i18n/client";import { getTranslation } from "@/i18n/server";import { getPathname } from "@/lib/pathname";import { buildSafeRedirectUrl } from "@/lib/utils/redirect";+import { ApiAccessUpgrade } from "./components/api-access-upgrade";import { ApiKeysList } from "./components/api-keys-list";+import { ApiUsageLimits } from "./components/api-usage-limits";import { CreateApiKeyButton } from "./components/create-api-key-button";export default async function ApiKeysSettingsPage() {- const result = await getSpaceApiKeys();+ const pageState = await getApiKeysPageState();- if (!result.ok) {- if (result.reason === "unauthorized") {- redirect(- buildSafeRedirectUrl({- destination: "/login",- returnUrl: await getPathname(),- }),- );- }+ if (pageState.state === "unauthorized") {+ redirect(+ buildSafeRedirectUrl({+ destination: "/login",+ returnUrl: await getPathname(),+ }),+ );+ }+ if (pageState.state === "unavailable") {notFound();}return (<SettingsPage>- <SettingsPageHeader className="flex-row items-center justify-between">- <div>- <SettingsPageTitle>- <Trans i18nKey="apiKeys" defaults="API Keys" />- </SettingsPageTitle>- <SettingsPageDescription>- <Trans- i18nKey="apiKeysDescription"- defaults="Manage API keys for programmatic access to your space"- />- </SettingsPageDescription>- </div>- <CreateApiKeyButton />+ <SettingsPageHeader>+ <SettingsPageTitle>+ <Trans i18nKey="apiKeys" defaults="API Keys" />+ </SettingsPageTitle>+ <SettingsPageDescription>+ <Trans+ i18nKey="apiKeysDescription"+ defaults="Manage API keys for programmatic access to your space"+ />+ </SettingsPageDescription>+ {pageState.state === "enabled" ? (+ <SettingsPageAction>+ <CreateApiKeyButton />+ </SettingsPageAction>+ ) : null}</SettingsPageHeader><SettingsPageContent>- <PageSectionGroup>- <PageSection>- <PageSectionContent>- <ApiKeysList apiKeys={result.apiKeys} />- </PageSectionContent>- </PageSection>- </PageSectionGroup>+ {pageState.state === "upgrade_required" ? (+ <ApiAccessUpgrade />+ ) : (+ <ApiKeysContent />+ )}</SettingsPageContent></SettingsPage>);}+async function ApiKeysContent() {+ const result = await getSpaceApiKeys();++ if (!result.ok) {+ notFound();+ }++ return (+ <PageSectionGroup>+ <ApiUsageLimits />+ <PageSection>+ <PageSectionContent>+ <ApiKeysList apiKeys={result.apiKeys} />+ </PageSectionContent>+ </PageSection>+ </PageSectionGroup>+ );+}+export async function generateMetadata(): Promise<Metadata> {const { t } = await getTranslation();return {diff --git a/apps/web/src/app/[locale]/(space)/settings/calendars/page.tsx b/apps/web/src/app/[locale]/(space)/settings/calendars/page.tsxindex 436a65004..ace4d6498 100644--- a/apps/web/src/app/[locale]/(space)/settings/calendars/page.tsx+++ b/apps/web/src/app/[locale]/(space)/settings/calendars/page.tsx@@ -11,6 +11,7 @@ import {} from "@/components/page-layout";import {SettingsPage,+ SettingsPageAction,SettingsPageContent,SettingsPageDescription,SettingsPageHeader,@@ -41,22 +42,20 @@ export default async function CalendarsPage() {return (<HydrationBoundary state={dehydrate(trpc.queryClient)}><SettingsPage>- <div className="flex justify-between">- <SettingsPageHeader>- <SettingsPageTitle>- <Trans i18nKey="calendars" defaults="Calendars" />- </SettingsPageTitle>- <SettingsPageDescription>- <Trans- i18nKey="calendarsDescription"- defaults="Manage your calendar connections"- />- </SettingsPageDescription>- </SettingsPageHeader>- <div>+ <SettingsPageHeader>+ <SettingsPageTitle>+ <Trans i18nKey="calendars" defaults="Calendars" />+ </SettingsPageTitle>+ <SettingsPageDescription>+ <Trans+ i18nKey="calendarsDescription"+ defaults="Manage your calendar connections"+ />+ </SettingsPageDescription>+ <SettingsPageAction><ConnectCalendarDropdown />- </div>- </div>+ </SettingsPageAction>+ </SettingsPageHeader><SettingsPageContent><PageSection variant="card"><PageSectionHeader>diff --git a/apps/web/src/app/[locale]/(space)/settings/event-types/event-types-settings-page.tsx b/apps/web/src/app/[locale]/(space)/settings/event-types/event-types-settings-page.tsxindex 5a145d84e..335c6586c 100644--- a/apps/web/src/app/[locale]/(space)/settings/event-types/event-types-settings-page.tsx+++ b/apps/web/src/app/[locale]/(space)/settings/event-types/event-types-settings-page.tsx@@ -34,6 +34,7 @@ import { OptimizedAvatarImage } from "@/components/optimized-avatar-image";import { RandomGradientBar } from "@/components/random-gradient-bar";import {SettingsPage,+ SettingsPageAction,SettingsPageContent,SettingsPageDescription,SettingsPageHeader,@@ -174,25 +175,23 @@ export function EventTypesSettingsPage() {return (<SettingsPage>- <div className="flex justify-between gap-4">- <SettingsPageHeader>- <SettingsPageTitle>- <Trans i18nKey="eventTypes" defaults="Event Types" />- </SettingsPageTitle>- <SettingsPageDescription>- <Trans- i18nKey="eventTypesDescription"- defaults="Reusable event configurations."- />- </SettingsPageDescription>- </SettingsPageHeader>- <div>+ <SettingsPageHeader>+ <SettingsPageTitle>+ <Trans i18nKey="eventTypes" defaults="Event Types" />+ </SettingsPageTitle>+ <SettingsPageDescription>+ <Trans+ i18nKey="eventTypesDescription"+ defaults="Reusable event configurations."+ />+ </SettingsPageDescription>+ <SettingsPageAction><Button variant="primary" onClick={() => createDialog.trigger()}><PlusIcon data-icon="inline-start" /><Trans i18nKey="newEventType" defaults="New Event Type" /></Button>- </div>- </div>+ </SettingsPageAction>+ </SettingsPageHeader><SettingsPageContent>{eventTypes.length === 0 ? (<EventTypesEmptyState onCreate={() => createDialog.trigger()} />diff --git a/apps/web/src/app/[locale]/(space)/settings/members/page-client.tsx b/apps/web/src/app/[locale]/(space)/settings/members/page-client.tsxindex 2dbccce60..095f3347f 100644--- a/apps/web/src/app/[locale]/(space)/settings/members/page-client.tsx+++ b/apps/web/src/app/[locale]/(space)/settings/members/page-client.tsx@@ -39,7 +39,7 @@ export function MembersSettingsPageClient() {<SettingsPage><SettingsPageHeader><SettingsPageTitle>- <Trans i18nKey="members" />+ <Trans i18nKey="members" defaults="Members" /></SettingsPageTitle><SettingsPageDescription><Transdiff --git a/apps/web/src/app/api/private/utils/rate-limit.ts b/apps/web/src/app/api/private/utils/rate-limit.tsindex 8aaa5d34f..dc1bd123e 100644--- a/apps/web/src/app/api/private/utils/rate-limit.ts+++ b/apps/web/src/app/api/private/utils/rate-limit.ts@@ -1,13 +1,15 @@import { RedisStore } from "@hono-rate-limiter/redis";import { rateLimiter } from "hono-rate-limiter";+import { API_RATE_LIMIT_PER_MINUTE } from "@/features/api-keys/constants";import { redis } from "@/lib/kv";import { apiError } from "./poll";/**- * Requests per minute allowed for a single space, uniform across all- * endpoints. Adding more API keys to a space does not raise this limit.+ * Re-exported for the OpenAPI docs and tests. The value lives in the+ * api-keys feature so the settings UI can read it without pulling in this+ * server-only module.*/-export const RATE_LIMIT_PER_MINUTE = 60;+export const RATE_LIMIT_PER_MINUTE = API_RATE_LIMIT_PER_MINUTE;type RateLimitEnv = {Variables: {diff --git a/apps/web/src/components/settings-layout.tsx b/apps/web/src/components/settings-layout.tsxindex 6f01b787a..3186063cd 100644--- a/apps/web/src/components/settings-layout.tsx+++ b/apps/web/src/components/settings-layout.tsx@@ -24,7 +24,14 @@ export function SettingsPageHeader({className?: string;}) {return (- <div className={cn("flex flex-col items-start", className)}>{children}</div>+ <div+ className={cn(+ "grid auto-rows-min items-start gap-x-4 gap-y-1 has-data-[slot=settings-page-action]:grid-cols-1 has-data-[slot=settings-page-action]:sm:grid-cols-[1fr_auto]",+ className,+ )}+ >+ {children}+ </div>);}@@ -55,9 +62,27 @@ export function SettingsPageDescription({className?: string;}) {return (- <p className={cn("mt-1 text-muted-foreground text-sm", className)}>+ <p className={cn("text-muted-foreground text-sm", className)}>{children}</p>+ );+}++export function SettingsPageAction({+ children,+ className,+}: {+ children?: React.ReactNode;+ className?: string;+}) {+ return (+ <div+ data-slot="settings-page-action"+ className={cn(+ "mt-3 flex shrink-0 items-center gap-2 sm:col-start-2 sm:row-span-2 sm:row-start-1 sm:mt-0 sm:self-center sm:justify-self-end",+ className,+ )}+ >{children}- </p>+ </div>);}diff --git a/apps/web/src/features/api-keys/constants.ts b/apps/web/src/features/api-keys/constants.tsnew file mode 100644index 000000000..835443f28--- /dev/null+++ b/apps/web/src/features/api-keys/constants.ts@@ -0,0 +1,23 @@+/**+ * Requests per minute allowed for a single space, uniform across all+ * endpoints. Adding more API keys to a space does not raise this limit.+ *+ * This is the single source of truth: the rate-limit middleware, the OpenAPI+ * docs, and the settings UI all read it, so the enforced limit and the+ * published limit can never drift.+ */+export const API_RATE_LIMIT_PER_MINUTE = 60;++/**+ * Path segment for the current version of the public API. Keeping it here+ * means moving between versions (e.g. `private` → `v1`) only touches one+ * place, and every link stays pointed at the latest docs.+ */+const CURRENT_API_VERSION_PATH = "private";++/**+ * Link to the interactive API reference for the current version. Callers+ * should use this rather than hardcoding the path so version moves don't+ * leave stale links behind.+ */+export const getApiDocsPath = () => `/api/${CURRENT_API_VERSION_PATH}/docs`;diff --git a/apps/web/src/features/api-keys/data.ts b/apps/web/src/features/api-keys/data.tsindex b5c34f953..4ec2bdedb 100644--- a/apps/web/src/features/api-keys/data.ts+++ b/apps/web/src/features/api-keys/data.ts@@ -62,6 +62,36 @@ export const getSpaceApiKeys = cache(async () => {return { ok: true, apiKeys } as const;});+/**+ * Resolve the current space's tier for the API keys page. Lets the page tell+ * "needs to upgrade" (hobby tier) apart from other reasons access is blocked+ * (feature flag off, not the owner), which all collapse to a single reason in+ * getApiKeyAccessContext.+ */+export const getApiKeysPageState = cache(async () => {+ const user = await getCurrentUser();++ if (!user) {+ return { state: "unauthorized" } as const;+ }++ const space = await getActiveSpaceForUser(user.id);++ if (!space) {+ return { state: "unauthorized" } as const;+ }++ if (await isApiAccessEnabled(user, space)) {+ return { state: "enabled" } as const;+ }++ if (space.tier === "hobby") {+ return { state: "upgrade_required" } as const;+ }++ return { state: "unavailable" } as const;+});+/*** Determines if a user has access to API features (API keys, developer tools)* @param user - The user to check access for