<commit_before>import R from 'ramda'

import {
  makeDebugger,
  $solver,
  asyncRes,
  asyncErr,
  EVENT,
  ERR,
  THREAD,
  TOPIC,
  errRescue,
} from 'utils'

import SR71 from 'utils/async/sr71'
import S from './schema'

const sr71$ = new SR71({
  resv_event: [EVENT.COMMUNITY_CHANGE, EVENT.TABBER_CHANGE],
})
let sub$ = null
let store = null

/* eslint-disable-next-line */
const debug = makeDebugger('L:TagsBar')

export const onTagSelect = tag => store.selectTag(tag)

const NO_TAG_THREADS = [THREAD.USER, THREAD.CHEATSHEET, THREAD.WIKI]

export const loadTags = (topic = TOPIC.POST) => {
  const { curThread } = store
  if (R.contains(curThread, NO_TAG_THREADS)) return false

  const community = store.curCommunity.raw
  const thread = R.toUpper(curThread)

  const args = { community, thread, topic }

  /* debug('#### loadTags --> ', args) */
  sr71$.query(S.partialTags, args)
}

// ###############################
// Data & Error handlers
// ###############################

const DataSolver = [
  {
    match: asyncRes('partialTags'),
    action: ({ partialTags: tags }) => store.markState({ tags }),
  },
  {
    match: asyncRes(EVENT.COMMUNITY_CHANGE),
    action: () => loadTags(),
  },
  {
    match: asyncRes(EVENT.TABBER_CHANGE),
    action: data => {
      const { topic } = data[EVENT.TABBER_CHANGE].data
      loadTags(topic)
    },
  },
]
const ErrSolver = [
  {
    match: asyncErr(ERR.GRAPHQL),
    action: () => {},
  },
  {
    match: asyncErr(ERR.TIMEOUT),
    action: ({ details }) => {
      errRescue({ type: ERR.TIMEOUT, details, path: 'AccountEditor' })
    },
  },
  {
    match: asyncErr(ERR.NETWORK),
    action: () => errRescue({ type: ERR.NETWORK, path: 'AccountEditor' }),
  },
]

export const init = (_store, thread, topic, active) => {
  store = _store

  if (sub$) return false
  sub$ = sr71$.data().subscribe($solver(DataSolver, ErrSolver))

  let activeTag = R.pick(['id', 'title', 'color'], active)
  if (R.isEmpty(activeTag.title)) activeTag = null
  store.markState({ thread, topic, activeTag })
}<commit_msg>fix(tags bar): fix empty when page cache err<commit_after>import R from 'ramda'

import {
  makeDebugger,
  $solver,
  asyncRes,
  asyncErr,
  EVENT,
  ERR,
  THREAD,
  TOPIC,
  errRescue,
} from 'utils'

import SR71 from 'utils/async/sr71'
import S from './schema'

const sr71$ = new SR71({
  resv_event: [EVENT.COMMUNITY_CHANGE, EVENT.TABBER_CHANGE],
})
let sub$ = null
let store = null

/* eslint-disable-next-line */
const debug = makeDebugger('L:TagsBar')

export const onTagSelect = tag => store.selectTag(tag)

const NO_TAG_THREADS = [THREAD.USER, THREAD.CHEATSHEET, THREAD.WIKI]

export const loadTags = (topic = TOPIC.POST) => {
  const { curThread } = store
  if (R.contains(curThread, NO_TAG_THREADS)) return false

  const community = store.curCommunity.raw
  const thread = R.toUpper(curThread)

  const args = { community, thread, topic }

  /* debug('#### loadTags --> ', args) */
  sr71$.query(S.partialTags, args)
}

// ###############################
// Data & Error handlers
// ###############################

const DataSolver = [
  {
    match: asyncRes('partialTags'),
    action: ({ partialTags: tags }) => store.markState({ tags }),
  },
  {
    match: asyncRes(EVENT.COMMUNITY_CHANGE),
    action: () => loadTags(),
  },
  {
    match: asyncRes(EVENT.TABBER_CHANGE),
    action: data => {
      const { topic } = data[EVENT.TABBER_CHANGE].data
      loadTags(topic)
    },
  },
]
const ErrSolver = [
  {
    match: asyncErr(ERR.GRAPHQL),
    action: () => {},
  },
  {
    match: asyncErr(ERR.TIMEOUT),
    action: ({ details }) => {
      errRescue({ type: ERR.TIMEOUT, details, path: 'AccountEditor' })
    },
  },
  {
    match: asyncErr(ERR.NETWORK),
    action: () => errRescue({ type: ERR.NETWORK, path: 'AccountEditor' }),
  },
]

export const init = (_store, thread, topic, active) => {
  store = _store

  if (sub$) return false
  sub$ = sr71$.data().subscribe($solver(DataSolver, ErrSolver))
  debug('init tags: ', store.tagsData)
  if (R.isEmpty(store.tagsData)) loadTags()

  let activeTag = R.pick(['id', 'title', 'color'], active)
  if (R.isEmpty(activeTag.title)) activeTag = null
  store.markState({ thread, topic, activeTag })
}
{'fraction_non_alphanumeric': 0.18599148130619972, 'fraction_numerical': 0.005679129200189304, 'mean_word_length': 3.5945652173913043, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 23, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5419792', 'n_tokens_mistral': 1496, 'n_tokens_neox': 1464, 'n_words': 327}