tldraw/packages/editor/src/lib/utils/sync/hardReset.ts
Steve Ruiz 2f28d7c6f8
Protect local storage calls (#3043)
This PR provides some safe wrappers for local storage calls. Local
storage is not available in all environments (for example, a React
Native web view). The PR also adds an eslint rule preventing direct
calls to local / session storage.

### Change Type

- [x] `patch` — Bug fix

### Release Notes

- Fixes a bug that could cause crashes in React Native webviews.
2024-03-04 13:37:09 +00:00

28 lines
746 B
TypeScript

import { clearLocalStorage } from '@tldraw/utils'
import { deleteDB } from 'idb'
import { getAllIndexDbNames } from './indexedDb'
/**
* Clear the database of all data associated with tldraw.
*
* @public */
export async function hardReset({ shouldReload = true } = {}) {
clearLocalStorage()
await Promise.all(getAllIndexDbNames().map((db) => deleteDB(db)))
clearLocalStorage()
if (shouldReload) {
if (typeof window !== 'undefined') {
window.location.reload()
}
}
}
if (typeof window !== 'undefined') {
if (process.env.NODE_ENV === 'development') {
;(window as any).hardReset = hardReset
}
// window.__tldraw__hardReset is used to inject the logic into the tldraw library
;(window as any).__tldraw__hardReset = hardReset
}