Open source CapCut alternative.
TypeScript
feat(site): let a super user hide promotions from their profile
David Lebd2958eb6e83
diff --git a/site/src/app/api/account/feature-flags/route.ts b/site/src/app/api/account/feature-flags/route.tsindex bc8894b9..0c897dfd 100644--- a/site/src/app/api/account/feature-flags/route.ts+++ b/site/src/app/api/account/feature-flags/route.ts@@ -1,26 +1,19 @@import { NextResponse } from "next/server";import {+ isDonkeySuperUser,withDonkeyAuth,type DonkeyAuthenticatedRequest,} from "@/lib/donkey-api-auth";-import { ACCOUNT_FEATURE_FLAGS, isKnownFeatureFlag } from "@/lib/feature-flags";+import { featureFlagsFor } from "@/lib/feature-flags";+import { accountFlags } from "@/lib/feature-flags-server";import { prisma } from "@/lib/prisma";-// The signed-in account's feature flags: the full registry with each flag's-// enabled state. Rows exist only for flags the user has touched; everyone else-// gets the flag's registry default.+// The signed-in account's feature flags: the registry as this role sees it,+// with each flag's enabled state. Rows exist only for flags the user has+// touched; everyone else gets the flag's registry default.export const GET = withDonkeyAuth(async (request: DonkeyAuthenticatedRequest) => {- const rows = await prisma.userFeatureFlag.findMany({- where: { userId: request.donkey.userId },- });- const enabled = new Map(rows.map((r) => [r.flag, r.enabled]));- return NextResponse.json({- flags: ACCOUNT_FEATURE_FLAGS.map((f) => ({- ...f,- enabled: enabled.get(f.id) ?? f.defaultEnabled,- })),- });+ return NextResponse.json({ flags: await accountFlags(request.donkey.userId) });});export const PUT = withDonkeyAuth(async (request: DonkeyAuthenticatedRequest) => {@@ -28,10 +21,14 @@ export const PUT = withDonkeyAuth(async (request: DonkeyAuthenticatedRequest) =>flag?: string;enabled?: boolean;} | null;- if (!body?.flag || typeof body.enabled !== "boolean" || !isKnownFeatureFlag(body.flag)) {+ const userId = request.donkey.userId;+ const known =+ body?.flag && typeof body.enabled === "boolean"+ ? featureFlagsFor(await isDonkeySuperUser(userId)).some((f) => f.id === body.flag)+ : false;+ if (!known || !body?.flag || typeof body.enabled !== "boolean") {return NextResponse.json({ error: "Unknown flag." }, { status: 400 });}- const userId = request.donkey.userId;await prisma.userFeatureFlag.upsert({where: { userId_flag: { userId, flag: body.flag } },create: { userId, flag: body.flag, enabled: body.enabled },diff --git a/site/src/app/api/credits/subscribe-bonus/route.ts b/site/src/app/api/credits/subscribe-bonus/route.tsindex f714b8b1..ac083646 100644--- a/site/src/app/api/credits/subscribe-bonus/route.ts+++ b/site/src/app/api/credits/subscribe-bonus/route.ts@@ -3,10 +3,16 @@ import { NextResponse } from "next/server";import { contextFromRequest } from "@/lib/config/effective";import { readSubscribeBonusOffer } from "@/lib/credits/subscribe-bonus";import { withDonkeyAuth } from "@/lib/donkey-api-auth";+import { HIDE_PROMOTIONS_FLAG } from "@/lib/feature-flags";+import { accountFlagEnabled } from "@/lib/feature-flags-server";// The caller's subscribe bonus offer, opened on this read when the account-// has just crossed the share of its signup grant the offer waits for.+// has just crossed the share of its signup grant the offer waits for. A+// super user who has hidden promotions gets none, and this read opens none.export const GET = withDonkeyAuth(async (request) => {+ if (await accountFlagEnabled(request.donkey.userId, HIDE_PROMOTIONS_FLAG)) {+ return NextResponse.json({ offer: null });+ }const ctx = await contextFromRequest(request);const offer = await readSubscribeBonusOffer(ctx);return NextResponse.json({ offer });diff --git a/site/src/app/cut/app/(home)/settings/profile/SuperUserSection.tsx b/site/src/app/cut/app/(home)/settings/profile/SuperUserSection.tsxnew file mode 100644index 00000000..ec1af0ba--- /dev/null+++ b/site/src/app/cut/app/(home)/settings/profile/SuperUserSection.tsx@@ -0,0 +1,43 @@+"use client";++import { Switch } from "@/components/ui/switch";+import { useAccountFlags, useSetAccountFlag } from "@/queries/featureFlags";++// Switches only a super user has. The flags route lists the group for that+// role alone, so for everyone else the section is not there.+export function SuperUserSection() {+ const set = useSetAccountFlag();+ const flags = useAccountFlags().data?.filter((f) => f.group === "su");++ if (!flags?.length) return null;++ return (+ <div className="rounded-xl border bg-card p-5">+ <div className="text-sm font-medium">Super user</div>+ <div className="mt-4 border-t pt-4">+ {flags.map((flag, i) => (+ <div key={flag.id} className={i > 0 ? "mt-4 border-t pt-4" : undefined}>+ <div className="flex items-start justify-between gap-6">+ <span className="min-w-0">+ <span className="block text-sm font-medium">{flag.title}</span>+ <span className="mt-0.5 block text-sm text-muted-foreground">+ {flag.description}+ </span>+ </span>+ <Switch+ aria-label={flag.title}+ checked={flag.enabled}+ onCheckedChange={(v) => set.mutate({ flag: flag.id, enabled: v === true })}+ />+ </div>+ </div>+ ))}+ {set.isError && (+ <p className="mt-3 text-sm text-red-600">+ Couldn't save that change — try again.+ </p>+ )}+ </div>+ </div>+ );+}diff --git a/site/src/app/cut/app/(home)/settings/profile/page.tsx b/site/src/app/cut/app/(home)/settings/profile/page.tsxindex 8cf53c80..bd3df18d 100644--- a/site/src/app/cut/app/(home)/settings/profile/page.tsx+++ b/site/src/app/cut/app/(home)/settings/profile/page.tsx@@ -7,6 +7,7 @@ import { AvatarDialog } from "@/app/cut/app/(home)/settings/profile/AvatarDialogimport { EditorSection } from "@/app/cut/app/(home)/settings/profile/EditorSection";import { EmailSection } from "@/app/cut/app/(home)/settings/profile/EmailSection";import { FeatureFlagsSection } from "@/app/cut/app/(home)/settings/profile/FeatureFlagsSection";+import { SuperUserSection } from "@/app/cut/app/(home)/settings/profile/SuperUserSection";import { Button } from "@/components/ui/button";import { Input } from "@/components/ui/input";import { Label } from "@/components/ui/label";@@ -129,6 +130,8 @@ export default function CutProfilePage() {<FeatureFlagsSection />+ <SuperUserSection />+<AvatarDialogopen={editingAvatar}onOpenChange={setEditingAvatar}diff --git a/site/src/lib/feature-flags-server.ts b/site/src/lib/feature-flags-server.tsnew file mode 100644index 00000000..071e8b1b--- /dev/null+++ b/site/src/lib/feature-flags-server.ts@@ -0,0 +1,30 @@+import { ACCOUNT_FEATURE_FLAGS, featureFlagsFor } from "@/lib/feature-flags";+import { isDonkeySuperUser } from "@/lib/donkey-api-auth";+import { prisma } from "@/lib/prisma";++/** Whether an account flag is on for a user: the account's own row, or the+ * registry default. A flag the user's role cannot see is off whatever a row+ * says. */+export async function accountFlagEnabled(userId: string, flagId: string): Promise<boolean> {+ const flag = ACCOUNT_FEATURE_FLAGS.find((f) => f.id === flagId);+ if (!flag) return false;+ if (flag.group === "su" && !(await isDonkeySuperUser(userId))) return false;+ const row = await prisma.userFeatureFlag.findUnique({+ select: { enabled: true },+ where: { userId_flag: { userId, flag: flagId } },+ });+ return row?.enabled ?? flag.defaultEnabled;+}++/** The registry as this user may see it, with each flag's enabled state. */+export async function accountFlags(userId: string) {+ const [rows, superUser] = await Promise.all([+ prisma.userFeatureFlag.findMany({ where: { userId } }),+ isDonkeySuperUser(userId),+ ]);+ const enabled = new Map(rows.map((r) => [r.flag, r.enabled]));+ return featureFlagsFor(superUser).map((f) => ({+ ...f,+ enabled: enabled.get(f.id) ?? f.defaultEnabled,+ }));+}diff --git a/site/src/lib/feature-flags.ts b/site/src/lib/feature-flags.tsindex eda67908..02c50765 100644--- a/site/src/lib/feature-flags.ts+++ b/site/src/lib/feature-flags.ts@@ -1,19 +1,23 @@// The registry of account flags. A flag ships here first; the account settings// page renders this list and the API accepts only these ids. `group` decides-// which settings section a flag sits in: an editor preference everyone has, or-// an early feature an account opts into.+// which settings section a flag sits in: an editor preference everyone has, an+// early feature an account opts into, or a super-user switch only that role+// sees and can set.++export type AccountFeatureFlagGroup = "editor" | "early" | "su";export type AccountFeatureFlag = {id: string;title: string;description: string;- group: "editor" | "early";+ group: AccountFeatureFlagGroup;/** The state an account with no row of its own gets. A flag that has shipped* to everyone defaults on and the switch becomes an opt-out. */defaultEnabled: boolean;};export const CREDITS_PILL_FLAG = "editor_credits_pill";+export const HIDE_PROMOTIONS_FLAG = "su_hide_promotions";export const ACCOUNT_FEATURE_FLAGS: AccountFeatureFlag[] = [{@@ -23,7 +27,19 @@ export const ACCOUNT_FEATURE_FLAGS: AccountFeatureFlag[] = [group: "editor",defaultEnabled: true,},+ {+ id: HIDE_PROMOTIONS_FLAG,+ title: "Hide promotions",+ description: "No credit offers or subscribe bonuses for this account: the top bar pill, the offer dialog and the Pro card's offer.",+ group: "su",+ defaultEnabled: false,+ },];+/** The flags an account can see and set: the super-user group is only there+ * for that role. */+export const featureFlagsFor = (superUser: boolean) =>+ ACCOUNT_FEATURE_FLAGS.filter((f) => superUser || f.group !== "su");+export const isKnownFeatureFlag = (id: string) =>ACCOUNT_FEATURE_FLAGS.some((f) => f.id === id);diff --git a/site/src/queries/featureFlags.ts b/site/src/queries/featureFlags.tsindex aa394e02..8cec3ab4 100644--- a/site/src/queries/featureFlags.ts+++ b/site/src/queries/featureFlags.ts@@ -2,6 +2,7 @@import { useMutation, useQuery, useQueryClient } from "@tanstack/react-query";+import type { AccountFeatureFlagGroup } from "@/lib/feature-flags";import { apiFetch } from "@/queries/apiClient";export const featureFlagsQueryKey = ["account-feature-flags"] as const;@@ -10,7 +11,7 @@ export type AccountFlag = {id: string;title: string;description: string;- group: "editor" | "early";+ group: AccountFeatureFlagGroup;enabled: boolean;};