From 6653d19842eb59ea2190507ed34b428f56127307 Mon Sep 17 00:00:00 2001 From: Jeff Emmett <46964190+Jeff-Emmett@users.noreply.github.com> Date: Wed, 27 Nov 2024 12:25:04 +0700 Subject: [PATCH] remove copy file creating problems --- package.json | 3 +- src/hooks/copy useLocalStorageRoom.ts | 103 -------------------------- 2 files changed, 2 insertions(+), 104 deletions(-) delete mode 100644 src/hooks/copy useLocalStorageRoom.ts diff --git a/package.json b/package.json index eb40e52..4c50064 100644 --- a/package.json +++ b/package.json @@ -17,6 +17,7 @@ "license": "ISC", "dependencies": { "@dimforge/rapier2d": "^0.11.2", + "@tldraw/assets": "^3.5.0", "@tldraw/sync": "^3.4.1", "@tldraw/sync-core": "^3.4.1", "@tldraw/tldraw": "^3.4.1", @@ -60,4 +61,4 @@ "vite-plugin-wasm": "^3.2.2", "wrangler": "^3.88.0" } -} \ No newline at end of file +} diff --git a/src/hooks/copy useLocalStorageRoom.ts b/src/hooks/copy useLocalStorageRoom.ts deleted file mode 100644 index 7816591..0000000 --- a/src/hooks/copy useLocalStorageRoom.ts +++ /dev/null @@ -1,103 +0,0 @@ -import useLocalStorageState from 'use-local-storage-state'; -import { TLRecord, createTLStore, SerializedStore, Editor, StoreSchema, TLStoreProps } from '@tldraw/tldraw'; -import { customSchema } from '../../worker/TldrawDurableObject'; -import { useMemo, useCallback, useEffect, useState } from 'react'; -import { useSync } from '@tldraw/sync'; -import { WORKER_URL } from '../components/Board'; -import { TLRecord as TLSchemaRecord } from '@tldraw/tlschema' -import { defaultAssetUrls } from '@tldraw/assets' - -const CACHE_VERSION = '1.0'; - -export function useLocalStorageRoom(roomId: string) { - const [isOnline, setIsOnline] = useState(navigator.onLine); - const storageKey = `tldraw_board_${roomId}_v${CACHE_VERSION}`; - - const [records, setRecords] = useLocalStorageState>(storageKey, { - defaultValue: createTLStore({ - schema: customSchema as unknown as StoreSchema - }).serialize() - }); - - // Create a persistent store - const baseStore = useMemo(() => { - return createTLStore({ - schema: customSchema as unknown as StoreSchema, - initialData: records, - }) - }, [records]); - - // Use sync with the base store - const syncedStore = useSync({ - uri: `${WORKER_URL.replace('https://', 'wss://')}/connect/${roomId}`, - schema: customSchema, - store: baseStore, - assets: defaultAssetUrls - }); - - // Handle online/offline transitions - useEffect(() => { - const handleOnline = () => { - setIsOnline(true); - if (syncedStore?.store) { - const filteredRecords = filterNonCameraRecords(records); - syncedStore.store.mergeRemoteChanges(() => { - Object.values(filteredRecords).forEach(record => { - syncedStore.store.put([record as unknown as TLSchemaRecord]); - }); - }); - } - }; - - const handleOffline = () => setIsOnline(false); - - window.addEventListener('online', handleOnline); - window.addEventListener('offline', handleOffline); - - return () => { - window.removeEventListener('online', handleOnline); - window.removeEventListener('offline', handleOffline); - }; - }, [records, syncedStore?.store]); - - const filterNonCameraRecords = (data: SerializedStore) => { - return Object.fromEntries( - Object.entries(data).filter(([_, record]) => { - return (record as TLRecord).typeName !== 'camera' && - (record as TLRecord).typeName !== 'instance_page_state' && - (record as TLRecord).typeName !== 'instance_presence'; - }) - ) as SerializedStore; - }; - - // Sync with server store when online - useEffect(() => { - if (!isOnline || !syncedStore?.store) return; - - const syncInterval = setInterval(() => { - const serverRecords = syncedStore.store.allRecords(); - if (Object.keys(serverRecords).length > 0) { - setRecords(syncedStore.store.serialize() as typeof records); - } - }, 5000); - - return () => clearInterval(syncInterval); - }, [isOnline, syncedStore?.store, setRecords]); - - const store = useMemo(() => { - if (isOnline && syncedStore?.store) { - return syncedStore.store; - } - return createTLStore({ - schema: customSchema as unknown as StoreSchema, - initialData: records, - }); - }, [isOnline, syncedStore?.store, records]); - - return { - store, - records, - setRecords, - isOnline - }; -} \ No newline at end of file