import { trpc } from '@/lib/trpc'

export function useNewsList(kategorie?: string) {
  const { data, isLoading, refetch, isFetching } = trpc.news.list.useQuery({
    kategorie: kategorie || undefined,
  })

  return {
    data: data ?? [],
    isLoading,
    refetch,
    isRefetching: isFetching,
  }
}

export function useNewsDetail(id: string) {
  const utils = trpc.useUtils()
  const { data, isLoading } = trpc.news.byId.useQuery({ id })

  const markReadMutation = trpc.news.markRead.useMutation()

  function onOpen() {
    markReadMutation.mutate({ newsId: id })
    utils.news.list.invalidate()
  }

  return { data: data ?? null, isLoading, onOpen }
}
