<commit_before>import React, { Component, Fragment } from 'react'
import { connect } from 'react-redux'
import { bindActionCreators } from 'redux'
import Error from 'next/error'
import { addItemsToSecondaryQueueStorage, clearItemsFromSecondaryQueueStorage } from 'podverse-ui'
import MediaHeaderCtrl from '~/components/MediaHeaderCtrl/MediaHeaderCtrl'
import MediaInfoCtrl from '~/components/MediaInfoCtrl/MediaInfoCtrl'
import MediaListCtrl from '~/components/MediaListCtrl/MediaListCtrl'
import Meta from '~/components/Meta/Meta'
import { convertToNowPlayingItem } from '~/lib/nowPlayingItem'
import { clone, getUrlFromRequestOrWindow, removeDoubleQuotes } from '~/lib/utility'
import { pageIsLoading, pagesSetQueryState, playerQueueLoadSecondaryItems
  } from '~/redux/actions'
import { getEpisodesByQuery, getMediaRefsByQuery, getPodcastById } from '~/services/'

type Props = {
  is404Page?: boolean
  lastScrollPosition?: number
  meta?: any
  pageKey?: string
  pagesSetQueryState?: any
  playerQueue?: any
  podcast?: any
  queryFrom?: any
  queryPage: number
  querySort?: any
  queryType?: any
  user?: any
  userSetInfo?: any
}

type State = {}

const kPageKey = 'podcast_'

class Podcast extends Component<Props, State> {

  static async getInitialProps({ query, req, store }) {
    const pageKeyWithId = `${kPageKey}${query.id}`
    const state = store.getState()
    const { pages, settings, user } = state
    const { nsfwMode } = settings

    try {
      const podcastResult = await getPodcastById(query.id, nsfwMode)
      const podcast = podcastResult.data

      if (!podcast) {
        store.dispatch(pageIsLoading(false))
        return { is404Page: true }
      }

      const currentPage = pages[pageKeyWithId] || {}
      const lastScrollPosition = currentPage.lastScrollPosition
      const queryFrom = currentPage.queryFrom || query.from || 'from-podcast'
      const queryPage = currentPage.queryPage || query.page || 1
      const querySort = currentPage.querySort || query.sort || 'most-recent'
      const queryType = currentPage.queryType || query.type || 'episodes'
      let podcastId = ''

      if (queryFrom === 'from-podcast') {
        podcastId = podcast.id
      } else if (queryFrom === 'subscribed-only') {
        podcastId = user.subscribedPodcastIds
      }

      if (Object.keys(currentPage).length === 0) {
        let results

        if (queryType === 'episodes') {
          results = await getEpisodesByQuery({
            from: queryFrom,
            page: queryPage,
            ...(podcastId ? { podcastId } : {}),
            sort: querySort,
            type: queryType
          }, nsfwMode)
        } else {
          results = await getMediaRefsByQuery({
            from: queryFrom,
            page: queryPage,
            ...(podcastId ? { podcastId } : {}),
            sort: querySort,
            type: queryType
          }, nsfwMode)
        }

        let listItems = results.data[0].map(x => convertToNowPlayingItem(x))
        
        store.dispatch(playerQueueLoadSecondaryItems(clone(listItems)))

        store.dispatch(pagesSetQueryState({
          pageKey: pageKeyWithId,
          listItems,
          listItemsTotal: results.data[1],
          podcast,
          queryFrom,
          queryPage,
          querySort,
          queryType<commit_msg>Update podcast.tsx<commit_after>import React, { Component, Fragment } from 'react'
import { connect } from 'react-redux'
import { bindActionCreators } from 'redux'
import Error from 'next/error'
import { addItemsToSecondaryQueueStorage, clearItemsFromSecondaryQueueStorage } from 'podverse-ui'
import MediaHeaderCtrl from '~/components/MediaHeaderCtrl/MediaHeaderCtrl'
import MediaInfoCtrl from '~/components/MediaInfoCtrl/MediaInfoCtrl'
import MediaListCtrl from '~/components/MediaListCtrl/MediaListCtrl'
import Meta from '~/components/Meta/Meta'
import { convertToNowPlayingItem } from '~/lib/nowPlayingItem'
import { clone, getUrlFromRequestOrWindow, removeDoubleQuotes } from '~/lib/utility'
import { pageIsLoading, pagesSetQueryState, playerQueueLoadSecondaryItems
  } from '~/redux/actions'
import { getEpisodesByQuery, getMediaRefsByQuery, getPodcastById } from '~/services/'

type Props = {
  is404Page?: boolean
  lastScrollPosition?: number
  meta?: any
  pageKey?: string
  pagesSetQueryState?: any
  playerQueue?: any
  podcast?: any
  queryFrom?: any
  queryPage: number
  querySort?: any
  queryType?: any
  user?: any
  userSetInfo?: any
}

type State = {}

const kPageKey = 'podcast_'

class Podcast extends Component<Props, State> {

  static async getInitialProps({ query, req, store }) {
    const pageKeyWithId = `${kPageKey}${query.id}`
    const state = store.getState()
    const { pages, settings, user } = state
    const { nsfwMode } = settings

    try {
      const podcastResult = await getPodcastById(query.id, nsfwMode)
      const podcast = podcastResult.data

      if (!podcast) {
        store.dispatch(pageIsLoading(false))
        return { is404Page: true }
      }

      const currentPage = pages[pageKeyWithId] || {}
      const lastScrollPosition = currentPage.lastScrollPosition
      const queryFrom = currentPage.queryFrom || query.from || 'from-podcast'
      const queryPage = currentPage.queryPage || query.page || 1
      const querySort = currentPage.querySort || query.sort || 'most-recent'
      const queryType = currentPage.queryType || query.type || 'episodes'
      let podcastId = ''

      if (queryFrom === 'from-podcast') {
        podcastId = podcast.id
      } else if (queryFrom === 'subscribed-only') {
        podcastId = user.subscribedPodcastIds
      }

      if (Object.keys(currentPage).length === 0) {
        let results

        if (queryType === 'episodes') {
          results = await getEpisodesByQuery({
            from: queryFrom,
            page: queryPage,
            ...(podcastId ? { podcastId } : {}),
            sort: querySort,
            type: queryType
          }, nsfwMode)
        } else {
          results = await getMediaRefsByQuery({
            from: queryFrom,
            page: queryPage,
            ...(podcastId ? { podcastId } : {}),
            sort: querySort,
            type: queryType
          }, nsfwMode)
        }

        let listItems = results.data[0].map(x => {
          let item = convertToNowPlayingItem(x)
          item.podcastId = podcast.id
          item.podcastImageUrl = podcast.imageUrl
          item.podcastTitle = podcast.title
          return item
        })
        
        store.dispatch(playerQueueLoadSecondaryItems(clone(listItems)))

        store.dispatch(pagesSetQueryState({
          pageKey: pageKeyWithId,
          listItems,
          listItemsTotal: results.data[1],
          podcast,
          queryFrom,
          queryPage,
          querySort,
          queryType
{'fraction_non_alphanumeric': 0.10411799065420561, 'fraction_numerical': 0.0029205607476635513, 'mean_word_length': 2.798668885191348, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 7, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 4, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4356127', 'n_tokens_mistral': 1983, 'n_tokens_neox': 1930, 'n_words': 462}