Ask the browser to confirm before the tab closes or reloads while there is unsaved work, and switch the guard off the moment it is saved.
pnpm dlx shadcn@latest add "https://ui.tealmac.com/r/use-before-unload.json"