feat: anonymous read-only public link + QR code for meal plans (v0.68.0)
Mirrors shopping lists' isPublic pattern but read-only only -- no
publicEditable equivalent, since anonymous editing of someone's
weekly meal schedule isn't a real use case the way collaborative
shopping-list editing is. New PATCH /api/v1/meal-plans/{weekStart}
toggles it (lazily creates the week's plan row if missing, same
pattern as the existing POST). Public page at /m/[id] renders the
week grouped by day, linking through to public/unlisted recipes and
just showing the title otherwise.
ShareMealPlanButton gained the same public-link toggle UI as its
shopping-list sibling, plus a QR code (generated client-side via the
already-installed `qrcode` package) for the link once enabled --
handing someone a printed or screenshotted plan doesn't require
typing a URL.
Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
This commit is contained in:
@@ -1,9 +1,10 @@
|
||||
"use client";
|
||||
|
||||
import { useState } from "react";
|
||||
import { useEffect, useState } from "react";
|
||||
import { useTranslations } from "next-intl";
|
||||
import { UserPlus, X } from "lucide-react";
|
||||
import { UserPlus, X, Link2, Copy, Check } from "lucide-react";
|
||||
import { toast } from "sonner";
|
||||
import QRCode from "qrcode";
|
||||
import {
|
||||
Dialog,
|
||||
DialogContent,
|
||||
@@ -13,6 +14,7 @@ import {
|
||||
} from "@/components/ui/dialog";
|
||||
import { Button } from "@/components/ui/button";
|
||||
import { Input } from "@/components/ui/input";
|
||||
import { Switch } from "@/components/ui/switch";
|
||||
import {
|
||||
Select,
|
||||
SelectContent,
|
||||
@@ -21,6 +23,7 @@ import {
|
||||
SelectValue,
|
||||
} from "@/components/ui/select";
|
||||
import { Badge } from "@/components/ui/badge";
|
||||
import { Separator } from "@/components/ui/separator";
|
||||
import { Tooltip, TooltipContent, TooltipProvider, TooltipTrigger } from "@/components/ui/tooltip";
|
||||
|
||||
type Role = "viewer" | "editor";
|
||||
@@ -39,9 +42,11 @@ interface Member {
|
||||
|
||||
interface Props {
|
||||
weekStart: string;
|
||||
mealPlanId?: string;
|
||||
initialIsPublic?: boolean;
|
||||
}
|
||||
|
||||
export function ShareMealPlanButton({ weekStart }: Props) {
|
||||
export function ShareMealPlanButton({ weekStart, mealPlanId, initialIsPublic = false }: Props) {
|
||||
const t = useTranslations("mealPlan");
|
||||
const ts = useTranslations("shareDialog");
|
||||
const tCommon = useTranslations("common");
|
||||
@@ -51,6 +56,61 @@ export function ShareMealPlanButton({ weekStart }: Props) {
|
||||
const [members, setMembers] = useState<Member[]>([]);
|
||||
const [loading, setLoading] = useState(false);
|
||||
const [inviting, setInviting] = useState(false);
|
||||
const [isPublic, setIsPublic] = useState(initialIsPublic);
|
||||
const [planId, setPlanId] = useState(mealPlanId);
|
||||
const [savingPublic, setSavingPublic] = useState(false);
|
||||
const [copied, setCopied] = useState(false);
|
||||
const [qrDataUrl, setQrDataUrl] = useState<string | null>(null);
|
||||
|
||||
function shareUrl(id: string): string {
|
||||
return `${window.location.origin}/m/${id}`;
|
||||
}
|
||||
|
||||
useEffect(() => {
|
||||
if (initialIsPublic && mealPlanId) {
|
||||
QRCode.toDataURL(shareUrl(mealPlanId), { margin: 1, width: 160 }).then(setQrDataUrl).catch(() => {});
|
||||
}
|
||||
// Only needs to run once per mount with the plan's already-saved state —
|
||||
// subsequent changes are handled by togglePublic itself.
|
||||
// eslint-disable-next-line react-hooks/exhaustive-deps
|
||||
}, []);
|
||||
|
||||
async function togglePublic(checked: boolean) {
|
||||
setSavingPublic(true);
|
||||
const previous = isPublic;
|
||||
setIsPublic(checked);
|
||||
try {
|
||||
const res = await fetch(`/api/v1/meal-plans/${weekStart}`, {
|
||||
method: "PATCH",
|
||||
headers: { "Content-Type": "application/json" },
|
||||
body: JSON.stringify({ isPublic: checked }),
|
||||
});
|
||||
if (!res.ok) throw new Error();
|
||||
const data = (await res.json()) as { id: string; isPublic: boolean };
|
||||
setPlanId(data.id);
|
||||
if (checked) {
|
||||
setQrDataUrl(await QRCode.toDataURL(shareUrl(data.id), { margin: 1, width: 160 }));
|
||||
} else {
|
||||
setQrDataUrl(null);
|
||||
}
|
||||
} catch {
|
||||
setIsPublic(previous);
|
||||
toast.error(ts("publicLinkToggleFailed"));
|
||||
} finally {
|
||||
setSavingPublic(false);
|
||||
}
|
||||
}
|
||||
|
||||
async function copyLink() {
|
||||
if (!planId) return;
|
||||
try {
|
||||
await navigator.clipboard.writeText(shareUrl(planId));
|
||||
setCopied(true);
|
||||
setTimeout(() => setCopied(false), 2000);
|
||||
} catch {
|
||||
toast.error(ts("copyLinkFailed"));
|
||||
}
|
||||
}
|
||||
|
||||
async function fetchMembers() {
|
||||
setLoading(true);
|
||||
@@ -137,6 +197,31 @@ export function ShareMealPlanButton({ weekStart }: Props) {
|
||||
</DialogDescription>
|
||||
</DialogHeader>
|
||||
|
||||
<div className="flex items-center justify-between gap-3 rounded-lg border p-3">
|
||||
<div className="flex items-center gap-2 min-w-0">
|
||||
<Link2 className="h-4 w-4 text-muted-foreground shrink-0" />
|
||||
<div className="min-w-0">
|
||||
<p className="text-sm font-medium">{ts("publicLinkTitle")}</p>
|
||||
<p className="text-xs text-muted-foreground">{t("publicLinkReadOnlyDescription")}</p>
|
||||
</div>
|
||||
</div>
|
||||
<Switch checked={isPublic} disabled={savingPublic} onCheckedChange={(v) => { void togglePublic(v); }} />
|
||||
</div>
|
||||
{isPublic && planId && (
|
||||
<div className="flex items-center gap-3 rounded-lg border p-3">
|
||||
{qrDataUrl && (
|
||||
// eslint-disable-next-line @next/next/no-img-element -- a data: URL generated client-side, not an optimizable remote image
|
||||
<img src={qrDataUrl} alt={t("qrCodeAlt")} className="h-16 w-16 shrink-0" />
|
||||
)}
|
||||
<Button type="button" variant="outline" size="sm" className="flex-1" onClick={() => void copyLink()}>
|
||||
{copied ? <Check className="h-4 w-4" /> : <Copy className="h-4 w-4" />}
|
||||
{copied ? ts("linkCopied") : ts("copyLink")}
|
||||
</Button>
|
||||
</div>
|
||||
)}
|
||||
|
||||
<Separator />
|
||||
|
||||
<div className="flex gap-2 mt-2">
|
||||
<Input
|
||||
type="email"
|
||||
|
||||
Reference in New Issue
Block a user