2021-10-09 13:57:44 +00:00
|
|
|
/* eslint-disable @typescript-eslint/no-non-null-assertion */
|
|
|
|
import * as React from 'react'
|
2021-11-22 14:00:24 +00:00
|
|
|
import { Tldraw } from '@tldraw/tldraw'
|
|
|
|
import { createClient } from '@liveblocks/client'
|
|
|
|
import { LiveblocksProvider, RoomProvider } from '@liveblocks/react'
|
|
|
|
import { useMultiplayerState } from './useMultiplayerState'
|
2021-10-09 13:57:44 +00:00
|
|
|
|
|
|
|
const client = createClient({
|
2021-11-08 14:21:37 +00:00
|
|
|
publicApiKey: process.env.LIVEBLOCKS_PUBLIC_API_KEY || '',
|
2021-11-22 14:00:24 +00:00
|
|
|
throttle: 100,
|
2021-10-09 13:57:44 +00:00
|
|
|
})
|
|
|
|
|
2021-11-22 14:00:24 +00:00
|
|
|
const roomId = 'mp-test-8'
|
2021-10-09 13:57:44 +00:00
|
|
|
|
|
|
|
export function Multiplayer() {
|
|
|
|
return (
|
|
|
|
<LiveblocksProvider client={client}>
|
2021-11-08 14:21:37 +00:00
|
|
|
<RoomProvider id={roomId}>
|
2021-11-16 19:41:16 +00:00
|
|
|
<Editor roomId={roomId} />
|
2021-10-09 13:57:44 +00:00
|
|
|
</RoomProvider>
|
|
|
|
</LiveblocksProvider>
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
2021-11-16 19:41:16 +00:00
|
|
|
function Editor({ roomId }: { roomId: string }) {
|
2021-11-22 14:00:24 +00:00
|
|
|
const { error, ...events } = useMultiplayerState(roomId)
|
2021-10-09 13:57:44 +00:00
|
|
|
if (error) return <div>Error: {error.message}</div>
|
|
|
|
|
|
|
|
return (
|
|
|
|
<div className="tldraw">
|
2021-12-25 17:06:33 +00:00
|
|
|
<Tldraw showPages={false} {...events} disableAssets={true} />
|
2021-10-09 13:57:44 +00:00
|
|
|
</div>
|
|
|
|
)
|
|
|
|
}
|