fix: Filter throwing an error on Embedded Dashboard (#21157)

* fix: Filter throwing an error on Embedded Dashboard

* use short_id if storage not available

* avoid infinite loop
This commit is contained in:
Lily Kuang 2022-08-24 09:38:20 -07:00 committed by GitHub
parent 53de94c047
commit 604e30b3f0
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -17,6 +17,7 @@
* under the License.
*/
import { useEffect, useState } from 'react';
import shortid from 'shortid';
import { BroadcastChannel } from 'broadcast-channel';
interface TabIdChannelMessage {
@ -32,7 +33,21 @@ const channel = new BroadcastChannel<TabIdChannelMessage>('tab_id_channel');
export function useTabId() {
const [tabId, setTabId] = useState<string>();
function isStorageAvailable() {
try {
return window.localStorage && window.sessionStorage;
} catch (error) {
return false;
}
}
useEffect(() => {
if (!isStorageAvailable()) {
if (!tabId) {
setTabId(shortid.generate());
}
return;
}
const updateTabId = () => {
const lastTabId = window.localStorage.getItem('last_tab_id');
const newTabId = String(