Undo toast
A toast after a delete with a button that restores the record from the trash. It loads its data on the server through the block's client.
pnpm dev:example) to see it.Add the components
terminal npx shadcn add sonnerInstall the glue
Install the block with
npx better-supabase add trash. The glue items are copied into your app, so edit them as you need.None: the page calls the block's client on the server.
Write the component
React and shadcn
View in the exampleundo-toast.tsx "use client"; import { useAction } from "better-supabase/react"; import { useExtracted } from "next-intl"; import { toast } from "sonner"; import { useErrorMessage } from "@/lib/use-error-message"; import { restoreTrashItem } from "../trash-actions"; export interface TrashedRecord { /** The trash item the delete returned. */ readonly trashItemId: string; readonly name: string; } /** * Returns `show`, which confirms a delete with a toast whose Undo button * restores the record's whole trash batch. */ export function useUndoToast() { const t = useExtracted("trash"); const errorMessage = useErrorMessage(); const restore = useAction(restoreTrashItem, { onSuccess: () => { toast.success(t("Restored")); }, onError: (error) => { toast.error(errorMessage(error)); }, }); return function show({ trashItemId, name }: TrashedRecord) { toast( <span data-recipe="undo-toast"> {t("{name} moved to the trash", { name })} </span>, { action: { label: t("Undo"), onClick: () => { void restore.run({ id: trashItemId }); }, }, }, ); }; }