Skip to content

Commit b8d808c

Browse files
committed
move useUrlTracker to kibana_react, as it is a react hook
improve
1 parent e49d9ff commit b8d808c

8 files changed

Lines changed: 145 additions & 23 deletions

File tree

examples/state_containers_examples/public/todo.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,9 +40,9 @@ import {
4040
createStateContainerReactHelpers,
4141
PureTransition,
4242
syncStates,
43-
useUrlTracker,
4443
getStateFromKbnUrl,
4544
} from '../../../src/plugins/kibana_utils/public';
45+
import { useUrlTracker } from '../../../src/plugins/kibana_react/public';
4646
import {
4747
defaultState,
4848
pureTransitions,

src/plugins/kibana_react/public/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,4 +25,5 @@ export * from './overlays';
2525
export * from './ui_settings';
2626
export * from './field_icon';
2727
export * from './table_list_view';
28+
export { useUrlTracker } from './use_url_tracker';
2829
export { toMountPoint } from './util';
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
/*
2+
* Licensed to Elasticsearch B.V. under one or more contributor
3+
* license agreements. See the NOTICE file distributed with
4+
* this work for additional information regarding copyright
5+
* ownership. Elasticsearch B.V. licenses this file to you under
6+
* the Apache License, Version 2.0 (the "License"); you may
7+
* not use this file except in compliance with the License.
8+
* You may obtain a copy of the License at
9+
*
10+
* http://www.apache.org/licenses/LICENSE-2.0
11+
*
12+
* Unless required by applicable law or agreed to in writing,
13+
* software distributed under the License is distributed on an
14+
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15+
* KIND, either express or implied. See the License for the
16+
* specific language governing permissions and limitations
17+
* under the License.
18+
*/
19+
20+
export { useUrlTracker } from './use_url_tracker';
Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
/*
2+
* Licensed to Elasticsearch B.V. under one or more contributor
3+
* license agreements. See the NOTICE file distributed with
4+
* this work for additional information regarding copyright
5+
* ownership. Elasticsearch B.V. licenses this file to you under
6+
* the Apache License, Version 2.0 (the "License"); you may
7+
* not use this file except in compliance with the License.
8+
* You may obtain a copy of the License at
9+
*
10+
* http://www.apache.org/licenses/LICENSE-2.0
11+
*
12+
* Unless required by applicable law or agreed to in writing,
13+
* software distributed under the License is distributed on an
14+
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15+
* KIND, either express or implied. See the License for the
16+
* specific language governing permissions and limitations
17+
* under the License.
18+
*/
19+
20+
import { renderHook } from '@testing-library/react-hooks';
21+
import { useUrlTracker } from './use_url_tracker';
22+
import { StubBrowserStorage } from 'test_utils/stub_browser_storage';
23+
import { createMemoryHistory } from 'history';
24+
25+
describe('useUrlTracker', () => {
26+
const key = 'key';
27+
let storage = new StubBrowserStorage();
28+
let history = createMemoryHistory();
29+
beforeEach(() => {
30+
storage = new StubBrowserStorage();
31+
history = createMemoryHistory();
32+
});
33+
34+
it('should track history changes and save them to storage', () => {
35+
expect(storage.getItem(key)).toBeNull();
36+
const { unmount } = renderHook(() => {
37+
useUrlTracker(key, history, () => false, storage);
38+
});
39+
expect(storage.getItem(key)).toBe('/');
40+
history.push('/change');
41+
expect(storage.getItem(key)).toBe('/change');
42+
unmount();
43+
history.push('/other-change');
44+
expect(storage.getItem(key)).toBe('/change');
45+
});
46+
47+
it('by default should restore initial url', () => {
48+
storage.setItem(key, '/change');
49+
renderHook(() => {
50+
useUrlTracker(key, history, undefined, storage);
51+
});
52+
expect(history.location.pathname).toBe('/change');
53+
});
54+
55+
it('should restore initial url if shouldRestoreUrl cb returns true', () => {
56+
storage.setItem(key, '/change');
57+
renderHook(() => {
58+
useUrlTracker(key, history, () => true, storage);
59+
});
60+
expect(history.location.pathname).toBe('/change');
61+
});
62+
63+
it('should not restore initial url if shouldRestoreUrl cb returns false', () => {
64+
storage.setItem(key, '/change');
65+
renderHook(() => {
66+
useUrlTracker(key, history, () => false, storage);
67+
});
68+
expect(history.location.pathname).toBe('/');
69+
});
70+
});
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
/*
2+
* Licensed to Elasticsearch B.V. under one or more contributor
3+
* license agreements. See the NOTICE file distributed with
4+
* this work for additional information regarding copyright
5+
* ownership. Elasticsearch B.V. licenses this file to you under
6+
* the Apache License, Version 2.0 (the "License"); you may
7+
* not use this file except in compliance with the License.
8+
* You may obtain a copy of the License at
9+
*
10+
* http://www.apache.org/licenses/LICENSE-2.0
11+
*
12+
* Unless required by applicable law or agreed to in writing,
13+
* software distributed under the License is distributed on an
14+
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15+
* KIND, either express or implied. See the License for the
16+
* specific language governing permissions and limitations
17+
* under the License.
18+
*/
19+
20+
import { History } from 'history';
21+
import { useLayoutEffect } from 'react';
22+
import { createUrlTracker } from '../../../kibana_utils/public/';
23+
24+
/**
25+
* State management url_tracker in react hook form
26+
*
27+
* Replicates what src/legacy/ui/public/chrome/api/nav.ts did
28+
* Persists the url in sessionStorage so it could be restored if navigated back to the app
29+
*
30+
* @param key - key to use in storage
31+
* @param history - history instance to use
32+
* @param shouldRestoreUrl - cb if url should be restored
33+
* @param storage - storage to use. window.sessionStorage is default
34+
*/
35+
export function useUrlTracker(
36+
key: string,
37+
history: History,
38+
shouldRestoreUrl: (urlToRestore: string) => boolean = () => true,
39+
storage: Storage = sessionStorage
40+
) {
41+
useLayoutEffect(() => {
42+
const urlTracker = createUrlTracker(key, storage);
43+
const urlToRestore = urlTracker.getTrackedUrl();
44+
if (urlToRestore && shouldRestoreUrl(urlToRestore)) {
45+
history.replace(urlToRestore);
46+
}
47+
const stopTrackingUrl = urlTracker.startTrackingUrl(history);
48+
return () => {
49+
stopTrackingUrl();
50+
};
51+
}, [key, history]);
52+
}

src/plugins/kibana_utils/public/index.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,6 @@ export {
4040
unhashUrl,
4141
unhashQuery,
4242
createUrlTracker,
43-
useUrlTracker,
4443
createKbnUrlControls,
4544
getStateFromKbnUrl,
4645
getStatesFromKbnUrl,

src/plugins/kibana_utils/public/state_management/url/index.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,4 +25,4 @@ export {
2525
getStatesFromKbnUrl,
2626
IKbnUrlControls,
2727
} from './kbn_url_storage';
28-
export { createUrlTracker, useUrlTracker } from './url_tracker';
28+
export { createUrlTracker } from './url_tracker';

src/plugins/kibana_utils/public/state_management/url/url_tracker.ts

Lines changed: 0 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@
1818
*/
1919

2020
import { createBrowserHistory, History, Location } from 'history';
21-
import { useLayoutEffect } from 'react';
2221
import { getRelativeToHistoryPath } from './kbn_url_storage';
2322

2423
export interface IUrlTracker {
@@ -48,22 +47,3 @@ export function createUrlTracker(key: string, storage: Storage = sessionStorage)
4847
},
4948
};
5049
}
51-
52-
export function useUrlTracker(
53-
key: string,
54-
history: History,
55-
shouldRestoreUrl: (urlToRestore: string) => boolean = () => true,
56-
storage: Storage = sessionStorage
57-
) {
58-
useLayoutEffect(() => {
59-
const urlTracker = createUrlTracker(key, storage);
60-
const urlToRestore = urlTracker.getTrackedUrl();
61-
if (urlToRestore && shouldRestoreUrl(urlToRestore)) {
62-
history.replace(urlToRestore);
63-
}
64-
const stopTrackingUrl = urlTracker.startTrackingUrl(history);
65-
return () => {
66-
stopTrackingUrl();
67-
};
68-
}, [key, history]);
69-
}

0 commit comments

Comments
 (0)