import {TemplateResult, html} from "@benev/slate" import {styles} from "./styles.js" import loadingSvg from "../../icons/loading.svg.js" import exitSvg from "../../icons/gravity-ui/exit.svg.js" import crownSvg from "../../icons/remix-icon/crown.svg.js" import xMarkSvg from "../../icons/gravity-ui/x-mark.svg.js" import personSvg from "../../icons/gravity-ui/person.svg.js" import slidersSvg from "../../icons/gravity-ui/sliders.svg.js" import warningSvg from "../../icons/gravity-ui/warning.svg.js" import {collaboration, shadow_view} from "../../context/context.js" import collaborateSvg from "../../icons/gravity-ui/collaborate.svg.js" export const CollaborationManager = shadow_view((use) => () => { use.styles(styles) const [joiningOrCreatingInProgress, setJoiningOrCreatingInProgress] = use.state(false) const [sessionError, setSessionError] = use.state("") const [inviteID, setInviteID] = use.state("") const [isModalOpened, setIsModalOpened] = use.state(false) const [hostInviteID, setHostInviteID] = use.state("") const [isHost, setIsHost] = use.state(!!collaboration.host) const [isClient, setIsClient, getIsClient] = use.state(!!collaboration.client) const [numberOfCollaborators, setNumberOfCollaborators] = use.state(collaboration.numberOfConnectedUsers) const [allow, setAllow] = use.state(true) setIsClient(!!collaboration.client) use.mount(() => { const dispose2 = collaboration.onChange(() => use.rerender()) const locker = collaboration.onLock((v) => setAllow(v)) const dispose = collaboration.onNumberOfClientsChange(number => setNumberOfCollaborators(number)) const dispose1 = collaboration.onDisconnect(() => { setIsHost(false) setHostInviteID("") setNumberOfCollaborators(0) if(getIsClient()) { setIsClient(false) window.location.hash = "#/editor" } }) return () => { dispose(); dispose1(); dispose2(); locker(); if(isClient || isHost) { collaboration.disconnect() } } }) const createRoom = async () => { setJoiningOrCreatingInProgress(true) try { const host = await collaboration.createRoom() setIsHost(true) setHostInviteID(host.invite) setJoiningOrCreatingInProgress(false) } catch(e) { setSessionError(e) setJoiningOrCreatingInProgress(false) } } const joinRoom = async () => { setJoiningOrCreatingInProgress(true) try { await collaboration.joinRoom(inviteID) setJoiningOrCreatingInProgress(false) setIsClient(true) } catch(e) { collaboration.initiatingProject = false setSessionError(e) setJoiningOrCreatingInProgress(false) } } const noSession = !isHost && !isClient const renderHostModal = () => { return renderModal(html`

You are ${crownSvg} host of a collaborative session

Invite code: ${hostInviteID} Lock session Close session

Collaborators:

${numberOfCollaborators > 0 ? [...collaboration.connectedClients.entries()].map( ([id, client]) => html`
${personSvg} ${client.id}
` ) : html`No collaborators connected`}
`) } const renderNoSessionModal = () => { return renderModal( html`

Create Session

Join Session

setInviteID((e.target as HTMLInputElement).value)}>
` ) } const renderModal = (content: TemplateResult): TemplateResult => { return html` ` } return html` ${noSession ? html` ${renderNoSessionModal()} ` : html`
${isHost ? html` setIsModalOpened(true)} class=settings>${slidersSvg}` : html` collaboration.disconnect()} class=exit>${exitSvg}` } ${isHost ? html`${crownSvg} Host` : html`${personSvg} Client`}
${numberOfCollaborators > 0 ? html`` : html``} ${collaborateSvg}${numberOfCollaborators}
${isHost ? renderHostModal() : null}
`} ` })