Skip to content

AI-native design editor. Open-source Figma alternative.

4.4 KB
Raw Blame
import { useClipboard } from '@vueuse/core'
import { computed, inject, provide, proxyRefs, ref, watch } from 'vue'
import type { InjectionKey, ShallowUnwrapRef } from 'vue'

import { useI18n } from '@open-pencil/vue/i18n'

import { useJoinRoom } from '@/app/collab/join'
import { activeRoom } from '@/app/collab/rooms'
import { DEFAULT_COLLAB_STATE, useCollabInjected } from '@/app/collab/use'
import { useActiveEditorStoreRef } from '@/app/editor/active-store'
import { useNotificationMessages } from '@/app/i18n/notifications'
import { presenceOf, renameAgent } from '@/app/presence/registry'
import type { FollowTarget } from '@/app/presence/types'
import { toast } from '@/app/shell/ui'
import { presenceRows as buildPresenceRows } from '@/components/presence/rows'
import { getShareURL } from '@/constants'

function createCollabPanelContext() {
  const collab = useCollabInjected()
  const joinRoomFromInput = useJoinRoom()
  const { copy } = useClipboard()
  const { common, collaboration } = useI18n()
  const notifications = useNotificationMessages()

  const state = computed(() => collab?.state.value ?? DEFAULT_COLLAB_STATE)
  const joinInput = ref('')
  const joinError = ref(false)
  const nameDraft = ref('')
  const popoverOpen = ref(false)
  // Each time the panel opens it shows the current name, or stays empty while the person has
  // none of their own, so the field shows the generated one as its placeholder.
  watch(
    popoverOpen,
    (open) => {
      if (open) nameDraft.value = state.value.hasChosenName ? state.value.localName : ''
    },
    { immediate: true }
  )
  const peers = computed(() => collab?.remotePeers.value ?? [])
  const following = computed(() => collab?.following.value ?? null)
  const storeRef = useActiveEditorStoreRef()
  const presenceRows = computed(() => {
    const store = storeRef.value
    const voice = activeRoom.value?.voice
    const speaking = voice?.speaking.value ?? new Set<number>()
    return buildPresenceRows(
      {
        name: state.value.localName,
        color: state.value.localColor,
        agents: store ? presenceOf(store).agents.value : [],
        voice: voice?.joined.value
          ? {
              muted: voice.muted.value,
              speaking: !voice.muted.value && speaking.has(voice.localClientId)
            }
          : undefined
      },
      peers.value,
      (pageId) => store?.graph.getNode(pageId)?.name,
      speaking
    )
  })
  /** Everyone in the room's voice call, ourselves first. */
  const callRows = computed(() => presenceRows.value.filter((row) => row.voice))
  const shareURL = computed(() => (state.value.roomId ? getShareURL(state.value.roomId) : ''))

  function saveName() {
    const name = nameDraft.value.trim()
    if (name && name !== state.value.localName) collab?.setLocalName(name)
  }

  function share() {
    if (!collab) return
    saveName()
    const roomId = collab.shareCurrentDoc()
    if (!roomId) return
    void copy(getShareURL(roomId))
    toast.info(notifications.value.linkCopied)
    popoverOpen.value = false
  }

  function join() {
    saveName()
    if (!joinRoomFromInput(joinInput.value)) {
      joinError.value = true
      return
    }
    joinInput.value = ''
    joinError.value = false
    popoverOpen.value = false
  }

  function clearJoinError() {
    joinError.value = false
  }

  function disconnect() {
    collab?.disconnect()
    popoverOpen.value = false
  }

  function follow(target: FollowTarget | null) {
    collab?.follow(target)
  }

  function renameLocalAgent(agentId: string, name: string) {
    if (storeRef.value) renameAgent(storeRef.value, agentId, name)
  }

  return {
    common,
    messages: collaboration,
    joinInput,
    joinError,
    nameDraft,
    popoverOpen,
    state,
    following,
    presenceRows,
    callRows,
    shareURL,
    saveName,
    share,
    join,
    clearJoinError,
    disconnect,
    follow,
    renameLocalAgent
  }
}

export type CollabPanelContext = ShallowUnwrapRef<ReturnType<typeof createCollabPanelContext>>

const COLLAB_PANEL_KEY: InjectionKey<CollabPanelContext> = Symbol('CollabPanelContext')

export function provideCollabPanel() {
  const ctx = proxyRefs(createCollabPanelContext())
  provide(COLLAB_PANEL_KEY, ctx)
  return ctx
}

export function useCollabPanelContext(): CollabPanelContext {
  const ctx = inject(COLLAB_PANEL_KEY)
  if (!ctx) throw new Error('Collab panel controls must be used within CollabPanel')
  return ctx
}