1
1
import { createContext , h } from 'preact' ;
2
2
import { useSignalEffect } from '@preact/signals' ;
3
3
import { paramsToQuery , toRange } from '../history.service.js' ;
4
- import { EVENT_RANGE_CHANGE , EVENT_SEARCH_COMMIT , KNOWN_ACTIONS , OVERSCAN_AMOUNT } from '../constants.js' ;
4
+ import { EVENT_RANGE_CHANGE , KNOWN_ACTIONS , OVERSCAN_AMOUNT } from '../constants.js' ;
5
5
import { usePlatformName } from '../types.js' ;
6
6
import { eventToTarget } from '../../../../shared/handlers.js' ;
7
- import { useContext } from 'preact/hooks' ;
7
+ import { useCallback , useContext } from 'preact/hooks' ;
8
8
import { useSelected } from './SelectionProvider.js' ;
9
9
import { useGlobalState } from './GlobalStateProvider.js' ;
10
10
import { useQueryDispatch } from './QueryProvider.js' ;
@@ -15,6 +15,13 @@ const HistoryServiceContext = createContext({
15
15
service : /** @type {import("../history.service.js").HistoryService } */ ( { } ) ,
16
16
} ) ;
17
17
18
+ /**
19
+ * @typedef {{kind: 'search-commit', params: URLSearchParams} } Action
20
+ */
21
+
22
+ // Create the context
23
+ const HistoryServiceDispatchContext = createContext ( /** @type {(action: Action)=>void } */ ( ( action ) => { } ) ) ;
24
+
18
25
/**
19
26
* Provides a context for the history service, allowing dependent components to access it.
20
27
* Everything that interacts with the service should be registered here
@@ -24,13 +31,36 @@ const HistoryServiceContext = createContext({
24
31
* @param {import("preact").ComponentChild } props.children
25
32
*/
26
33
export function HistoryServiceProvider ( { service, children } ) {
27
- return < HistoryServiceContext . Provider value = { { service } } > { children } </ HistoryServiceContext . Provider > ;
34
+ /** @type {(a: Action) => void } */
35
+ const dispatch = useCallback (
36
+ /**
37
+ * @param {Action } action
38
+ */
39
+ function dispatch ( action ) {
40
+ switch ( action . kind ) {
41
+ case 'search-commit' : {
42
+ const asQuery = paramsToQuery ( action . params ) ;
43
+ service . trigger ( asQuery ) ;
44
+ break ;
45
+ }
46
+ }
47
+ } ,
48
+ [ service ] ,
49
+ ) ;
50
+ return (
51
+ < HistoryServiceContext . Provider value = { { service } } >
52
+ < HistoryServiceDispatchContext . Provider value = { dispatch } > { children } </ HistoryServiceDispatchContext . Provider >
53
+ </ HistoryServiceContext . Provider >
54
+ ) ;
55
+ }
56
+
57
+ export function useHistoryServiceDispatch ( ) {
58
+ return useContext ( HistoryServiceDispatchContext ) ;
28
59
}
29
60
30
61
export function useGlobalHandlers ( ) {
31
62
const { service } = useContext ( HistoryServiceContext ) ;
32
63
const platformName = usePlatformName ( ) ;
33
- useSearchCommit ( service ) ;
34
64
useRangeChange ( service ) ;
35
65
useLinkClickHandler ( service , platformName ) ;
36
66
useButtonClickHandler ( service ) ;
@@ -82,36 +112,6 @@ function useDeleteItems(service) {
82
112
} ) ;
83
113
}
84
114
85
- /**
86
- * A hook that listens to the "search-commit" custom event and triggers the history service
87
- * with the parsed query parameter values from the event's detail object.
88
- *
89
- * This hook is used to bind the EVENT_SEARCH_COMMIT event with the associated service
90
- * logic for handling search parameters.
91
- *
92
- * @param {import('../history.service.js').HistoryService } service
93
- */
94
- function useSearchCommit ( service ) {
95
- useSignalEffect ( ( ) => {
96
- function handler ( /** @type {CustomEvent<{params: URLSearchParams}> } */ event ) {
97
- const detail = event . detail ;
98
- if ( detail && detail . params instanceof URLSearchParams ) {
99
- const asQuery = paramsToQuery ( detail . params ) ;
100
- service . trigger ( asQuery ) ;
101
- } else {
102
- console . error ( 'missing detail.params from search-commit event' ) ;
103
- }
104
- }
105
-
106
- window . addEventListener ( EVENT_SEARCH_COMMIT , handler ) ;
107
-
108
- // Cleanup the event listener on unmount
109
- return ( ) => {
110
- window . removeEventListener ( EVENT_SEARCH_COMMIT , handler ) ;
111
- } ;
112
- } ) ;
113
- }
114
-
115
115
/**
116
116
* @param {import('../history.service.js').HistoryService } service
117
117
*/
0 commit comments