MCPcopy
hub / github.com/msgbyte/tianji / NavigationBlocker

Function NavigationBlocker

src/client/components/NavigationBlocker.tsx:43–112  ·  view source on GitHub ↗
({
  when,
  title,
  description,
  cancelText,
  confirmText,
}: NavigationBlockerProps)

Source from the content-addressed store, hash-verified

41 * Shows a confirmation dialog to the user before allowing navigation
42 */
43export function NavigationBlocker({
44 when,
45 title,
46 description,
47 cancelText,
48 confirmText,
49}: NavigationBlockerProps) {
50 const { t } = useTranslation();
51
52 // State for navigation blocker dialog
53 const [showBlockerDialog, setShowBlockerDialog] = useState(false);
54 const [blockerResolver, setBlockerResolver] = useState<{
55 resolve: (value: boolean) => void;
56 } | null>(null);
57
58 // Use blocker to prevent navigation when condition is met
59 useBlocker(() => {
60 return new Promise<boolean>((resolve) => {
61 setShowBlockerDialog(true);
62 setBlockerResolver({ resolve });
63 });
64 }, when);
65
66 const handleBlockerCancel = useEvent(() => {
67 setShowBlockerDialog(false);
68 if (blockerResolver) {
69 blockerResolver.resolve(false); // Block the navigation
70 setBlockerResolver(null);
71 }
72 });
73
74 const handleBlockerConfirm = useEvent(() => {
75 setShowBlockerDialog(false);
76 if (blockerResolver) {
77 blockerResolver.resolve(true); // Allow the navigation
78 setBlockerResolver(null);
79 }
80 });
81
82 return (
83 <AlertDialog
84 open={showBlockerDialog}
85 onOpenChange={(open) => {
86 if (!open) {
87 handleBlockerCancel();
88 }
89 }}
90 >
91 <AlertDialogContent>
92 <AlertDialogHeader>
93 <AlertDialogTitle>{title ?? t('Unsaved Changes')}</AlertDialogTitle>
94 <AlertDialogDescription>
95 {description ??
96 t(
97 'You have unsaved changes that will be lost if you leave. Are you sure you want to discard them?'
98 )}
99 </AlertDialogDescription>
100 </AlertDialogHeader>

Callers

nothing calls this directly

Calls 1

useEventFunction · 0.90

Tested by

no test coverage detected