- Implemented BlogBodySanitizer to clean HTML content before saving articles, ensuring security against XSS attacks. - Added tests for BlogBodySanitizer to verify that unsafe tags and attributes are stripped from the content. - Introduced ApiLeastPrivilegeTest to ensure that unauthorized users cannot access sensitive API routes, maintaining strict access control.
25 lines
1.2 KiB
TypeScript
25 lines
1.2 KiB
TypeScript
import { useCallback } from 'react';
|
|
import { useLocation, useNavigate } from 'react-router';
|
|
|
|
/**
|
|
* رفتار یکسانِ «بازگشت» در کل پنل: اگر کاربر از صفحهٔ دیگری داخل خود پنل آمده باشد،
|
|
* یک قدم در تاریخچه برمیگردد؛ اگر صفحه مستقیم باز شده باشد (لینک مستقیم، رفرش،
|
|
* بوکمارک) تاریخچهای برای برگشتن نیست و به صفحهٔ والدِ همان بخش میرود.
|
|
*
|
|
* تشخیص «ورود مستقیم» با `location.key === 'default'` انجام میشود — همان چیزی که
|
|
* React Router برای اولین ورودیِ تاریخچه میگذارد؛ `history.length` قابل اتکا نیست
|
|
* چون تبهای قبلی مرورگر هم در آن شمرده میشوند.
|
|
*/
|
|
export function useGoBack(fallback: string): () => void {
|
|
const navigate = useNavigate();
|
|
const location = useLocation();
|
|
|
|
return useCallback(() => {
|
|
if (location.key !== 'default') {
|
|
navigate(-1);
|
|
return;
|
|
}
|
|
navigate(fallback, { replace: true });
|
|
}, [navigate, location.key, fallback]);
|
|
}
|