fix: use proper Automerge URL format for repo.find()
The issue was that repo.find() was creating a NEW document instead of
waiting for the existing one to sync from the network.
Changes:
- Use 'automerge:{documentId}' URL format for repo.find()
- Remove try-catch that was creating new documents
- Let repo.find() properly wait for network sync
This ensures all clients use the SAME document ID and can sync in real-time.
🤖 Generated with [Claude Code](https://claude.com/claude-code)
Co-Authored-By: Claude <noreply@anthropic.com>
This commit is contained in:
parent
02808d170e
commit
6ab966cc95
|
|
@ -3,7 +3,7 @@ import { TLStoreSnapshot } from "@tldraw/tldraw"
|
||||||
import { CloudflareNetworkAdapter } from "./CloudflareAdapter"
|
import { CloudflareNetworkAdapter } from "./CloudflareAdapter"
|
||||||
import { useAutomergeStoreV2, useAutomergePresence } from "./useAutomergeStoreV2"
|
import { useAutomergeStoreV2, useAutomergePresence } from "./useAutomergeStoreV2"
|
||||||
import { TLStoreWithStatus } from "@tldraw/tldraw"
|
import { TLStoreWithStatus } from "@tldraw/tldraw"
|
||||||
import { Repo } from "@automerge/automerge-repo"
|
import { Repo, parseAutomergeUrl, stringifyAutomergeUrl } from "@automerge/automerge-repo"
|
||||||
import { DocHandle } from "@automerge/automerge-repo"
|
import { DocHandle } from "@automerge/automerge-repo"
|
||||||
|
|
||||||
interface AutomergeSyncConfig {
|
interface AutomergeSyncConfig {
|
||||||
|
|
@ -146,18 +146,11 @@ export function useAutomergeSync(config: AutomergeSyncConfig): TLStoreWithStatus
|
||||||
let handle: DocHandle<TLStoreSnapshot>
|
let handle: DocHandle<TLStoreSnapshot>
|
||||||
|
|
||||||
if (documentId) {
|
if (documentId) {
|
||||||
// Try to find the existing document
|
// Convert document ID to Automerge URL format and find it
|
||||||
console.log(`🔍 Attempting to find document ${documentId}`)
|
console.log(`🔍 Finding document ${documentId} via network sync`)
|
||||||
try {
|
const docUrl = `automerge:${documentId}` as const
|
||||||
handle = await repo.find<TLStoreSnapshot>(documentId as any)
|
handle = await repo.find<TLStoreSnapshot>(docUrl as any)
|
||||||
console.log(`✅ Found document handle: ${documentId}`)
|
console.log(`✅ Got handle for document: ${handle.documentId}, isReady: ${handle.isReady()}`)
|
||||||
} catch (error) {
|
|
||||||
// Document not available yet - this can happen when it exists on server but not locally
|
|
||||||
console.log(`📝 Document ${documentId} not immediately available, creating new handle`)
|
|
||||||
// Create a new handle - the sync will handle merging with server state
|
|
||||||
handle = repo.create<TLStoreSnapshot>()
|
|
||||||
console.log(`📝 Created new handle ${handle.documentId}, will sync with server`)
|
|
||||||
}
|
|
||||||
} else {
|
} else {
|
||||||
// Create a new document and register its ID with the server
|
// Create a new document and register its ID with the server
|
||||||
handle = repo.create<TLStoreSnapshot>()
|
handle = repo.create<TLStoreSnapshot>()
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue