-
Notifications
You must be signed in to change notification settings - Fork 8.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
move useUrlTracker to kibana_react, as it is a react hook
improve
- Loading branch information
Showing
8 changed files
with
145 additions
and
23 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
/* | ||
* Licensed to Elasticsearch B.V. under one or more contributor | ||
* license agreements. See the NOTICE file distributed with | ||
* this work for additional information regarding copyright | ||
* ownership. Elasticsearch B.V. licenses this file to you under | ||
* the Apache License, Version 2.0 (the "License"); you may | ||
* not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, | ||
* software distributed under the License is distributed on an | ||
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY | ||
* KIND, either express or implied. See the License for the | ||
* specific language governing permissions and limitations | ||
* under the License. | ||
*/ | ||
|
||
export { useUrlTracker } from './use_url_tracker'; |
70 changes: 70 additions & 0 deletions
70
src/plugins/kibana_react/public/use_url_tracker/use_url_tracker.test.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
/* | ||
* Licensed to Elasticsearch B.V. under one or more contributor | ||
* license agreements. See the NOTICE file distributed with | ||
* this work for additional information regarding copyright | ||
* ownership. Elasticsearch B.V. licenses this file to you under | ||
* the Apache License, Version 2.0 (the "License"); you may | ||
* not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, | ||
* software distributed under the License is distributed on an | ||
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY | ||
* KIND, either express or implied. See the License for the | ||
* specific language governing permissions and limitations | ||
* under the License. | ||
*/ | ||
|
||
import { renderHook } from '@testing-library/react-hooks'; | ||
import { useUrlTracker } from './use_url_tracker'; | ||
import { StubBrowserStorage } from 'test_utils/stub_browser_storage'; | ||
import { createMemoryHistory } from 'history'; | ||
|
||
describe('useUrlTracker', () => { | ||
const key = 'key'; | ||
let storage = new StubBrowserStorage(); | ||
let history = createMemoryHistory(); | ||
beforeEach(() => { | ||
storage = new StubBrowserStorage(); | ||
history = createMemoryHistory(); | ||
}); | ||
|
||
it('should track history changes and save them to storage', () => { | ||
expect(storage.getItem(key)).toBeNull(); | ||
const { unmount } = renderHook(() => { | ||
useUrlTracker(key, history, () => false, storage); | ||
}); | ||
expect(storage.getItem(key)).toBe('/'); | ||
history.push('/change'); | ||
expect(storage.getItem(key)).toBe('/change'); | ||
unmount(); | ||
history.push('/other-change'); | ||
expect(storage.getItem(key)).toBe('/change'); | ||
}); | ||
|
||
it('by default should restore initial url', () => { | ||
storage.setItem(key, '/change'); | ||
renderHook(() => { | ||
useUrlTracker(key, history, undefined, storage); | ||
}); | ||
expect(history.location.pathname).toBe('/change'); | ||
}); | ||
|
||
it('should restore initial url if shouldRestoreUrl cb returns true', () => { | ||
storage.setItem(key, '/change'); | ||
renderHook(() => { | ||
useUrlTracker(key, history, () => true, storage); | ||
}); | ||
expect(history.location.pathname).toBe('/change'); | ||
}); | ||
|
||
it('should not restore initial url if shouldRestoreUrl cb returns false', () => { | ||
storage.setItem(key, '/change'); | ||
renderHook(() => { | ||
useUrlTracker(key, history, () => false, storage); | ||
}); | ||
expect(history.location.pathname).toBe('/'); | ||
}); | ||
}); |
52 changes: 52 additions & 0 deletions
52
src/plugins/kibana_react/public/use_url_tracker/use_url_tracker.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
/* | ||
* Licensed to Elasticsearch B.V. under one or more contributor | ||
* license agreements. See the NOTICE file distributed with | ||
* this work for additional information regarding copyright | ||
* ownership. Elasticsearch B.V. licenses this file to you under | ||
* the Apache License, Version 2.0 (the "License"); you may | ||
* not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, | ||
* software distributed under the License is distributed on an | ||
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY | ||
* KIND, either express or implied. See the License for the | ||
* specific language governing permissions and limitations | ||
* under the License. | ||
*/ | ||
|
||
import { History } from 'history'; | ||
import { useLayoutEffect } from 'react'; | ||
import { createUrlTracker } from '../../../kibana_utils/public/'; | ||
|
||
/** | ||
* State management url_tracker in react hook form | ||
* | ||
* Replicates what src/legacy/ui/public/chrome/api/nav.ts did | ||
* Persists the url in sessionStorage so it could be restored if navigated back to the app | ||
* | ||
* @param key - key to use in storage | ||
* @param history - history instance to use | ||
* @param shouldRestoreUrl - cb if url should be restored | ||
* @param storage - storage to use. window.sessionStorage is default | ||
*/ | ||
export function useUrlTracker( | ||
key: string, | ||
history: History, | ||
shouldRestoreUrl: (urlToRestore: string) => boolean = () => true, | ||
storage: Storage = sessionStorage | ||
) { | ||
useLayoutEffect(() => { | ||
const urlTracker = createUrlTracker(key, storage); | ||
const urlToRestore = urlTracker.getTrackedUrl(); | ||
if (urlToRestore && shouldRestoreUrl(urlToRestore)) { | ||
history.replace(urlToRestore); | ||
} | ||
const stopTrackingUrl = urlTracker.startTrackingUrl(history); | ||
return () => { | ||
stopTrackingUrl(); | ||
}; | ||
}, [key, history]); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters