Files
Arnaud 95a4dccce8 feat: duplicate your own recipe
The fork endpoint already had no "isn't yours" check, so this is a
UI-only change: ForkRecipeButton takes a variant prop ("fork" for
others' recipes, "duplicate" for your own) that swaps icon/label/toast
copy, and now renders unconditionally instead of only for non-owners.

Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
2026-07-13 22:27:14 +02:00

55 lines
2.1 KiB
TypeScript

"use client";
import { useState } from "react";
import { useTranslations } from "next-intl";
import { useRouter } from "next/navigation";
import { GitFork, Copy, Loader2 } from "lucide-react";
import { Button } from "@/components/ui/button";
import { Tooltip, TooltipContent, TooltipProvider, TooltipTrigger } from "@/components/ui/tooltip";
import { toast } from "sonner";
// Forking someone else's recipe and duplicating your own hit the exact same
// endpoint (apps/web/app/api/v1/recipes/[id]/fork/route.ts has no
// "isn't yours" check) — only the label/icon/toast copy differs, since
// "fork" implies attribution that doesn't apply to your own recipe.
export function ForkRecipeButton({ recipeId, variant = "fork" }: { recipeId: string; variant?: "fork" | "duplicate" }) {
const t = useTranslations("recipe");
const router = useRouter();
const [forking, setForking] = useState(false);
const isDuplicate = variant === "duplicate";
async function handleFork() {
setForking(true);
try {
const res = await fetch(`/api/v1/recipes/${recipeId}/fork`, { method: "POST" });
if (!res.ok) {
const data = await res.json() as { error?: string };
throw new Error(
res.status === 403 ? t("forkLimitReached") : (data.error ?? t("forkFailed"))
);
}
const data = await res.json() as { id: string };
toast.success(isDuplicate ? t("duplicated") : t("forked"));
router.push(`/recipes/${data.id}`);
} catch (err) {
toast.error(err instanceof Error ? err.message : t("forkFailed"));
setForking(false);
}
}
const label = isDuplicate ? t("duplicateTooltip") : t("forkTooltip");
return (
<TooltipProvider>
<Tooltip>
<TooltipTrigger render={
<Button variant="ghost" size="icon" onClick={() => { void handleFork(); }} disabled={forking} aria-label={label}>
{forking ? <Loader2 className="h-4 w-4 animate-spin" /> : isDuplicate ? <Copy className="h-4 w-4" /> : <GitFork className="h-4 w-4" />}
</Button>
} />
<TooltipContent>{label}</TooltipContent>
</Tooltip>
</TooltipProvider>
);
}