("")
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`
${collaborateSvg} Collaboration
${joiningOrCreatingInProgress
? html`
Session loading ${loadingSvg}`
: sessionError === ""
? content
: html`
${warningSvg} error joining or creating session
reason: ${sessionError}
`
}
`
}
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}
`}
`
})