Skip to content

Commit

Permalink
Remove localStorage from useBootstrap
Browse files Browse the repository at this point in the history
  • Loading branch information
neftaly committed Feb 1, 2024
1 parent aadf0f3 commit fd576a5
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 9 deletions.
2 changes: 1 addition & 1 deletion packages/automerge-repo-react-hooks/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ These hooks are provided as helpers for using Automerge in your React project.

#### [useBootstrap](./src/useBootstrap.ts)
This hook is used to load a document based on the URL hash, for example `//myapp/#documentId=[document ID]`.
It can also load the document ID from localStorage, or create a new document if none is specified.
It can also create a new document if none is specified.

#### [useLocalAwareness](./src/useLocalAwareness.ts) & [useRemoteAwareness](./src/useRemoteAwareness.ts)
These hooks implement ephemeral awareness/presence, similar to [Yjs Awareness](https://docs.yjs.dev/getting-started/adding-awareness).
Expand Down
14 changes: 6 additions & 8 deletions packages/automerge-repo-react-hooks/src/useBootstrap.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,19 +41,18 @@ const setQueryParamValue = (
}

const getAutomergeUrl = (key: string, hash: string) =>
key && (getQueryParamValue(key, hash) || localStorage.getItem(key))
key && getQueryParamValue(key, hash)

const setAutomergeUrl = (key: string, automergeUrl: AutomergeUrl) => {
if (key) {
// Only set URL hash if automerge URL changed
if (automergeUrl !== getQueryParamValue(key, window.location.hash))
setHash(setQueryParamValue(key, automergeUrl, window.location.hash))
}
if (key) localStorage.setItem(key, automergeUrl)
}

export interface UseBootstrapOptions<T> {
/** Key to use for the URL hash and localStorage */
/** Key to use for the URL hash */
key?: string
/** Function returning a document handle called if lookup fails. Defaults to repo.create() */
onNoDocument?: (repo: Repo) => DocHandle<T>
Expand All @@ -65,15 +64,14 @@ export interface UseBootstrapOptions<T> {
* This hook is used to set up a single document as the base of an app session.
* This is a common pattern for simple multiplayer apps with shareable URLs.
*
* It will first check for the automergeUrl in the URL hash:
* It will check for the automergeUrl in the URL hash:
* //myapp/#automergeUrl=[document URL]
* Failing that, it will check for a `automergeUrl` key in localStorage.
* Failing that, it will call onNoDocument, expecting a handle to be returned.
*
* The URL and localStorage will then be updated.
* The URL will then be updated.
* Finally, it will return the Automerge document's URL.
*
* @param {string?} props.key Key to use for the URL hash and localStorage
* @param {string?} props.key Key to use for the URL hash
* @param {function?} props.fallback Function returning a document handle called if lookup fails. Defaults to repo.create()
* @param {function?} props.onInvalidAutomergeUrl Function to call if URL is invalid; signature (error) => (repo, onCreate)
* @returns {DocHandle} The document handle
Expand All @@ -100,7 +98,7 @@ export const useBootstrap = <T>({
}
}, [hash, repo, onNoDocument, onInvalidAutomergeUrl])

// Update hashroute & localStorage on changes
// Update hashroute on changes
useEffect(() => {
if (handle) {
setAutomergeUrl(key, handle.url)
Expand Down

0 comments on commit fd576a5

Please sign in to comment.