-
Notifications
You must be signed in to change notification settings - Fork 48
/
Copy pathindex.ts
90 lines (82 loc) · 2.86 KB
/
index.ts
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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
'use client';
import { createUseFunnel } from '@use-funnel/core';
import { useSearchParams } from 'next/navigation';
import { useLayoutEffect, useMemo, useState } from 'react';
export const useFunnel = createUseFunnel(({ id, initialState }) => {
const searchParams = useSearchParams();
const [state, setState] = useState<Record<string, any>>({});
useLayoutEffect(() => {
if (typeof window !== 'undefined') {
setState(window.history.state);
}
function handlePopState(event: PopStateEvent) {
setState(event.state);
}
window.addEventListener('popstate', handlePopState);
return () => {
window.removeEventListener('popstate', handlePopState);
};
}, []);
const currentStep = searchParams.get(`${id}.step`);
const currentContext = state?.[`${id}.context`];
const currentState = useMemo(() => {
return currentStep != null && currentContext != null
? ({
step: currentStep,
context: currentContext,
} as typeof initialState)
: initialState;
}, [currentStep, currentContext, initialState]);
const history: (typeof initialState)[] = useMemo(
() => state?.[`${id}.histories`] ?? [currentState],
[state, currentState],
);
const currentIndex = history.length - 1;
return useMemo(
() => ({
history,
currentIndex,
currentState,
push(newState) {
const newSearchParams = new URLSearchParams(searchParams);
newSearchParams.set(`${id}.step`, newState.step);
const newHistoryState = {
...state,
[`${id}.context`]: newState.context,
[`${id}.histories`]: [...(history ?? []), newState],
};
window.history.pushState(newHistoryState, '', `?${newSearchParams.toString()}`);
setState((prevHistoryState) => {
const newHistoryState = {
...prevHistoryState,
[`${id}.context`]: newState.context,
[`${id}.histories`]: [...(history ?? []), newState],
};
return newHistoryState;
});
},
replace(newState) {
const newSearchParams = new URLSearchParams(searchParams);
newSearchParams.set(`${id}.step`, newState.step);
const newHistoryState = {
...state,
[`${id}.context`]: newState.context,
[`${id}.histories`]: [...(history ?? []), newState],
};
window.history.replaceState(newHistoryState, '', `?${newSearchParams.toString()}`);
setState((prevHistoryState) => {
const newHistoryState = {
...prevHistoryState,
[`${id}.context`]: newState.context,
[`${id}.histories`]: [...(history ?? []), newState],
};
return newHistoryState;
});
},
go(index) {
window.history.go(index);
},
}),
[history, currentIndex, currentState, searchParams, id, state],
);
});