Skip to content

Commit a269d4f

Browse files
committed
Block leaving setting page with unsaved changes
1 parent 2831b21 commit a269d4f

1 file changed

Lines changed: 14 additions & 0 deletions

File tree

src/app/pages/SettingPage.tsx

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import { useBlocker } from '@tanstack/react-router'
12
import { FC, PropsWithChildren, useCallback, useEffect, useState } from 'react'
23
import toast, { Toaster } from 'react-hot-toast'
34
import { useTranslation } from 'react-i18next'
@@ -96,6 +97,19 @@ function SettingPage() {
9697
setTimeout(() => location.reload(), 500)
9798
}, [userConfig])
9899

100+
useBlocker(t('You have unsaved changes, are you sure you want to leave?'), dirty)
101+
102+
useEffect(() => {
103+
const listener = (e: Event) => {
104+
if (dirty) {
105+
e.preventDefault()
106+
e.returnValue = false
107+
}
108+
}
109+
window.addEventListener('beforeunload', listener)
110+
return () => window.removeEventListener('beforeunload', listener)
111+
}, [dirty])
112+
99113
if (!userConfig) {
100114
return null
101115
}

0 commit comments

Comments
 (0)