Skip to content

Open source CapCut alternative.

TypeScript

refactor(site): move feature flags onto the profile page

David Le4b87b72ad278
diff --git a/site/src/app/cut/app/(home)/settings/SettingsHeader.tsx b/site/src/app/cut/app/(home)/settings/SettingsHeader.tsx
index 6f534f1f..4ea5bcae 100644
--- a/site/src/app/cut/app/(home)/settings/SettingsHeader.tsx
+++ b/site/src/app/cut/app/(home)/settings/SettingsHeader.tsx
@@ -14,12 +14,6 @@ const SECTIONS = [
description: "Your AI generation usage this billing period.",
pinned: false,
},
- {
- suffix: "/settings/flags",
- title: "Feature flags",
- description: "Early features you can turn on for your account.",
- pinned: true,
- },
{
suffix: "/settings/profile",
title: "Profile",
@@ -35,8 +29,9 @@ const SECTIONS = [
export function SettingsHeader() {
const pathname = usePathname();
+ // Billing is the settings root, so it's also the fallback title.
const section =
- SECTIONS.find((s) => pathname.endsWith(s.suffix)) ?? SECTIONS[1];
+ SECTIONS.find((s) => pathname.endsWith(s.suffix)) ?? SECTIONS.at(-1)!;
return (
<div
className={cn(
diff --git a/site/src/app/cut/app/(home)/settings/flags/page.tsx b/site/src/app/cut/app/(home)/settings/flags/page.tsx
deleted file mode 100644
index 1e4653a1..00000000
--- a/site/src/app/cut/app/(home)/settings/flags/page.tsx
+++ /dev/null
@@ -1,76 +0,0 @@
-"use client";
-
-import { useEffect, useState } from "react";
-
-import { Switch } from "@/components/ui/switch";
-
-type FlagRow = { id: string; title: string; description: string; enabled: boolean };
-
-// Self-serve account feature flags. The account is the source of truth: the
-// switch writes the row and the page holds the new state optimistically until
-// the write answers.
-export default function CutFeatureFlagsPage() {
- const [flags, setFlags] = useState<FlagRow[] | null>(null);
- const [error, setError] = useState<string | null>(null);
-
- useEffect(() => {
- let alive = true;
- void fetch("/api/account/feature-flags")
- .then(async (res) => {
- if (!res.ok) throw new Error();
- const body = (await res.json()) as { flags: FlagRow[] };
- if (alive) setFlags(body.flags);
- })
- .catch(() => {
- if (alive) setError("Couldn't load your feature flags.");
- });
- return () => {
- alive = false;
- };
- }, []);
-
- const toggle = (id: string, enabled: boolean) => {
- setFlags((cur) => cur?.map((f) => (f.id === id ? { ...f, enabled } : f)) ?? cur);
- void fetch("/api/account/feature-flags", {
- method: "PUT",
- headers: { "Content-Type": "application/json" },
- body: JSON.stringify({ flag: id, enabled }),
- }).then((res) => {
- if (res.ok) return;
- // The account write failed; put the switch back.
- setFlags((cur) => cur?.map((f) => (f.id === id ? { ...f, enabled: !enabled } : f)) ?? cur);
- setError("Couldn't save that change — try again.");
- });
- };
-
- return (
- <div className="max-w-2xl space-y-6 pb-9">
- <div className="rounded-xl border bg-card p-5">
- {error && <p className="mb-3 text-sm text-red-600">{error}</p>}
- {!flags && !error && <p className="text-sm text-muted-foreground">Loading…</p>}
- {flags?.length === 0 && (
- <p className="text-sm text-muted-foreground">
- There are no early features to turn on right now.
- </p>
- )}
- {flags?.map((f, i) => (
- <div key={f.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">{f.title}</span>
- <span className="mt-0.5 block text-sm text-muted-foreground">
- {f.description}
- </span>
- </span>
- <Switch
- aria-label={f.title}
- checked={f.enabled}
- onCheckedChange={(v) => toggle(f.id, v === true)}
- />
- </div>
- </div>
- ))}
- </div>
- </div>
- );
-}
diff --git a/site/src/app/cut/app/(home)/settings/profile/FeatureFlagsSection.tsx b/site/src/app/cut/app/(home)/settings/profile/FeatureFlagsSection.tsx
new file mode 100644
index 00000000..493192aa
--- /dev/null
+++ b/site/src/app/cut/app/(home)/settings/profile/FeatureFlagsSection.tsx
@@ -0,0 +1,51 @@
+"use client";
+
+import { Switch } from "@/components/ui/switch";
+import { useAccountFlags, useSetAccountFlag } from "@/queries/featureFlags";
+
+// Self-serve account feature flags, on the profile page rather than a page of
+// their own — there are rarely more than a couple, and they belong to the
+// account like everything else here. With no flags shipping there is nothing to
+// say, so the section isn't there at all.
+export function FeatureFlagsSection() {
+ const { data: flags } = useAccountFlags();
+ const set = useSetAccountFlag();
+
+ if (!flags?.length) return null;
+
+ return (
+ <div className="rounded-xl border bg-card p-5">
+ <div className="text-sm font-medium">Feature flags</div>
+ <p className="mt-1 text-sm text-muted-foreground">
+ Early features you can turn on for your account.
+ </p>
+
+ <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&apos;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.tsx
index 62cd8f56..d624d4b0 100644
--- a/site/src/app/cut/app/(home)/settings/profile/page.tsx
+++ b/site/src/app/cut/app/(home)/settings/profile/page.tsx
@@ -4,11 +4,11 @@ import { useState } from "react";
import { Pencil } from "lucide-react";
import { AvatarDialog } from "@/app/cut/app/(home)/settings/profile/AvatarDialog";
+import { FeatureFlagsSection } from "@/app/cut/app/(home)/settings/profile/FeatureFlagsSection";
import { Button } from "@/components/ui/button";
import { Input } from "@/components/ui/input";
import { Label } from "@/components/ui/label";
import { UserAvatar } from "@/cut/components/UserAvatar";
-import { openOnboarding } from "@/cut/lib/onboarding";
import {
useAccountProfile,
useUpdateDisplayName,
@@ -98,19 +98,7 @@ export default function CutProfilePage() {
</div>
</div>
- {/* The welcome sequence is a full-window overlay mounted in the app
- shell, so this row asks for it rather than rendering it. */}
- <div className="flex items-center justify-between gap-4 rounded-xl border bg-card p-5">
- <div className="min-w-0">
- <div className="text-sm font-medium">Welcome tour</div>
- <p className="mt-1 text-sm text-muted-foreground">
- The short intro to Donkey Cut you saw when you signed up.
- </p>
- </div>
- <Button variant="outline" onClick={openOnboarding}>
- Replay
- </Button>
- </div>
+ <FeatureFlagsSection />
<AvatarDialog
open={editingAvatar}
diff --git a/site/src/cut/components/NavUser.tsx b/site/src/cut/components/NavUser.tsx
index b67fd343..4192f702 100644
--- a/site/src/cut/components/NavUser.tsx
+++ b/site/src/cut/components/NavUser.tsx
@@ -6,9 +6,9 @@ import {
ChevronRight,
CreditCard,
EllipsisVertical,
- Flag,
LogOut,
MonitorDown,
+ Sparkles,
} from "lucide-react";
import {
DropdownMenu,
@@ -21,6 +21,7 @@ import {
import { Skeleton } from "@/components/ui/skeleton";
import { UserAvatar } from "@/cut/components/UserAvatar";
import { cutInstallHref } from "@/cut/lib/install";
+import { openOnboarding } from "@/cut/lib/onboarding";
import { useCutBase } from "@/cut/lib/nav";
import { authClient } from "@/lib/auth-client";
import { useAccountProfile, visibleName } from "@/queries/accountProfile";
@@ -96,8 +97,10 @@ export function NavUser() {
<DropdownMenuItem onClick={() => router.push(`${base}/settings/usage`)}>
<ChartColumn /> Usage
</DropdownMenuItem>
- <DropdownMenuItem onClick={() => router.push(`${base}/settings/flags`)}>
- <Flag /> Feature flags
+ {/* The welcome sequence is a full-window overlay mounted in the app
+ shell, so this asks for it rather than routing anywhere. */}
+ <DropdownMenuItem onClick={openOnboarding}>
+ <Sparkles /> View onboarding
</DropdownMenuItem>
{/* The install page lives outside the editor, so it opens in its own
tab rather than navigating away from an open project. */}
diff --git a/site/src/queries/featureFlags.ts b/site/src/queries/featureFlags.ts
new file mode 100644
index 00000000..5cd36ecf
--- /dev/null
+++ b/site/src/queries/featureFlags.ts
@@ -0,0 +1,49 @@
+"use client";
+
+import { useMutation, useQuery, useQueryClient } from "@tanstack/react-query";
+
+import { apiFetch } from "@/queries/apiClient";
+
+export const featureFlagsQueryKey = ["account-feature-flags"] as const;
+
+export type AccountFlag = {
+ id: string;
+ title: string;
+ description: string;
+ enabled: boolean;
+};
+
+export function useAccountFlags() {
+ return useQuery({
+ queryFn: async () => {
+ const body = await apiFetch<{ flags: AccountFlag[] }>(
+ "/api/account/feature-flags",
+ );
+ return body.flags;
+ },
+ queryKey: featureFlagsQueryKey,
+ });
+}
+
+// The switch shows the new state while the account write is in flight and puts
+// itself back if the write fails, so a flag can't read as on when it isn't.
+export function useSetAccountFlag() {
+ const queryClient = useQueryClient();
+ return useMutation({
+ mutationFn: (variables: { flag: string; enabled: boolean }) =>
+ apiFetch<{ ok: true }>("/api/account/feature-flags", {
+ body: JSON.stringify(variables),
+ method: "PUT",
+ }),
+ onMutate: ({ flag, enabled }) => {
+ const previous = queryClient.getQueryData<AccountFlag[]>(featureFlagsQueryKey);
+ queryClient.setQueryData<AccountFlag[]>(featureFlagsQueryKey, (flags) =>
+ flags?.map((f) => (f.id === flag ? { ...f, enabled } : f)),
+ );
+ return { previous };
+ },
+ onError: (_error, _variables, context) => {
+ queryClient.setQueryData(featureFlagsQueryKey, context?.previous);
+ },
+ });
+}