All files / src/composables useSpacePage.ts

98.03% Statements 50/51
96.77% Branches 30/31
100% Functions 9/9
100% Lines 43/43

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106                                                  20x 20x 20x   20x 20x 20x   20x 11x           20x 38x 18x 12x     42x     32x 32x 32x 32x 32x 12x 12x 12x         3x   7x 2x 5x 4x   1x 1x     12x         20x 2x 2x 2x 2x         1x       25x 25x 5x 5x     1x       32x 25x   20x    
// @implements UC-SPACE-010.1, UC-SPACE-019.1
/**
 * Loads a page of a flow's public space, and the space's menu, for the public
 * pages. A visitor reads the public endpoints (pages visible to everyone); a
 * member of the flow reads the member endpoints, through the flow's id, so
 * members-only pages and menu entries show too. See FLOW_SPACE.md "Pages and content blocks".
 * The visitor's reads go through the spaces CDN (services/spacesApi.ts).
 */
import { computed, ref, watch, type Ref } from 'vue'
import api from '@/services/api'
import spacesApi from '@/services/spacesApi'
import type { PublicSpace } from '@/components/SpaceLayout.vue'
import {
  HOME_SLUG,
  type ListItemsPage,
  type LoadListItems,
  type SpacePageContent,
  type SpacePageMenuEntry
} from '@/utils/spacePages'
 
export function useSpacePage(
  space: Ref<PublicSpace | null>,
  memberFlowId: Ref<string | null>,
  pageSlug: Ref<string | null>
) {
  const page = ref<SpacePageContent | null>(null)
  const pageLoading = ref(false)
  const pageNotFound = ref(false)
  /** A published members-only page, for a visitor who is not a member (UC-SPACE-010.1) */
  const pageMembersOnly = ref(false)
  const pageError = ref(false)
  const memberMenu = ref<SpacePageMenuEntry[] | null>(null)
 
  const memberRequest = (flowId: string, path: string, params?: Record<string, unknown>) =>
    api.get(`/v1/flows/${encodeURIComponent(flowId)}${path}`, {
      headers: { 'X-Flow-Id': flowId },
      params
    })
 
  /** The home page is read when one may exist: always for a member (it may be members-only) */
  const slugToLoad = computed(() => {
    if (!space.value) return null
    if (pageSlug.value) return pageSlug.value
    return memberFlowId.value || space.value.hasHome ? HOME_SLUG : null
  })
 
  const menu = computed<SpacePageMenuEntry[]>(() => memberMenu.value ?? space.value?.menu ?? [])
 
  async function loadPage(slug: string | null) {
    page.value = null
    pageNotFound.value = false
    pageMembersOnly.value = false
    pageError.value = false
    if (!slug || !space.value) return
    pageLoading.value = true
    try {
      const response = memberFlowId.value
        ? await memberRequest(memberFlowId.value, `/pages/${encodeURIComponent(slug)}`)
        : await spacesApi.get(
            `/v1/spaces/${encodeURIComponent(space.value.slug)}/pages/${encodeURIComponent(slug)}`
          )
      page.value = response.data
    } catch (err: any) {
      if (err.response?.data?.error === 'MEMBERS_ONLY') {
        pageMembersOnly.value = true
      } else if (err.response?.status === 404 || err.response?.status === 403) {
        pageNotFound.value = true
      } else {
        console.error('[useSpacePage] failed to load page:', err)
        pageError.value = true
      }
    } finally {
      pageLoading.value = false
    }
  }
 
  /** "Load more" of an item list of the page shown, read like the page */
  const loadListItems: LoadListItems = async (blockId, offset) => {
    const slug = page.value?.slug
    Iif (!slug || !space.value) return { items: [], hasMore: false }
    const path = `/pages/${encodeURIComponent(slug)}/blocks/${encodeURIComponent(blockId)}`
    const response = memberFlowId.value
      ? await memberRequest(memberFlowId.value, path, { offset })
      : await spacesApi.get(`/v1/spaces/${encodeURIComponent(space.value.slug)}${path}`, {
          params: { offset }
        })
    return response.data as ListItemsPage
  }
 
  async function loadMemberMenu(flowId: string | null) {
    memberMenu.value = null
    if (!flowId) return
    try {
      memberMenu.value = (await memberRequest(flowId, '/pages')).data.menu
    } catch (err) {
      // The public menu stays
      console.error('[useSpacePage] failed to load the members menu:', err)
    }
  }
 
  watch(slugToLoad, (slug) => loadPage(slug), { immediate: true })
  watch(memberFlowId, (flowId) => loadMemberMenu(flowId), { immediate: true })
 
  return { page, pageLoading, pageNotFound, pageMembersOnly, pageError, menu, loadListItems }
}