-
Notifications
You must be signed in to change notification settings - Fork 9
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
♻️(frontend) create useProviderStore
We created useProviderStore, a store dedicated to managing the provider of the document. We created as well a new hook useCollaboration, it will be use to interact with the provider store. This refacto is a first step to implement the long polling.
- Loading branch information
Showing
9 changed files
with
126 additions
and
73 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
1 change: 1 addition & 0 deletions
1
src/frontend/apps/impress/src/features/docs/doc-management/hooks/index.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,2 @@ | ||
export * from './useCollaboration'; | ||
export * from './useTrans'; |
35 changes: 35 additions & 0 deletions
35
src/frontend/apps/impress/src/features/docs/doc-management/hooks/useCollaboration.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
import { useEffect } from 'react'; | ||
|
||
import { useCollaborationUrl } from '@/core/config'; | ||
import { useBroadcastStore } from '@/stores'; | ||
|
||
import { useProviderStore } from '../stores/useProviderStore'; | ||
import { Base64 } from '../types'; | ||
|
||
export const useCollaboration = (room?: string, initialContent?: Base64) => { | ||
const collaborationUrl = useCollaborationUrl(room); | ||
const { setBroadcastProvider } = useBroadcastStore(); | ||
const { provider, createProvider, destroyProvider } = useProviderStore(); | ||
|
||
useEffect(() => { | ||
if (!room || !collaborationUrl || provider) { | ||
return; | ||
} | ||
|
||
const newProvider = createProvider(collaborationUrl, room, initialContent); | ||
setBroadcastProvider(newProvider); | ||
}, [ | ||
provider, | ||
collaborationUrl, | ||
room, | ||
initialContent, | ||
createProvider, | ||
setBroadcastProvider, | ||
]); | ||
|
||
useEffect(() => { | ||
return () => { | ||
destroyProvider(); | ||
}; | ||
}, [destroyProvider]); | ||
}; |
1 change: 1 addition & 0 deletions
1
src/frontend/apps/impress/src/features/docs/doc-management/stores/index.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,2 @@ | ||
export * from './useDocStore'; | ||
export * from './useProviderStore'; |
43 changes: 2 additions & 41 deletions
43
src/frontend/apps/impress/src/features/docs/doc-management/stores/useDocStore.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
52 changes: 52 additions & 0 deletions
52
src/frontend/apps/impress/src/features/docs/doc-management/stores/useProviderStore.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
import { HocuspocusProvider } from '@hocuspocus/provider'; | ||
import * as Y from 'yjs'; | ||
import { create } from 'zustand'; | ||
|
||
import { Base64 } from '@/features/docs/doc-management'; | ||
|
||
export interface UseCollaborationStore { | ||
createProvider: ( | ||
providerUrl: string, | ||
storeId: string, | ||
initialDoc?: Base64, | ||
) => HocuspocusProvider; | ||
destroyProvider: () => void; | ||
provider: HocuspocusProvider | undefined; | ||
} | ||
|
||
const defaultValues = { | ||
provider: undefined, | ||
}; | ||
|
||
export const useProviderStore = create<UseCollaborationStore>((set, get) => ({ | ||
...defaultValues, | ||
createProvider: (wsUrl, storeId, initialDoc) => { | ||
const doc = new Y.Doc({ | ||
guid: storeId, | ||
}); | ||
|
||
if (initialDoc) { | ||
Y.applyUpdate(doc, Buffer.from(initialDoc, 'base64')); | ||
} | ||
|
||
const provider = new HocuspocusProvider({ | ||
url: wsUrl, | ||
name: storeId, | ||
document: doc, | ||
}); | ||
|
||
set({ | ||
provider, | ||
}); | ||
|
||
return provider; | ||
}, | ||
destroyProvider: () => { | ||
const provider = get().provider; | ||
if (provider) { | ||
provider.destroy(); | ||
} | ||
|
||
set(defaultValues); | ||
}, | ||
})); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters