2021-10-12 14:59:04 +00:00
|
|
|
/* eslint-disable @typescript-eslint/no-non-null-assertion */
|
2021-10-08 23:05:24 +00:00
|
|
|
import * as React from 'react'
|
2021-11-22 14:00:24 +00:00
|
|
|
import { Tldraw, TldrawApp, useFileSystem } from '@tldraw/tldraw'
|
|
|
|
import { createClient } from '@liveblocks/client'
|
|
|
|
import { LiveblocksProvider, RoomProvider } from '@liveblocks/react'
|
2021-11-16 16:01:29 +00:00
|
|
|
import { useAccountHandlers } from '-hooks/useAccountHandlers'
|
|
|
|
import { styled } from '-styles'
|
2021-11-22 14:00:24 +00:00
|
|
|
import { useMultiplayerState } from '-hooks/useMultiplayerState'
|
2021-10-09 13:57:44 +00:00
|
|
|
|
2021-10-08 23:05:24 +00:00
|
|
|
const client = createClient({
|
2021-11-08 14:21:37 +00:00
|
|
|
publicApiKey: process.env.NEXT_PUBLIC_LIVEBLOCKS_PUBLIC_API_KEY || '',
|
2021-10-16 20:44:58 +00:00
|
|
|
throttle: 80,
|
2021-10-08 23:05:24 +00:00
|
|
|
})
|
|
|
|
|
2021-11-16 16:01:29 +00:00
|
|
|
export default function MultiplayerEditor({
|
|
|
|
roomId,
|
|
|
|
isUser = false,
|
|
|
|
isSponsor = false,
|
|
|
|
}: {
|
|
|
|
roomId: string
|
|
|
|
isUser: boolean
|
|
|
|
isSponsor: boolean
|
|
|
|
}) {
|
2021-10-08 23:05:24 +00:00
|
|
|
return (
|
|
|
|
<LiveblocksProvider client={client}>
|
2021-11-16 19:41:16 +00:00
|
|
|
<RoomProvider id={roomId} defaultStorageRoot={TldrawApp.defaultDocument}>
|
2021-11-16 16:01:29 +00:00
|
|
|
<Editor roomId={roomId} isSponsor={isSponsor} isUser={isUser} />
|
2021-10-08 23:05:24 +00:00
|
|
|
</RoomProvider>
|
|
|
|
</LiveblocksProvider>
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
2021-11-16 16:01:29 +00:00
|
|
|
// Inner Editor
|
|
|
|
|
2021-11-19 12:59:08 +00:00
|
|
|
function Editor({
|
|
|
|
roomId,
|
|
|
|
isUser,
|
|
|
|
isSponsor,
|
|
|
|
}: {
|
|
|
|
roomId: string
|
|
|
|
isUser: boolean
|
|
|
|
isSponsor: boolean
|
|
|
|
}) {
|
2021-11-16 16:01:29 +00:00
|
|
|
const fileSystemEvents = useFileSystem()
|
|
|
|
const { onSignIn, onSignOut } = useAccountHandlers()
|
2021-11-22 14:00:24 +00:00
|
|
|
const { error, ...events } = useMultiplayerState(roomId)
|
2021-11-16 16:01:29 +00:00
|
|
|
|
|
|
|
if (error) return <LoadingScreen>Error: {error.message}</LoadingScreen>
|
2021-10-08 23:05:24 +00:00
|
|
|
|
|
|
|
return (
|
|
|
|
<div className="tldraw">
|
2021-11-16 16:01:29 +00:00
|
|
|
<Tldraw
|
|
|
|
autofocus
|
2021-10-16 20:44:58 +00:00
|
|
|
showPages={false}
|
2021-11-16 16:01:29 +00:00
|
|
|
showSponsorLink={isSponsor}
|
|
|
|
onSignIn={isSponsor ? undefined : onSignIn}
|
2021-11-19 12:59:08 +00:00
|
|
|
onSignOut={isUser ? onSignOut : undefined}
|
2021-11-16 16:01:29 +00:00
|
|
|
{...fileSystemEvents}
|
2021-11-22 14:00:24 +00:00
|
|
|
{...events}
|
2021-10-16 20:44:58 +00:00
|
|
|
/>
|
2021-10-08 23:05:24 +00:00
|
|
|
</div>
|
|
|
|
)
|
|
|
|
}
|
2021-11-16 16:01:29 +00:00
|
|
|
|
|
|
|
const LoadingScreen = styled('div', {
|
|
|
|
position: 'absolute',
|
|
|
|
top: 0,
|
|
|
|
left: 0,
|
|
|
|
width: '100%',
|
|
|
|
height: '100%',
|
|
|
|
display: 'flex',
|
|
|
|
alignItems: 'center',
|
|
|
|
justifyContent: 'center',
|
|
|
|
})
|