<commit_before>import { useQuery } from '@apollo/client'
import React, { useCallback, useContext, useEffect } from 'react'
import { useNavigate } from 'react-router'
import { AdvancedSearchContext } from 'v2/components/AdvancedSearch/AdvancedSearchContext'
import Text from 'v2/components/UI/Text'
import useRecentSearches from 'v2/hooks/useRecentSearches'
import {
  AdvancedQuickSearchTotal,
  AdvancedQuickSearchTotalVariables,
} from '__generated__/AdvancedQuickSearchTotal'
import { ICON_OFFSET } from '../AdvancedSearchInput'
import AdvancedSearchResult from '../AdvancedSearchResult'
import advancedSearchResultsTotalQuery from './queries/advancedSearchResultsTotalQuery'

interface AdvancedQuickSearchResultProps {
  index: number
  maxResults: number
  pathname: string
  selected: boolean
}

export const AdvancedSearchResultsTotal: React.FC<AdvancedQuickSearchResultProps> = ({
  index,
  maxResults,
  pathname,
  selected,
}) => {
  const { generateUrl, state } = useContext(AdvancedSearchContext)
  const navigate = useNavigate()
  const { addRecentSearch } = useRecentSearches()
  const { data, loading, refetch } = useQuery<
    AdvancedQuickSearchTotal,
    AdvancedQuickSearchTotalVariables
  >(advancedSearchResultsTotalQuery, {
    variables: state.variables,
  })

  useEffect(() => {
    refetch(state.variables)
  }, [state.variables])

  const term = state.variables.term?.facet

  const handleClick = useCallback(() => {
    addRecentSearch(state.variables)
    return navigate(generateUrl(false, pathname))
  }, [state, addRecentSearch])

  if (loading) {
    const loadingLabel = term
      ? `See all results for '${term}'`
      : 'See all results'
    return (
      <AdvancedSearchResult
        key={`see_all_results_${index === maxResults - 1}`}
        to={generateUrl(false, pathname)}
        pl={ICON_OFFSET}
        selected={selected}
      >
        <Text fontWeight="bold">{loadingLabel}</Text>
      </AdvancedSearchResult>
    )
  }

  const total = data?.searches?.advanced.total

  if (total === 0) {
    return (
      <AdvancedSearchResult
        key={`no_results_${index === maxResults - 1}`}
        pl={ICON_OFFSET}
        bg="background"
      >
        <Text fontWeight="bold">No results</Text>
      </AdvancedSearchResult>
    )
  }

  const searchLabel =
    term && total > 0
      ? `See all ${total} results for '${term}'`
      : 'See all results'

  return (
    <AdvancedSearchResult
      key={`see_all_results_${index === maxResults - 1}`}
      pl={ICON_OFFSET}
      selected={selected}
      onClick={handleClick}
    >
      <Text fontWeight="bold">{searchLabel}</Text>
    </AdvancedSearchResult>
  )
}

export default AdvancedSearchResultsTotal
<commit_msg>Fix no results state (closes ARE-456)<commit_after>import { useQuery } from '@apollo/client'
import React, { useCallback, useContext, useEffect } from 'react'
import styled from 'styled-components'

import { useNavigate } from 'react-router'
import { AdvancedSearchContext } from 'v2/components/AdvancedSearch/AdvancedSearchContext'
import Text from 'v2/components/UI/Text'
import useRecentSearches from 'v2/hooks/useRecentSearches'
import {
  AdvancedQuickSearchTotal,
  AdvancedQuickSearchTotalVariables,
} from '__generated__/AdvancedQuickSearchTotal'
import { ICON_OFFSET } from '../AdvancedSearchInput'
import AdvancedSearchResult from '../AdvancedSearchResult'
import advancedSearchResultsTotalQuery from './queries/advancedSearchResultsTotalQuery'
import Box from 'v2/components/UI/Box'

interface AdvancedQuickSearchResultProps {
  index: number
  maxResults: number
  pathname: string
  selected: boolean
}

const Container = styled(Box).attrs({
  pr: 6,
  py: 6,
  bg: 'gray.light',
  borderTop: '1px solid',
  borderColor: 'gray.semiLight',
})<{ hideHover?: boolean }>`
  display: flex;
  text-decoration: none;
  flex-direction: row;
  align-items: center;
  cursor: pointer;
`

const AdvancedNoResults: React.FC = () => {
  return (
    <Container key={`no_results_0`} pl={ICON_OFFSET} bg="gray.light">
      <Text fontWeight="bold">No results</Text>
    </Container>
  )
}

export const AdvancedSearchResultsTotal: React.FC<AdvancedQuickSearchResultProps> = ({
  index,
  maxResults,
  pathname,
  selected,
}) => {
  const { generateUrl, state } = useContext(AdvancedSearchContext)
  const navigate = useNavigate()
  const { addRecentSearch } = useRecentSearches()
  const { data, loading, refetch } = useQuery<
    AdvancedQuickSearchTotal,
    AdvancedQuickSearchTotalVariables
  >(advancedSearchResultsTotalQuery, {
    variables: state.variables,
  })

  useEffect(() => {
    refetch(state.variables)
  }, [state.variables])

  const term = state.variables.term?.facet

  const handleClick = useCallback(() => {
    addRecentSearch(state.variables)
    return navigate(generateUrl(false, pathname))
  }, [state, addRecentSearch])

  if (loading) {
    const loadingLabel = term
      ? `See all results for '${term}'`
      : 'See all results'
    return (
      <AdvancedSearchResult
        key={`see_all_results_${index === maxResults - 1}`}
        to={generateUrl(false, pathname)}
        pl={ICON_OFFSET}
        selected={selected}
      >
        <Text fontWeight="bold">{loadingLabel}</Text>
      </AdvancedSearchResult>
    )
  }

  const total = data?.searches?.advanced.total

  if (total === 0) {
    return <AdvancedNoResults />
  }

  const searchLabel =
    term && total > 0
      ? `See all ${total} results for '${term}'`
      : 'See all results'

  return (
    <AdvancedSearchResult
      key={`see_all_results_${index === maxResults - 1}`}
      pl={ICON_OFFSET}
      selected={selected}
      onClick={handleClick}
    >
      <Text fontWeight="bold">{searchLabel}</Text>
    </AdvancedSearchResult>
  )
}

export default AdvancedSearchResultsTotal

{'fraction_non_alphanumeric': 0.11881868131868131, 'fraction_numerical': 0.003949175824175824, 'mean_word_length': 3.9197635135135136, 'pattern_counts': {'":': 0, '<': 33, '<?xml version=': 0, '>': 42, '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': '3756903', 'n_tokens_mistral': 1772, 'n_tokens_neox': 1703, 'n_words': 376}