All files / src/composables usePublicSpace.ts

100% Statements 38/38
100% Branches 12/12
100% Functions 8/8
100% Lines 33/33

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                          43x 43x   43x 43x 43x 43x   43x     43x 71x 28x   28x   48x   43x   43x     43x 43x 43x 43x 43x 43x 43x 40x 40x 40x   3x 2x   1x 1x     43x       43x   43x 1x     43x    
/**
 * Loads a flow's public space (GET /v1/spaces/{slug}) for its public pages,
 * names the browser tab after the flow while one is open, and tells whether
 * the signed-in user is a member. See docs/architecture/FLOW_SPACE.md.
 */
import { computed, onBeforeUnmount, ref, watch, type Ref } from 'vue'
import spacesApi from '@/services/spacesApi'
import { useAuthStore } from '@/stores/auth'
import { useFlowsStore } from '@/stores/flows'
import type { PublicSpace } from '@/components/SpaceLayout.vue'
import { rememberSpaceTheme, rememberedSpaceTheme, type SpaceTheme } from '@/utils/spaceTheme'
 
export function usePublicSpace(slug: Ref<string>) {
  const authStore = useAuthStore()
  const flowsStore = useFlowsStore()
 
  const space = ref<PublicSpace | null>(null)
  const loading = ref(true)
  const notFound = ref(false)
  const loadError = ref(false)
  /** The space's theme as last shown in this browser, drawn while it loads */
  const loadingTheme = ref<SpaceTheme | undefined>(undefined)
 
  // A member of the flow gets a way into the app; a visitor sees the page only
  const memberFlow = computed(() => {
    if (!authStore.isAuthenticated || !space.value) return null
    const spaceSlug = space.value.slug
    // myFlows only holds active memberships (GET /v1/auth/my-flows)
    return flowsStore.myFlows.find((flow) => flow.flowSlug === spaceSlug) ?? null
  })
  const memberFlowSlug = computed(() => memberFlow.value?.flowSlug ?? null)
  /** The flow's id for a member: its members-only pages are read through it (UC-SPACE-010.1) */
  const memberFlowId = computed(() => memberFlow.value?.flowId ?? null)
 
  const originalTitle = document.title
 
  async function load(value: string) {
    loading.value = true
    notFound.value = false
    loadError.value = false
    space.value = null
    loadingTheme.value = rememberedSpaceTheme(value)
    try {
      const response = await spacesApi.get(`/v1/spaces/${encodeURIComponent(value)}`)
      space.value = response.data
      document.title = response.data.name
      rememberSpaceTheme(value, response.data.theme)
    } catch (err: any) {
      if (err.response?.status === 404) {
        notFound.value = true
      } else {
        console.error('[usePublicSpace] failed to load space:', err)
        loadError.value = true
      }
    } finally {
      loading.value = false
    }
  }
 
  watch(slug, (value) => load(value), { immediate: true })
 
  onBeforeUnmount(() => {
    document.title = originalTitle
  })
 
  return { space, loading, loadingTheme, notFound, loadError, memberFlowSlug, memberFlowId }
}