Files
FastGPT/packages/web/hooks/useBeforeunload.ts
Archer e6d53e3daa 4.8.16 test (#3442)
* perf: simple app save

* fix: notify config i18n

* perf: service side props render

* perf: model selector

* update doc
2024-12-20 18:58:40 +08:00

27 lines
748 B
TypeScript

import { useTranslation } from 'next-i18next';
import { useEffect } from 'react';
import { isProduction } from '@fastgpt/global/common/system/constants';
export const useBeforeunload = (props?: { callback?: () => any; tip?: string }) => {
const { t } = useTranslation();
const { tip = t('common:common.Confirm to leave the page'), callback } = props || {};
useEffect(() => {
const listen = isProduction
? (e: any) => {
e.preventDefault();
e.returnValue = tip;
callback?.();
}
: () => {
callback?.();
};
window.addEventListener('beforeunload', listen);
return () => {
window.removeEventListener('beforeunload', listen);
};
}, [tip, callback]);
};