⚡️(app-impress) create pad store
Better to store the WebrtcProvider, we will reuse the provider if one is already created.
This commit is contained in:
@@ -1 +1,2 @@
|
|||||||
NEXT_PUBLIC_API_URL=http://localhost:8071/api/v1.0/
|
NEXT_PUBLIC_API_URL=http://localhost:8071/api/v1.0/
|
||||||
|
NEXT_PUBLIC_SIGNALING_URL=ws://localhost:4444
|
||||||
|
|||||||
@@ -1 +1,2 @@
|
|||||||
NEXT_PUBLIC_API_URL=https://desk-staging.beta.numerique.gouv.fr/api/v1.0/
|
NEXT_PUBLIC_API_URL=https://desk-staging.beta.numerique.gouv.fr/api/v1.0/
|
||||||
|
NEXT_PUBLIC_SIGNALING_URL=ws://localhost:4444
|
||||||
@@ -20,5 +20,6 @@ declare module '*.svg?url' {
|
|||||||
namespace NodeJS {
|
namespace NodeJS {
|
||||||
interface ProcessEnv {
|
interface ProcessEnv {
|
||||||
NEXT_PUBLIC_API_URL?: string;
|
NEXT_PUBLIC_API_URL?: string;
|
||||||
|
NEXT_PUBLIC_SIGNALING_URL?: string;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,21 +1,37 @@
|
|||||||
import { BlockNoteView, useCreateBlockNote } from '@blocknote/react';
|
import { BlockNoteView, useCreateBlockNote } from '@blocknote/react';
|
||||||
import '@blocknote/react/style.css';
|
import '@blocknote/react/style.css';
|
||||||
import React from 'react';
|
import React, { useCallback } from 'react';
|
||||||
import { WebrtcProvider } from 'y-webrtc';
|
|
||||||
import * as Y from 'yjs';
|
|
||||||
|
|
||||||
const doc = new Y.Doc();
|
import { useAuthStore } from '@/core/auth';
|
||||||
const provider = new WebrtcProvider('my-document-id4', doc, {
|
|
||||||
signaling: ['ws://localhost:4444'],
|
import { PadStore, usePadStore } from '../store';
|
||||||
});
|
import { Pad } from '../types';
|
||||||
|
|
||||||
|
interface BlockNoteEditorProps {
|
||||||
|
pad: Pad;
|
||||||
|
}
|
||||||
|
|
||||||
|
export const BlockNoteEditor = ({ pad }: BlockNoteEditorProps) => {
|
||||||
|
const { userData } = useAuthStore();
|
||||||
|
const getProvider = useCallback(
|
||||||
|
(state: PadStore) => {
|
||||||
|
if (!state.providers[pad.id]) {
|
||||||
|
return state.createProvider(pad.id);
|
||||||
|
}
|
||||||
|
|
||||||
|
return state.providers[pad.id];
|
||||||
|
},
|
||||||
|
[pad.id],
|
||||||
|
);
|
||||||
|
|
||||||
|
const provider = usePadStore(getProvider);
|
||||||
|
|
||||||
export const BlockNoteEditor = () => {
|
|
||||||
const editor = useCreateBlockNote({
|
const editor = useCreateBlockNote({
|
||||||
collaboration: {
|
collaboration: {
|
||||||
provider,
|
provider,
|
||||||
fragment: doc.getXmlFragment('document-store'),
|
fragment: provider.doc.getXmlFragment('document-store'),
|
||||||
user: {
|
user: {
|
||||||
name: 'My Username',
|
name: userData?.name || userData?.email || 'Anonymous',
|
||||||
color: '#ff0000',
|
color: '#ff0000',
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
|
|||||||
@@ -14,7 +14,7 @@ export const PadEditor = ({ pad }: PadEditorProps) => {
|
|||||||
return (
|
return (
|
||||||
<Card className="m-b p-b" $height="100%">
|
<Card className="m-b p-b" $height="100%">
|
||||||
<Text as="h2">{pad.name}</Text>
|
<Text as="h2">{pad.name}</Text>
|
||||||
<BlockNoteEditor />
|
<BlockNoteEditor pad={pad} />
|
||||||
</Card>
|
</Card>
|
||||||
);
|
);
|
||||||
};
|
};
|
||||||
|
|||||||
@@ -0,0 +1 @@
|
|||||||
|
export * from './usePadStore';
|
||||||
@@ -0,0 +1,34 @@
|
|||||||
|
import { WebrtcProvider } from 'y-webrtc';
|
||||||
|
import * as Y from 'yjs';
|
||||||
|
import { create } from 'zustand';
|
||||||
|
|
||||||
|
import { Pad } from '../types';
|
||||||
|
|
||||||
|
export interface PadStore {
|
||||||
|
providers: { [padId: Pad['id']]: WebrtcProvider };
|
||||||
|
createProvider: (padId: Pad['id']) => WebrtcProvider;
|
||||||
|
}
|
||||||
|
|
||||||
|
const initialState = {
|
||||||
|
providers: {},
|
||||||
|
};
|
||||||
|
|
||||||
|
export const usePadStore = create<PadStore>((set) => ({
|
||||||
|
providers: initialState.providers,
|
||||||
|
createProvider: (padId: string) => {
|
||||||
|
const provider = new WebrtcProvider(padId, new Y.Doc(), {
|
||||||
|
signaling: [process.env.NEXT_PUBLIC_SIGNALING_URL || ''],
|
||||||
|
});
|
||||||
|
|
||||||
|
set(({ providers }) => {
|
||||||
|
return {
|
||||||
|
providers: {
|
||||||
|
...providers,
|
||||||
|
[padId]: provider,
|
||||||
|
},
|
||||||
|
};
|
||||||
|
});
|
||||||
|
|
||||||
|
return provider;
|
||||||
|
},
|
||||||
|
}));
|
||||||
Reference in New Issue
Block a user