forked from CredenceOrg/Credence-Frontend
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathuseSmartBack.ts
More file actions
60 lines (52 loc) · 1.76 KB
/
Copy pathuseSmartBack.ts
File metadata and controls
60 lines (52 loc) · 1.76 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
import { useNavigate, useLocation } from 'react-router-dom'
import { DEFAULT_FALLBACK_ROUTE } from '../config/navigation'
import {
resolveSmartBackDestination,
SmartBackLocationState,
SmartBackResult,
} from '../lib/smartBack'
export interface UseSmartBackOptions {
/** Optional fallback route when history is missing. Defaults to `/dashboard`. */
fallback?: string
}
export interface UseSmartBackReturn {
/** Trigger smart-back navigation */
goBack: () => void
/** The active fallback route */
fallback: string
/** Pure destination resolution for the current location & history */
getDestination: () => SmartBackResult
}
/**
* Custom React hook for smart-back navigation.
* - Prior-route path (`location.state.from`) is honoured when present.
* - Standard history back is used when history entry exists.
* - Missing history falls back to `/dashboard` (or custom fallback route).
*/
export function useSmartBack(options: UseSmartBackOptions = {}): UseSmartBackReturn {
const navigate = useNavigate()
const location = useLocation()
const fallback = options.fallback || DEFAULT_FALLBACK_ROUTE
const getDestination = (): SmartBackResult => {
const state = location.state as SmartBackLocationState | null
const hasHistory =
typeof window !== 'undefined' &&
(window.history.length > 1 || Boolean(window.history.state && window.history.state.idx > 0))
return resolveSmartBackDestination(state, hasHistory, fallback)
}
const goBack = () => {
const destination = getDestination()
if (destination.type === 'state') {
navigate(destination.path)
} else if (destination.type === 'history') {
navigate(-1)
} else {
navigate(destination.path)
}
}
return {
goBack,
fallback,
getDestination,
}
}