-
Notifications
You must be signed in to change notification settings - Fork 43
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
useBootstrap hook #102
useBootstrap hook #102
Conversation
You can either join an existing documentID by hashroute, or create a new one (by not having any hashroute). A demo is available at https://github.com/neftaly/autoburger |
I don't like how the "onCreate" function ties into getting and setting document ID strings. I'm not yet sure how else to integrate it into the system, however. |
I really like the goal of this PR but I have a few review comments:
Overall though, I think something like this is a really good idea! The bootstrapping process is an important one to get right and the demo code currently does not :) |
Thanks for your comments. The behaviour of useHash can absolutely be internalized, I'm only using half of it anyway. I'm not yet sure what the right approach to bootstrapping is, so I don't actually know what to name this yet - I think that will come last :) I would like to separate hashroute behaviour from document setup, though I'm not yet sure how to then connect the two. The document setup could take a function for various setup strategies (checking localstorage etc). I would also like to consider basic hashroute interop with routing libraries. |
Let's see -- can we agree on the pattern here? You have a documentId of some kind in a queryParam, or if it's not present, you might have it in a data store, and if not that, we make one. This feels like a cascading set of options that might read something like
What I like about this approach is that it reads clearly but exposes what's happening. If this, then that. I could be persuaded by something else but that's at least a straw-man proposal to keep the conversation going. What do you think? |
This sounds like a good strategy, I will follow up on it. Hooks won't work exactly like this but I like the idea of a simple composition you could edit for your own needs. I would like to consider how a physically local document ID source could tie into this, for example from a bluetooth beacon or mDNS advertisement, however this is not a significant concern. |
Looking forward to the patch! |
This commit internalizes useHash. It also (optionally) looks up document IDs in localStorage, if a hashroute ID is unavailable. If a document is already loaded, but you want to create a new one:
I feel like this is simple enough to be included in the docs, and not part of the hook. |
// Update hashroute & localStorage on changes | ||
useEffect(() => setDocumentId(handle.documentId), [hash, handle.documentId]); | ||
|
||
// TODO: Should we return a handle, not a documentID? |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think this is a better strategy but want to check first
return existingDocumentId | ||
? repo.find(existingDocumentId) | ||
: createDocument(repo, onCreate); | ||
} catch (e) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm assuming repo.find
will throw on invalid document ID
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
it will ... soon!
Merged 1d118fd |
This is an experimental hook for getting the current
documentId
from the hashroute URL, and creating a new document if one is not set.My goal is to remove as much document setup boilerplate as possible from the app entrypoint (i.e.
main.jsx
), and to support hashroute changes.For example, we'd remove most of this code from automerge-repo-demo-todo/src/main.tsx:
Instead of
<App>
taking a rootId prop, we'd just use the hook (with an additional onCreate argument for setting initial state, as well as a getter/setter for custom hash routing).