<commit_before>import {useState, useEffect} from 'react'
import client from 'part:@sanity/base/client'
import {observeForPreview} from 'part:@sanity/base/preview'
import {Reference, SchemaType} from '../../types'
import {noop} from '../helpers'

interface PreviewSnapshot {
  title: string
}

export function useRefValue<T = unknown>(refId: string | undefined | null): T | undefined {
  const [value, setValue] = useState<T | undefined>(undefined)
  useEffect(() => {
    if (!refId) {
      return noop
    }

    const subscription = client.observable.getDocument(refId).subscribe(setValue)

    return () => {
      subscription.unsubscribe()
    }
  }, [refId])
  return value
}

export function useRefPreview(
  value: Reference | undefined | null,
  schemaType: SchemaType
): PreviewSnapshot | undefined {
  const [preview, setPreview] = useState<PreviewSnapshot | undefined>(undefined)
  useEffect(() => {
    let subscription
    if (value) {
      subscription = observeForPreview(value, schemaType).subscribe(result =>
        setPreview(result.snapshot)
      )
    }

    return () => {
      if (subscription) {
        subscription.unsubscribe()
      }
    }
  }, [value, schemaType])
  return preview
}
<commit_msg>[field] Fix bug where ref/preview hooks would return stale value<commit_after>import {useState, useEffect} from 'react'
import client from 'part:@sanity/base/client'
import {observeForPreview} from 'part:@sanity/base/preview'
import {Reference, SchemaType} from '../../types'
import {noop} from '../helpers'

interface PreviewSnapshot {
  title: string
}

export function useRefValue<T = unknown>(refId: string | undefined | null): T | undefined {
  const [value, setValue] = useState<T | undefined>(undefined)
  useEffect(() => {
    if (!refId) {
      return noop
    }

    const subscription = client.observable.getDocument(refId).subscribe(setValue)

    return () => {
      subscription.unsubscribe()
    }
  }, [refId])

  // Always return undefined in the case of a falsey ref to prevent bug
  // when going from an ID to an undefined state
  return refId ? value : undefined
}

export function useRefPreview(
  value: Reference | undefined | null,
  schemaType: SchemaType
): PreviewSnapshot | undefined {
  const [preview, setPreview] = useState<PreviewSnapshot | undefined>(undefined)

  useEffect(() => {
    let subscription
    if (value) {
      subscription = observeForPreview(value, schemaType).subscribe(result =>
        setPreview(result.snapshot)
      )
    }

    return () => {
      if (subscription) {
        subscription.unsubscribe()
      }
    }
  }, [value, schemaType])

  return value ? preview : undefined
}

{'fraction_non_alphanumeric': 0.12215166230855436, 'fraction_numerical': 0.0, 'mean_word_length': 3.538983050847458, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 19, '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': '1361569', 'n_tokens_mistral': 762, 'n_tokens_neox': 768, 'n_words': 207}