diff --git a/src/app/hack/[slug]/edit/layout.tsx b/src/app/hack/[slug]/edit/layout.tsx new file mode 100644 index 0000000..e89c694 --- /dev/null +++ b/src/app/hack/[slug]/edit/layout.tsx @@ -0,0 +1,57 @@ +import { createClient } from "@/utils/supabase/server"; +import { FiAlertTriangle } from "react-icons/fi"; + +interface EditLayoutProps { + children: React.ReactNode; + params: Promise<{ slug: string }>; +} + +export default async function EditLayout({ children, params }: EditLayoutProps) { + const { slug } = await params; + const supabase = await createClient(); + const { data: { user } } = await supabase.auth.getUser(); + + // Only show banner if user is logged in + if (!user) { + return <>{children}>; + } + + // Check if user is admin + const { data: isAdmin } = await supabase.rpc("is_admin"); + if (!isAdmin) { + return <>{children}>; + } + + // Fetch only the minimal field needed to check ownership + const { data: hack } = await supabase + .from("hacks") + .select("created_by") + .eq("slug", slug) + .maybeSingle(); + + if (!hack) { + return <>{children}>; + } + + // Show banner if admin is not the creator (admins can edit any hack) + const showBanner = hack.created_by !== user.id; + + if (!showBanner) { + return <>{children}>; + } + + return ( + <> +
+ You are editing a hack that you do not own. Please be careful with your changes. +
++ You are editing a hack that you do not own. Please be careful with your changes. +
+