Files
Epicure/apps/web/components/social/new-collection-button.tsx
T
Arnaud 9da57dd1d0 feat: collections visibility enum (matches recipes) + QR on collection PDF (v0.54.0)
Replaces collections.isPublic (boolean) with collections.visibility
(private/unlisted/public/followers — same enum recipes use). Two-step
migration (0050 adds+backfills, 0051 drops isPublic) since drizzle-kit's
add+drop-in-one-diff rename heuristic needs an interactive prompt we
can't satisfy here.

New collectionVisibleToViewer(viewerId) in lib/visibility.ts mirrors the
existing recipe helper (author always sees own; public/unlisted visible
to anyone; followers-only via the same user_follows EXISTS pattern) —
used by the collection detail page, its print view, fork, and favorite,
replacing their old `or(isPublic, own)` checks.

Create/edit collection dialogs get the same 4-option visibility select
as the recipe form instead of a public/private checkbox.

Collection PDF export now generates a QR code (qrcode, same as the
recipe PDF) linking to /collections/{id}, shown only when visibility is
public/unlisted — same "would an anonymous scanner actually resolve
this" rule as the recipe QR.

Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
2026-07-19 19:26:34 +02:00

90 lines
3.8 KiB
TypeScript

"use client";
import { useState } from "react";
import { useTranslations } from "next-intl";
import { Plus } from "lucide-react";
import { useRouter } from "next/navigation";
import { toast } from "sonner";
import { Button } from "@/components/ui/button";
import { Dialog, DialogContent, DialogHeader, DialogTitle } from "@/components/ui/dialog";
import { Input } from "@/components/ui/input";
import { Textarea } from "@/components/ui/textarea";
import { Label } from "@/components/ui/label";
type Visibility = "private" | "unlisted" | "public" | "followers";
export function NewCollectionButton() {
const t = useTranslations("collections");
const tSocial = useTranslations("social");
const tCommon = useTranslations("common");
const tRecipe = useTranslations("recipe");
const router = useRouter();
const [open, setOpen] = useState(false);
const [name, setName] = useState("");
const [description, setDescription] = useState("");
const [visibility, setVisibility] = useState<Visibility>("private");
const [saving, setSaving] = useState(false);
async function create() {
if (!name.trim()) return;
setSaving(true);
try {
const res = await fetch("/api/v1/collections", {
method: "POST",
headers: { "Content-Type": "application/json" },
body: JSON.stringify({ name: name.trim(), description: description.trim() || undefined, visibility }),
});
if (!res.ok) { toast.error(t("createFailed")); return; }
const { id } = await res.json() as { id: string };
toast.success(tSocial("collectionCreated"));
setOpen(false);
setName(""); setDescription(""); setVisibility("private");
router.push(`/collections/${id}`);
router.refresh();
} finally {
setSaving(false);
}
}
return (
<>
<Button size="sm" onClick={() => setOpen(true)}>
<Plus className="h-4 w-4" /> {t("new")}
</Button>
<Dialog open={open} onOpenChange={setOpen}>
<DialogContent className="max-w-md">
<DialogHeader><DialogTitle>{t("newTitle")}</DialogTitle></DialogHeader>
<div className="space-y-4">
<div className="space-y-2">
<Label htmlFor="col-name">{t("nameLabel")}</Label>
<Input id="col-name" value={name} onChange={(e) => setName(e.target.value)} placeholder={t("namePlaceholder")} />
</div>
<div className="space-y-2">
<Label htmlFor="col-desc">{t("descriptionLabel")}</Label>
<Textarea id="col-desc" value={description} onChange={(e) => setDescription(e.target.value)} rows={2} placeholder={t("descriptionPlaceholder")} />
</div>
<div className="space-y-2">
<Label htmlFor="col-visibility">{tRecipe("visibilityMenuLabel")}</Label>
<select
id="col-visibility"
value={visibility}
onChange={(e) => setVisibility(e.target.value as Visibility)}
className="flex h-9 w-full rounded-md border border-input bg-transparent px-3 py-1 text-sm shadow-xs"
>
<option value="private">{tRecipe("visibility.private")}</option>
<option value="followers">{tRecipe("visibility.followers")}</option>
<option value="unlisted">{tRecipe("visibility.unlisted")}</option>
<option value="public">{tRecipe("visibility.public")}</option>
</select>
</div>
<div className="flex gap-2 justify-end">
<Button variant="outline" onClick={() => setOpen(false)}>{tCommon("cancel")}</Button>
<Button onClick={create} disabled={!name.trim() || saving}>{saving ? t("creating") : t("create")}</Button>
</div>
</div>
</DialogContent>
</Dialog>
</>
);
}