🗃️(frontend) add initial pad to webrtcserver

It is the webrtc server that will be responsible
for managing the data of the BlockNote Editor.

So to restore the data of a saved pads, we have to
add the initial pad to the room of the webrtc
server, it will then update the data of BlockNote
Editor.
The webrtc server accept Y.Doc, so we
convert our base64 data from Minio to make it
compatible with the server.

By doing so, we avoid the problem of
data lost when multiple users are connecting
and one user already updated the data of the
BlockNote Editor before saving it.
This commit is contained in:
Anthony LC
2024-05-17 23:06:29 +02:00
committed by Anthony LC
parent 64d508c260
commit 7aeea18202
2 changed files with 11 additions and 5 deletions

View File

@@ -23,7 +23,7 @@ export const BlockNoteEditor = ({ pad }: BlockNoteEditorProps) => {
const provider = padsStore?.[pad.id]?.provider;
if (!provider) {
createProvider(pad.id);
createProvider(pad.id, pad.content);
return null;
}

View File

@@ -5,7 +5,7 @@ import { create } from 'zustand';
import { signalingUrl } from '@/core';
import { Pad } from '../types';
import { Base64, Pad } from '../types';
export interface PadStore {
padsStore: {
@@ -14,7 +14,7 @@ export interface PadStore {
editor?: BlockNoteEditor;
};
};
createProvider: (padId: Pad['id']) => WebrtcProvider;
createProvider: (padId: Pad['id'], initialDoc: Base64) => WebrtcProvider;
setEditor: (padId: Pad['id'], editor: BlockNoteEditor) => void;
}
@@ -24,8 +24,14 @@ const initialState = {
export const usePadStore = create<PadStore>((set) => ({
padsStore: initialState.padsStore,
createProvider: (padId: string) => {
const provider = new WebrtcProvider(padId, new Y.Doc(), {
createProvider: (padId: string, initialDoc: Base64) => {
const doc = new Y.Doc();
if (initialDoc) {
Y.applyUpdate(doc, Buffer.from(initialDoc, 'base64'));
}
const provider = new WebrtcProvider(padId, doc, {
signaling: [signalingUrl()],
});