Skip to content

Openverse is a search engine for openly-licensed media. This monorepo includes all application code.

4.8 KB
Raw Blame
import { useNuxtApp } from "#imports"

import { defineStore } from "pinia"

import type { SupportedMediaType } from "#shared/constants/media"
import { validateUUID } from "#shared/utils/query-utils"
import { isDetail, isMediaDetail } from "#shared/types/media"
import type {
  AudioDetail,
  DetailFromMediaType,
  ImageDetail,
} from "#shared/types/media"
import type { FetchingError, FetchState } from "#shared/types/fetch-state"
import { useMediaStore } from "~/stores/media/index"
import { useApiClient } from "~/composables/use-api-client"

export type MediaItemState = {
  mediaType: SupportedMediaType | null
  mediaId: string | null
  mediaItem: DetailFromMediaType<SupportedMediaType> | null
  fetchState: FetchState
}

export const useSingleResultStore = defineStore("single-result", {
  state: (): MediaItemState => ({
    mediaType: null,
    mediaId: null,
    mediaItem: null,
    fetchState: { status: "idle", error: null },
  }),

  getters: {
    audio(state): AudioDetail | null {
      if (isDetail.audio(state.mediaItem)) {
        return state.mediaItem
      }
      return null
    },
    image(state): ImageDetail | null {
      if (isDetail.image(state.mediaItem)) {
        return state.mediaItem
      }
      return null
    },
    isFetching: (state) => state.fetchState.status === "fetching",
  },

  actions: {
    _endFetching(error?: FetchingError) {
      if (error) {
        this.fetchState = { status: "error", error }
      } else {
        this.fetchState = { status: "success", error: null }
      }
    },
    _startFetching() {
      this.fetchState = { status: "fetching", error: null }
    },

    _updateFetchState(action: "start" | "end", option?: FetchingError) {
      if (action === "start") {
        this._startFetching()
      } else {
        this._endFetching(option)
      }
    },

    reset() {
      this.mediaItem = null
      this.mediaType = null
      this.mediaId = null
      this.fetchState = { status: "idle", error: null }
    },

    /**
     * Set the media item with the display name of its provider
     * and its type. Reset the other media type item.
     * If the media item is null, reset both media items.
     */
    setMediaItem(mediaItem: AudioDetail | ImageDetail | null) {
      if (mediaItem) {
        this.mediaItem = mediaItem
        this.mediaType = mediaItem.frontendMediaType
        this.mediaId = mediaItem.id
      } else {
        this.mediaItem = null
        this.mediaType = null
        this.mediaId = null
      }
    },

    /**
     * If the item is already in the media store, reuse it.
     * Otherwise, set the media type and id, fetch the media
     * itself later.
     */
    setMediaById(type: SupportedMediaType, id: string) {
      if (this.mediaId === id && isMediaDetail(this.mediaItem, type)) {
        return
      }
      const existingItem = useMediaStore().getItemById(type, id)
      if (existingItem) {
        this.setMediaItem(existingItem)
      } else {
        this.mediaId = id
        this.mediaType = type
        this.mediaItem = null
      }
    },

    getExistingItem<T extends SupportedMediaType>(
      type: T,
      id: string
    ): DetailFromMediaType<T> | null {
      if (this.mediaId === id && isMediaDetail<T>(this.mediaItem, type)) {
        return this.mediaItem
      }
      const existingItem = useMediaStore().getItemById(type, id)
      if (existingItem) {
        return existingItem as DetailFromMediaType<T>
      }
      return null
    },

    /**
     * Check if the `id` matches the `mediaId` and the media item
     * is already fetched. If middleware only set the `id` and
     * did not set the media, fetch the media item.
     */
    async fetch<T extends SupportedMediaType>(type: T, id: string | null) {
      if (!id || !validateUUID(id)) {
        this._endFetching({
          code: "ERR_UNKNOWN",
          requestKind: "single-result",
          searchType: type,
        } as FetchingError)
        return null
      }
      const existingItem = this.getExistingItem(type, id)
      if (existingItem) {
        return existingItem
      }

      this._updateFetchState("start")

      // When fetch is called by the middleware, the app context is not available during the error handling,
      // so we need to use the `useNuxtApp` here, outside the catch clause, to access the app context.
      const { $processFetchingError } = useNuxtApp()

      const client = useApiClient()

      try {
        const item = await client.getSingleMedia(type, id)

        this.setMediaItem(item)
        this._updateFetchState("end")

        return item as DetailFromMediaType<typeof type>
      } catch (error) {
        const errorData = $processFetchingError(error, type, "single-result", {
          id,
        })
        this._updateFetchState("end", errorData)
        return null
      }
    },
  },
})