diff --git a/src/frontend/apps/impress/src/features/docs/doc-editor/hook/useSaveDoc.tsx b/src/frontend/apps/impress/src/features/docs/doc-editor/hook/useSaveDoc.tsx index ca1ed0054..ae00dac95 100644 --- a/src/frontend/apps/impress/src/features/docs/doc-editor/hook/useSaveDoc.tsx +++ b/src/frontend/apps/impress/src/features/docs/doc-editor/hook/useSaveDoc.tsx @@ -2,12 +2,10 @@ import { useRouter } from 'next/router'; import { useCallback, useEffect, useRef, useState } from 'react'; import * as Y from 'yjs'; -import { useUpdateDoc } from '@/features/docs/doc-management/'; +import { toBase64, useUpdateDoc } from '@/features/docs/doc-management/'; import { KEY_LIST_DOC_VERSIONS } from '@/features/docs/doc-versioning'; import { isFirefox } from '@/utils/userAgent'; -import { toBase64 } from '../utils'; - const useSaveDoc = (docId: string, doc: Y.Doc, canSave: boolean) => { const { mutate: updateDoc } = useUpdateDoc({ listInvalideQueries: [KEY_LIST_DOC_VERSIONS], diff --git a/src/frontend/apps/impress/src/features/docs/doc-editor/utils.ts b/src/frontend/apps/impress/src/features/docs/doc-editor/utils.ts index a3d311180..325a2304e 100644 --- a/src/frontend/apps/impress/src/features/docs/doc-editor/utils.ts +++ b/src/frontend/apps/impress/src/features/docs/doc-editor/utils.ts @@ -22,6 +22,3 @@ function hslToHex(h: number, s: number, l: number) { }; return `#${f(0)}${f(8)}${f(4)}`; } - -export const toBase64 = (str: Uint8Array) => - Buffer.from(str).toString('base64'); diff --git a/src/frontend/apps/impress/src/features/docs/doc-management/hooks/useCollaboration.tsx b/src/frontend/apps/impress/src/features/docs/doc-management/hooks/useCollaboration.tsx index 74e8214d5..2f1c50477 100644 --- a/src/frontend/apps/impress/src/features/docs/doc-management/hooks/useCollaboration.tsx +++ b/src/frontend/apps/impress/src/features/docs/doc-management/hooks/useCollaboration.tsx @@ -4,11 +4,10 @@ import * as Y from 'yjs'; import { useCollaborationUrl } from '@/core/config'; import { useBroadcastStore } from '@/stores'; -import { toBase64 } from '../../doc-editor'; import { syncDocPolling } from '../api/syncDocPolling'; import { useProviderStore } from '../stores/useProviderStore'; import { Base64 } from '../types'; -import { base64ToYDoc } from '../utils'; +import { base64ToYDoc, toBase64 } from '../utils'; export const useCollaboration = (room?: string, initialContent?: Base64) => { const collaborationUrl = useCollaborationUrl(room); @@ -19,17 +18,19 @@ export const useCollaboration = (room?: string, initialContent?: Base64) => { const intervalRef = useRef(); useEffect(() => { - if (!room || !collaborationUrl || provider) { + if (!room || !collaborationUrl?.wsUrl || provider) { return; } - console.log('Create provider'); - - const newProvider = createProvider(collaborationUrl, room, initialContent); + const newProvider = createProvider( + collaborationUrl.wsUrl, + room, + initialContent, + ); setBroadcastProvider(newProvider); }, [ provider, - collaborationUrl, + collaborationUrl?.wsUrl, room, initialContent, createProvider, @@ -37,8 +38,6 @@ export const useCollaboration = (room?: string, initialContent?: Base64) => { ]); useEffect(() => { - console.log('isProviderFailure:', isProviderFailure); - console.log('intervalRef.current:', intervalRef.current); const clearCurrentInterval = () => { if (intervalRef.current) { clearInterval(intervalRef.current); @@ -67,14 +66,10 @@ export const useCollaboration = (room?: string, initialContent?: Base64) => { .then((response) => { const { yDoc64 } = response; - console.log('Poll triggered'); - if (!yDoc64) { return; } - console.log('document updated'); - const yDoc = base64ToYDoc(yDoc64); Y.applyUpdate(provider.document, Y.encodeStateAsUpdate(yDoc)); }) @@ -84,7 +79,6 @@ export const useCollaboration = (room?: string, initialContent?: Base64) => { }, pollingInterval); return () => { - console.log('Clearing interval'); clearCurrentInterval(); }; }, [ @@ -96,7 +90,6 @@ export const useCollaboration = (room?: string, initialContent?: Base64) => { useEffect(() => { return () => { - console.log('Destroying provider'); destroyProvider(); }; }, [destroyProvider]); diff --git a/src/frontend/apps/impress/src/features/docs/doc-management/stores/useProviderStore.tsx b/src/frontend/apps/impress/src/features/docs/doc-management/stores/useProviderStore.tsx index 5630ad83c..58ca76f3b 100644 --- a/src/frontend/apps/impress/src/features/docs/doc-management/stores/useProviderStore.tsx +++ b/src/frontend/apps/impress/src/features/docs/doc-management/stores/useProviderStore.tsx @@ -2,13 +2,11 @@ import { HocuspocusProvider } from '@hocuspocus/provider'; import * as Y from 'yjs'; import { create } from 'zustand'; -import { CollaborationUrl } from '@/core/config'; import { Base64 } from '@/features/docs/doc-management'; -import { isFirefox } from '@/utils'; export interface UseCollaborationStore { createProvider: ( - providerUrl: CollaborationUrl, + providerUrl: string, storeId: string, initialDoc?: Base64, ) => HocuspocusProvider; @@ -28,7 +26,7 @@ const defaultValues = { export const useProviderStore = create((set, get) => ({ ...defaultValues, - createProvider: (providerUrl, storeId, initialDoc) => { + createProvider: (wsUrl, storeId, initialDoc) => { const doc = new Y.Doc({ guid: storeId, }); @@ -37,28 +35,16 @@ export const useProviderStore = create((set, get) => ({ Y.applyUpdate(doc, Buffer.from(initialDoc, 'base64')); } - console.log('navigator.userAgent', navigator.userAgent); - console.log('providerUrl', providerUrl); - const withWS = isFirefox(); - //const withWS = true; - const wsUrl = withWS - ? providerUrl.wsUrl //'ws://localhost:4444/collaboration/ws/?room=' + storeId - : 'ws://localhost:6666'; - const provider = new HocuspocusProvider({ url: wsUrl, name: storeId, document: doc, onConnect: () => { - console.log('Provider connected'); set({ failureCount: 0, isProviderFailure: false, }); }, - onAuthenticationFailed: () => { - console.error('Authentication failed'); - }, onClose: () => { set({ failureCount: get().failureCount + 1, @@ -68,18 +54,10 @@ export const useProviderStore = create((set, get) => ({ !get().isProviderFailure && get().failureCount > get().maxFailureCount ) { - console.error('Max failure count reached'); set({ isProviderFailure: true, }); } - console.log('Provider closed'); - }, - onDisconnect: () => { - console.log('Provider disconnected'); - }, - onDestroy() { - console.log('Provider destroyed'); }, }); diff --git a/src/frontend/apps/impress/src/features/docs/doc-management/utils.ts b/src/frontend/apps/impress/src/features/docs/doc-management/utils.ts index 2c229128e..2707e7468 100644 --- a/src/frontend/apps/impress/src/features/docs/doc-management/utils.ts +++ b/src/frontend/apps/impress/src/features/docs/doc-management/utils.ts @@ -12,6 +12,9 @@ export const currentDocRole = (abilities: Doc['abilities']): Role => { : Role.READER; }; +export const toBase64 = (str: Uint8Array) => + Buffer.from(str).toString('base64'); + export const base64ToYDoc = (base64: string) => { const uint8Array = Buffer.from(base64, 'base64'); const ydoc = new Y.Doc(); diff --git a/src/frontend/servers/y-provider/src/handlers/collaborationHandler.ts b/src/frontend/servers/y-provider/src/handlers/collaborationConnectionsHandler.ts similarity index 100% rename from src/frontend/servers/y-provider/src/handlers/collaborationHandler.ts rename to src/frontend/servers/y-provider/src/handlers/collaborationConnectionsHandler.ts diff --git a/src/frontend/servers/y-provider/src/handlers/index.ts b/src/frontend/servers/y-provider/src/handlers/index.ts index 5fb5a788f..051650f41 100644 --- a/src/frontend/servers/y-provider/src/handlers/index.ts +++ b/src/frontend/servers/y-provider/src/handlers/index.ts @@ -1,3 +1,3 @@ export * from './collaborationResetConnectionsHandler'; -export * from './collaborationHandler'; +export * from './collaborationConnectionsHandler'; export * from './convertMarkdownHandler';