RegistryUI recipes
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; the trash block must be installed (npx better-supabase add trash). Its page on bettersupabase.com is /recipes/undo-toast.
Web, with shadcn components:
npx shadcn add sonner"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 });
},
},
},
);
};
}Last updated on